As explained in the title I am to write a program that prints an array of floating point numbers using a malloc function.
Well, I did that successfully, to a point.
At first I planned to just print the array as integers. Which I did as you can see by the code I'm about to post. Now that I want to change the integers to floats It doesn't seem to be accepting.
F:\cet\malloc.c||In function 'main':|
F:\cet\malloc.c|11|error: invalid operands to binary + (have 'float *' and 'float')|
F:\cet\malloc.c|14|error: invalid operands to binary + (have 'float *' and 'float')|
||=== Build finished: 2 errors, 0 warnings ===|
There are my errors as compiled with floating point numbers.
I would also like to add in I am instructed to calculate the average of numbers entered. You will see how I attempted this, with the code commented out. I will attach a photo of my compiler of my attempt to calculate the average of my array. As you can see in the jpeg the average is incorrect. I'm not sure if that's a memory leak or what. . .
Here is my code;
int main(void)
{
int *list, sortnum, j, average = 0, sum = 0;
printf("Enter the number of data points to average: ");
scanf("%d",&sortnum);
list = (int *)malloc(sortnum * sizeof(int));
for (j = 0; j < sortnum; j++)
scanf("%d", list + j);
printf("You entered the following:\n");
for (j = 0; j < sortnum; j++)
printf("%d\n", *(list + j));
free(list);
/*sum = *(list + j) / sortnum + list;
average = list + j / sortnum;
printf("%d", average);*/
return 0;
}
I hope I've given you all of the information you need to give me a helping hand.
Any feedback will be appreciated, thanks.
blu
This post has been edited by blu: 08 March 2012 - 04:17 PM

New Topic/Question
Reply




MultiQuote






|