7 Replies - 2150 Views - Last Post: 19 April 2010 - 08:08 PM Rate Topic: -----

#1 texagg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-April 10

C++ Average of an Array

Posted 19 April 2010 - 04:10 PM

Hello,

Have been working on a progressing assignment and have already created the total array count, but Im having a hard time getting my head around how I can use that to find average of the array as well.

Any help would be appreciated. I was thinking I could somehow use total and array_size from the array_sum function but I've tried all different kinds of ways and none of them are working.
Everything in the program works properly, minus the an average function im trying to produce.




#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);
int  sum_array(int things[], int array_size);

void pause(void);


char     data_filename[]  = "C:\\Data_Files\\Demo_Farm_Acres_Input.txt";

//******************************************************
// 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);
  cout << "\n\nThe average acerage is: " << (total / array_size);
  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 opening input data file: " << input_filename << "\n\n";
    pause();
    exit(EXIT_FAILURE);
    }
  

  while (inData >> next_value)
    {
    number_of_values++;
    }

  
  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;           

  inData.open(input_filename, ios::in);        
  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++)   
      {
      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;         

  for (int i = 0; i < array_size; i++)
    {
    total += things[i];
    }
  return total;
  }

//******************************************************
// average_array
//******************************************************

?????
//******************************************************
// pause
//******************************************************

void pause(void)
  {
  cout << "\n\n";
  system("PAUSE");
  cout << "\n\n";
  return;
  }



This post has been edited by texagg: 19 April 2010 - 04:13 PM


Is This A Good Question/Topic? 0
  • +

Replies To: C++ Average of an Array

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: C++ Average of an Array

Posted 19 April 2010 - 04:15 PM

Since you already have sum_array written, you could call that to find the sum, and then divide by array_size to determine the average
Was This Post Helpful? 0
  • +
  • -

#3 texagg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-April 10

Re: C++ Average of an Array

Posted 19 April 2010 - 04:22 PM

That's along the lines of what I was thinking, but I was unsure of how to go about doing that.
Was This Post Helpful? 0
  • +
  • -

#4 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: C++ Average of an Array

Posted 19 April 2010 - 04:44 PM

Well, your average function would need to take two arguments, an int array, and the number of items. You would then call sum_array with the same arguments that were passed to your average function and divide the result by the number of elements. The result of that is what you should return.
Was This Post Helpful? 0
  • +
  • -

#5 texagg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-April 10

Re: C++ Average of an Array

Posted 19 April 2010 - 05:06 PM

View Posterik.price, on 19 April 2010 - 03:44 PM, said:

Well, your average function would need to take two arguments, an int array, and the number of items. You would then call sum_array with the same arguments that were passed to your average function and divide the result by the number of elements. The result of that is what you should return.



When I call the sum function, I include the information in parenthesis correct?

int sum_array(int things[], int array_size)
Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1076
  • View blog
  • Posts: 4,536
  • Joined: 09-June 09

Re: C++ Average of an Array

Posted 19 April 2010 - 06:37 PM

those are called parameters, and yes that is correct. You could also just pass a pointer so your not copying the memory
int sum_array(int *things, int array_size) 



but both ways are fine
Was This Post Helpful? 0
  • +
  • -

#7 texagg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-April 10

Re: C++ Average of an Array

Posted 19 April 2010 - 08:00 PM

I came up with this, but am getting a few compiler errors:

ln function 'integer avg_array(int*,int)':
name lookup of 'i' changed for new ISO 'for' scoping
using obsolete binding at 'i'

Sorry for all the questions! I think I'm almost there!

#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);
int average_array(int things[], int array_size);

void pause(void);


char     data_filename[]  = "C:\\Data_Files\\Demo_Farm_Acres_Input.txt";

//******************************************************
// 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);
  cout << "\n\nThe average acerage is: " << average_array(acres, 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 opening input data file: " << input_filename << "\n\n";
    pause();
    exit(EXIT_FAILURE);
    }
  

  while (inData >> next_value)
    {
    number_of_values++;
    }

  
  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;           

  inData.open(input_filename, ios::in);        
  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++)   
      {
      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;         

  for (int i = 0; i < array_size; i++)
    {
    total += things[i];
    }
  return total;
  }

//******************************************************
// average_array
//******************************************************
int average_array(int things[], int array_size)
 {
  int total  = 0;
  
 for (int i = 0; i < array_size; i++)
 {
total += things[i];
}

 return total/i;
}

//******************************************************
// pause
//******************************************************

void pause(void)
  {
  cout << "\n\n";
  system("PAUSE");
  cout << "\n\n";
  return;
  }

//******************************************************
// End of Program
//******************************************************
[code]

#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);
int average_array(int things[], int array_size);

void pause(void);


char     data_filename[]  = "C:\\Data_Files\\Demo_Farm_Acres_Input.txt";

//******************************************************
// 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);
  cout << "\n\nThe average acerage is: " << average_array(acres, 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 opening input data file: " << input_filename << "\n\n";
    pause();
    exit(EXIT_FAILURE);
    }
  

  while (inData >> next_value)
    {
    number_of_values++;
    }

  
  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;           

  inData.open(input_filename, ios::in);        
  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++)   
      {
      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;         

  for (int i = 0; i < array_size; i++)
    {
    total += things[i];
    }
  return total;
  }

//******************************************************
// average_array
//******************************************************
int average_array(int things[], int array_size)
 {
  int total  = 0;
  
 for (int i = 0; i < array_size; i++)
 {
total += things[i];
}

 return total/i;
}

//******************************************************
// pause
//******************************************************

void pause(void)
  {
  cout << "\n\n";
  system("PAUSE");
  cout << "\n\n";
  return;
  }

//******************************************************
// End of Program
//******************************************************



Was This Post Helpful? 0
  • +
  • -

#8 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1076
  • View blog
  • Posts: 4,536
  • Joined: 09-June 09

Re: C++ Average of an Array

Posted 19 April 2010 - 08:08 PM

well first off you need to learn about dynamic memory allocation
int record_count = count_file_values(data_filename);
  int acres[record_count];


this will never compile because to allocate an array like that you need a constant value since it is allocating on the stack. Dynamic memory allocates memory during run-time on the heap, thats why it is called "dynamic". The only thing is that the stack is cleared when the program ends, not days the heap is supposed to be also but to be safe you must deallocate the memory before the program ends

example
int *acres = new int[record_count]; //allocated on the heap during runtime

//some stuff

//ok im done with the array
delete []acres; //delete the memory at that location
acres = NULL; //set the point to NULL for saftey


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1