Thread: Question?

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    16

    Question?

    How do I find the average of the values that are within the array? I was planning on using interger division since I'm using interger values.

    Code:
     
    #include <iostream>
    #include <fstream>         
    #include <iomanip>         
    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);
    
    
    void pause(void);
    
    // Variables
    char     data_filename[]  = "C:\\Data_Files\\Demo_Farm_Acres_Input.txt";
    
    //******************************************************
    // main
    //******************************************************
    
    int main(void)
      {
      int record_count = count_file_values(data_filename);
      int areas[record_count];
      load_array_from_file(data_filename, areas, record_count);
      cout << "The areas are: \n";
      display_array(areas, record_count);
      pause();
    
      return 0;
      }
    
    //******************************************************
    // count_file_values
    //******************************************************
    
    int count_file_values(char input_filename[])
      {
      fstream  inData;           
      double   next_value;
      int      number_of_values = 0;
      
      inData.open(input_filename, ios::in);        
      if (!inData)
        {
        cout << "\n\nError " << input_filename << "\n\n";
        pause();
        exit(EXIT_FAILURE);
        }
      while (inData >> next_value)
        {
        number_of_values++;
        }
    
      // Close the files
      inData.close();                              
    
      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 << "\nInterger: " << setw(2) << i;
        cout << " Decimal: " << setw(2) << i+1;
        cout << " value is: " << setw(3) << things[i];
        }
      cout << "\n************************************";
    
      return;
      }

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    No need to read the file once first to find the number of lines. Just use a std::vector and let it grow.

    In this case, you don't even need that. Just keep 2 variables, count and sum. At the end, avg = sum / count.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    I understood the avg = sum part, but what did you mean before that?
    Last edited by Guru_; 04-20-2009 at 12:53 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Guru_ View Post
    I understood the avg = sum part, but what did you mean before that?
    You are reading the file twice, once to find out how many lines it has, and once to actually read the values from the file. If you use std::vector, you can just use "push_back" to automatically grow the vector [which is a C++ version of array] to the size required.

    But also, as stated, you do not need more than two variables to get an average, just sum up all the numbers and count them in one loop, and once the loop is finished, print out the sum / count as average.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM