Advertisement
Guest User

code

a guest
Nov 26th, 2012
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string>
  5. using namespace std;
  6. const int MAX_SIZE = 100;
  7. void Fill_Array(int Size[], int& count, int& numbers_used);
  8. void Print_Array(int Size[], int count);
  9. double Calc_Average(int Size[], double average);
  10. void Sort(int Size[], int numbers_used);
  11. void Swap(int& v1, int& v2);
  12. int index_of_smallest(const int Size[], int start_index, int number_used);
  13. int Calc_Median(int median, int Size[]);
  14. void Print_Array_and_Calculations(int median, double average);
  15. int main()
  16. {
  17.     ifstream in_size;
  18.     int count;
  19.     int Size [MAX_SIZE],
  20.         numbers_used = 0,
  21.         median = 0;
  22.     double average = 0.0;
  23.     Fill_Array(Size, count, numbers_used);
  24.     Print_Array(Size, count);
  25.     average = Calc_Average(Size, average);
  26.     Sort(Size, numbers_used);
  27.     Calc_Median(median, Size);
  28.     Print_Array_and_Calculations(median, average);
  29.     in_size.close();
  30.     return 0;
  31. }
  32. void Fill_Array(int Size[], int& count, int& numbers_used)
  33. {
  34.     int size;
  35.     char name[16];
  36.     ifstream in_size;
  37.     string name;
  38.     in_size.open (name);
  39.     cout << "Enter the file to read in: ";
  40.     cin >> name;
  41.     cout << endl << "The numbers in the array are:" << endl << endl;
  42.     if(in_size.fail())
  43.     {
  44.         cerr << "Error opening file" << endl;
  45.         exit(1);
  46.     }
  47.     count = 0;
  48.     in_size >> size;
  49.     while((!in_size.eof()) && (count <= MAX_SIZE))
  50.     {
  51.         Size[count] = size;
  52.         count++;
  53.         in_size >> size;
  54.     }
  55.     in_size.close();
  56. }
  57. void Print_Array(int Size[], int count)
  58. {
  59.     int number_used = 0;
  60.     for(int index = 0; index < number_used; index++)
  61.         cout << Size[index] << " ";
  62. }
  63. double Calc_Average(int Size[], double average)
  64. {
  65.     int total = 0;
  66.     for (int i = 0; i < MAX_SIZE; i++)
  67.     {
  68.         total = total + Size[i];
  69.     }
  70.     average = double(total) / MAX_SIZE;
  71.     return average;
  72. }
  73. void Sort(int Size[], int number_used)
  74. {
  75.     int index_of_next_smallest;
  76.     for (int index = 0; index < number_used - 1; index++)
  77.     {
  78.         index_of_next_smallest = index_of_smallest(Size, index, number_used);
  79.         Swap(Size[index], Size[index_of_next_smallest]);
  80.     }
  81. }
  82. void Swap(int& v1, int& v2)
  83. {
  84.     int temp;
  85.     temp = v1;
  86.     v1 = v2;
  87.     v2 = temp;
  88. }
  89. int index_of_smallest(const int Size[], int start_index, int number_used)
  90. {
  91.     int min = Size[start_index],
  92.         index_of_min = start_index;
  93.     for (int index = start_index + 1; index < number_used; index++)
  94.         if(Size[index] < min)
  95.         {
  96.             min = Size[index];
  97.             index_of_min = index;
  98.         }
  99.         return index_of_min;
  100. }
  101. int Calc_Median(int median, int Size[])
  102. {
  103.     median = Size [ MAX_SIZE / 2 ];
  104.     return median;
  105. }
  106. void Print_Array_and_Calculations(int median, double average)
  107. {
  108.     cout << endl << "The average of the numbers is " << average;
  109.     cout << endl << "The median of the numbers is " << median;
  110.     cout << endl << endl;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement