Thread: sum an array using function

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    72

    sum an array using function

    Hello,

    I created the following code:

    Code:
    // Headers and Other Technical Items
    
    #include <iostream>
    #include <fstream>         // For file I/O 
    #include <iomanip>         // For setw()
    using namespace std;
    
    // Function Prototypes
    
    int  count_file_values(char input_filename[]);
    void load_array_from_file(char input_filename[], int things[], int array_size);
    void display_array(int things[], int array_size);
    int  sum_array(int things[], int array_size);
    void pause(void);
    
    // Variables
     
    char     data_filename[]  = "C:\Cpp_Source_Code_Files\Chapter_19\Demo_Farm_Acres_Input";
    
    //******************************************************
    // main
    //******************************************************
    
    int main(void)
      {
      int record_count = count_file_values(data_filename);
      int acres[record_count];
      load_array_from_file(data_filename, acres, record_count);
      cout << "The individual farm acerages are: \n";
      display_array(acres, record_count);
      cout << "\n\nThe total acerage is: " << sum_array(acres, record_count);
      pause();
    
      return 0;
      }
    
    //******************************************************
    // count_file_values
    //******************************************************
    
    int count_file_values(char input_filename[])
      {
      fstream  inData;           // device token for the input stream
      double   next_value;
      int      number_of_values = 0;
      
      inData.open(input_filename, ios::in);        //Open input file
      if (!inData)
        {
        cout << "\n\nError opening input data file: " << input_filename << "\n\n";
        pause();
        exit(EXIT_FAILURE);
        }
      // Get data values and count them
    
      while (inData >> next_value)
        {
        number_of_values++;
        }
    
      // Close the files
      inData.close();                              //Close input file
    
      return number_of_values;
      }
    
    //******************************************************
    // load_array_from_file
    //******************************************************
    
    void load_array_from_file(char input_filename[], int things[], int array_size)
      {
      fstream  inData;           // device token for the input stream
    
      inData.open(input_filename, ios::in);        //Open input file
      if (!inData)
        {
        cout << "\n\nError opening input data file: " << input_filename << "\n\n";
        pause();
        exit(EXIT_FAILURE);
        }
      else
        {
        for (int i = 0; i < array_size; i++)   // Load the array
          {
          inData >> things[i];
          }
        inData.close();
        }
      return;
      }
    
    //******************************************************
    // display_array
    //******************************************************
    
    void display_array(int things[], int array_size)
      {
    
      cout << "\n************************************";
      for (int i = 0; i < array_size; i++)
        {
        cout << "\nOffset: " << setw(2) << i;
        cout << " Memeber: " << setw(2) << i+1;
        cout << " value is: " << setw(3) << things[i];
        }
      cout << "\n************************************";
    
      return;
      }
    
    //******************************************************
    // sum_array
    //******************************************************
    
    int sum_array(int things[], int array_size)
      {
      int  total = 0;         // Accumulator
    
      for (int i = 0; i < array_size; i++)
        {
        total += things[i];
        }
      return total;
      }
    
    //******************************************************
    // pause
    //******************************************************
    
    void pause(void)
      {
      cout << "\n\n";
      system("PAUSE");
      cout << "\n\n";
      return;
      }

    Now I have to add a function that returns an integer value after being passed an array and the number of array elements.elements. The value it should return is the average of the values that are within the array ( integer division ).
    The average would be the total divided by the number of elements in the array and it will truncate to a whole number ( that's ok). Lets say it would be average_array. Just supposed to display the average within the function main following the display of the sum.

    I am really having difficulties with that. I don't understand how I would do it. I hope somebody can help me

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You already have a function to sum the values in the array. You can use this in the function that calculates the average.

    P.S

    Code:
      int record_count = count_file_values(data_filename);
      int acres[record_count];
    Were you taught to program like that or did you discover it on your own that it "works"?

    This is not valid C++, and if you really don't want to or can't use standard C++ library (vector), at least it should look like:

    Code:
      int record_count = count_file_values(data_filename);
      int* acres = new int[record_count];
      //use as before
      //...
      //when done deallocate memory
      delete [] acres;
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    Quote Originally Posted by anon View Post
    You already have a function to sum the values in the array. You can use this in the function that calculates the average.

    P.S

    Code:
      int record_count = count_file_values(data_filename);
      int acres[record_count];
    Were you taught to program like that or did you discover it on your own that it "works"?

    This is not valid C++, and if you really don't want to or can't use standard C++ library (vector), at least it should look like:

    Code:
      int record_count = count_file_values(data_filename);
      int* acres = new int[record_count];
      //use as before
      //...
      //when done deallocate memory
      delete [] acres;
    Thanks. Well, the teacher is kind of strange.

    Do you mean this part?

    Code:
    int sum_array(int things[], int array_size)
      {
      int  total = 0;         // Accumulator
    
      for (int i = 0; i < array_size; i++)
        {
        total += things[i];
        }
      return total;
      }
    Last edited by XodoX; 04-21-2009 at 03:57 PM.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Was the rest of the code given to you?

    You know how to calculate the average. You have a function that can return the sum of the elements in an array. You have plenty of examples of functions with the requested prototype. You have code that demonstrates how to use the existing functions.

    What is it that you don't understand?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    No, it wasn't.

    I don't know , I just dont get it. So I just modify it so that I can get the sum?

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Modify what? The function already gets you the sum. All you need is another function that preferably reuses the summing function but returns the average (sum / number of elements) instead of the sum.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    Ok, that will work. So basically the average_array to capture the already calculated sum. I'll try that.
    Last edited by XodoX; 04-21-2009 at 04:49 PM.

  8. #8
    Registered User
    Join Date
    Jul 2008
    Posts
    72
    I came up with this now..


    Code:
    {
    average = 0;
    sum = 0;
    for (int i = 0; i < array_size; i++)
    sum += things[i];
    average = double(sum)/ array_size;
    }

    There's an error in there that I can't find. Would that be a good way though?

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    What are the errors? What does the compiler say? What lines does it points you to?

    You haven't understood the suggestion that you use the existing sum_array function, and by that I didn't mean copying and pasting code.

    Seeing this it is virtually impossible to believe the code you have posted before doesn't come from your teacher or some colleague...
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM
  5. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM