Hello! I am attempting to write some code that will find the average of values in an array. Using my textbook, I found some code that took an array and found the mean, median, and mode. I attempted to modify that example to be used simply for finding the mean for an array with 5 values. I wanted to be able to call it from main with a prototype rather than have it in main(). I also want the fucntion to work with any size array. I thought that I could solve this by using SIZE_MAX, which I have never used before and only found through a google search. I have been recieving Before I had included SIZE_MAX, the code did run with SIZE originally defined as 99. However, when solving for the average, the numbers were very off.
Here are some of the errors I recieved when trying to run the code:
note: this is the location of the previous definition
# define SIZE_MAX (18446744073709551615UL)
error: expected expression before ‘;’ token
for (size_t j = 0; j < SIZE_MAX; ++j)
error: expected expression before ‘)’ token
printf("The average of the array is %4f.\n", (double) total / SIZE_MAX);
I would appreciate any insight on the use of SIZE_MAX in C or information on an error in my math that may have messed things up. Thank you in advance for all help provided. It is greatly appreciated.
Code:
#include <stdio.h>#include <stdint.h>
#include <limits.h>
#define SIZE_MAX //Do I need this????????
void mean(const unsigned int array[]);
int main(void)
{
unsigned int array[] = {707, 3000, 5, 1, 3};
mean(array);
}
void mean(const unsigned int array[])
{
unsigned int total = 0;
for (size_t j = 0; j < SIZE_MAX; ++j)
{
total += array[j];
}
printf("The average of the array is %4f.\n", (double) total / SIZE_MAX);
}