Thread: Average of Values in an Array: Using SIZE_MAX?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    8

    Average of Values in an Array: Using SIZE_MAX?

    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);
    }
    Last edited by G707; 05-05-2019 at 01:28 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program to output min, max and average values
    By auspeedy in forum C Programming
    Replies: 2
    Last Post: 05-19-2015, 12:26 AM
  2. Array average help
    By internalbalance in forum C Programming
    Replies: 15
    Last Post: 03-24-2014, 02:08 PM
  3. display average pixel values
    By defunktlemon in forum C# Programming
    Replies: 2
    Last Post: 02-04-2013, 09:48 PM
  4. Average of an Array
    By Peteski in forum C Programming
    Replies: 19
    Last Post: 01-05-2012, 04:38 PM
  5. average in an array
    By s_ny33 in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2005, 01:03 PM

Tags for this Thread