Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- #include <string>
- using namespace std;
- const int MAX_SIZE = 100;
- void Fill_Array(int Size[], int& count, int& numbers_used);
- void Print_Array(int Size[], int count);
- double Calc_Average(int Size[], double average);
- void Sort(int Size[], int numbers_used);
- void Swap(int& v1, int& v2);
- int index_of_smallest(const int Size[], int start_index, int number_used);
- int Calc_Median(int median, int Size[]);
- void Print_Array_and_Calculations(int median, double average);
- int main()
- {
- ifstream in_size;
- int count;
- int Size [MAX_SIZE],
- numbers_used = 0,
- median = 0;
- double average = 0.0;
- Fill_Array(Size, count, numbers_used);
- Print_Array(Size, count);
- average = Calc_Average(Size, average);
- Sort(Size, numbers_used);
- Calc_Median(median, Size);
- Print_Array_and_Calculations(median, average);
- in_size.close();
- return 0;
- }
- void Fill_Array(int Size[], int& count, int& numbers_used)
- {
- int size;
- char name[16];
- ifstream in_size;
- string name;
- in_size.open (name);
- cout << "Enter the file to read in: ";
- cin >> name;
- cout << endl << "The numbers in the array are:" << endl << endl;
- if(in_size.fail())
- {
- cerr << "Error opening file" << endl;
- exit(1);
- }
- count = 0;
- in_size >> size;
- while((!in_size.eof()) && (count <= MAX_SIZE))
- {
- Size[count] = size;
- count++;
- in_size >> size;
- }
- in_size.close();
- }
- void Print_Array(int Size[], int count)
- {
- int number_used = 0;
- for(int index = 0; index < number_used; index++)
- cout << Size[index] << " ";
- }
- double Calc_Average(int Size[], double average)
- {
- int total = 0;
- for (int i = 0; i < MAX_SIZE; i++)
- {
- total = total + Size[i];
- }
- average = double(total) / MAX_SIZE;
- return average;
- }
- void Sort(int Size[], int number_used)
- {
- int index_of_next_smallest;
- for (int index = 0; index < number_used - 1; index++)
- {
- index_of_next_smallest = index_of_smallest(Size, index, number_used);
- Swap(Size[index], Size[index_of_next_smallest]);
- }
- }
- void Swap(int& v1, int& v2)
- {
- int temp;
- temp = v1;
- v1 = v2;
- v2 = temp;
- }
- int index_of_smallest(const int Size[], int start_index, int number_used)
- {
- int min = Size[start_index],
- index_of_min = start_index;
- for (int index = start_index + 1; index < number_used; index++)
- if(Size[index] < min)
- {
- min = Size[index];
- index_of_min = index;
- }
- return index_of_min;
- }
- int Calc_Median(int median, int Size[])
- {
- median = Size [ MAX_SIZE / 2 ];
- return median;
- }
- void Print_Array_and_Calculations(int median, double average)
- {
- cout << endl << "The average of the numbers is " << average;
- cout << endl << "The median of the numbers is " << median;
- cout << endl << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement