Why is the average for my array of numbers not correct?

Why is the average for my array of numbers not correct?



I have written a method to take an array (via pointer) and its size to calculate the average. I am still relatively new to pointers. I have tried to remove the reference operator all throughout the code to the point at which it will compile, but the values returning are not intended. For instance, if I pass in an array size of 3, and then my array values are 1, 2, 3, with my eyes closed I can tell you that that average should be 2. However my code returns 2.666666667. Thinking in reverse, I multiplied 2.6666...7 by 3, which equals 8. This leads me to think that I somewhere, a 2 is getting amended to the array, like 1,2,3,2, however I'm unsure at this point. If anyone has any experience with pointers/simple arithmetic I'd appreciate your advice, because again, I am new to pointers and this idea of reference by address. Thanks!


double *Average(double *arr, int size)

double *avg;
avg = (double *)malloc(sizeof(double));

*avg = 0;

int i;
for (i = 0; i < size; i++)

*avg = *avg + arr[i];


*avg = *avg / (double) size;

printf("The average of the array: %fn", *avg);
return avg;



How the method gets called from main:


else if (choice == 'C' || choice == 'c')

int count;

printf("How many numbers do you want to use in the array?n> ");
scanf("%d", &count);

double *arr;
arr = (double *)malloc(sizeof(double) * count);

int i;
for (i = 0; i < count; i++)


printf("Please enter a number (%d of %d)n> ", i + 1, count);
scanf("%lf", &arr[i]);


Min(arr, count);

Max(arr, count);

Average(arr, count);



Tests:



Expected: 3, Got: 4.333333



Expected: 1, Got: 1



Expected: 5, Got: 5.888889



Two extra methods per user request:


double *Min(double *arr, int size)

double *min = &arr[0];
int i;
for (int i = 1; i < size; i++)

if (arr[i] < *min)

*min = arr[i];



printf("Smallest number in array: %fn", *min);
return min;


double *Max(double *arr, int size)

double *max = &arr[0];
int i;
for (int i = 1; i < size; i++)

if (arr[i] > *max)

*max = arr[i];



printf("Largest number in array: %fn", *max);
return max;






Show us how you're calling the function. Not just summaries like Average(1, 3, 5, 3) but actual compilable code. A Minimal, Complete, and Verifiable example would be ideal.

– John Kugelman
Sep 13 '18 at 2:05



Average(1, 3, 5, 3)






Can you edit out the code that's not directly relevant? For instance, hard code the array contents instead of taking in user input. And get rid of the Min and Max calls. (Note: If doing either of these changes the program behavior then you have a clue.)

– John Kugelman
Sep 13 '18 at 2:08


Min


Max






Your Average function would be simpler if it returned a double rather than a double*. Make avg a simple double variable. Apart from that, I don't see a problem with your Average function, and it works for me. We definitely need a Minimal, Complete, and Verifiable example. (What you have now is two code fragments. With an MCVE, I could copy-and-paste the entire program onto my system and compile and run it without adding anything.)

– Keith Thompson
Sep 13 '18 at 2:38



Average


double


double*


avg


double


Average






No wonder, you're assigning min and max the address of your array, and then change them(meaning you're changing your array contents.) I will post the answer.

– Mediocre
Sep 13 '18 at 3:26


min


max






A function that computes an average should compute an average and do nothing else. If you need to print the result, do that in a function that calls Average. This way you get a chance to actually use your return value and free memory you malloc. Same thing about min and max. To make sure you don't inadvertently change the array, use const double* parameter type.

– n.m.
Sep 13 '18 at 3:37


const double*




1 Answer
1



Problem is here, correct this one:


*max = arr[i];



you're changing the *max which is arr[0] to the max or min of your array. So when Average() function gets the array, it's not the array you've input, it's changed. For example:


*max


arr[0]


Average()


Max(1,3,5)=5



array becomes:


arr=5,3,5



It's average correctly is:


Average(5,3,5 = 4.3333



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)