not sure if I did this right or not but returning alot of errors:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
void Fill_Array(int Size[], int& count, int& tempcount);
void Print_Array(int Size[], int count);
double Calc_Average(int Size[], double average);
void Sort(int Size[], int tempcount);
void Swap(int& v1, int& v2);
int index_of_smallest(const int Size[], int start_index, int tempcount);
cout << "The numbers in the array are: " << endl << Size[start_index] << endl;
int Calc_Median(int median, int Size[]);
void Print_Array_and_Calculations(int median, double average);
const int MAX_SIZE = 100;
int main()
{
ifstream in_size;
int count;
int Size[MAX_SIZE],
tempcount = 0,
median = 0;
double average = 0.0;
Fill_Array(Size, count, tempcount);
Print_Array(Size, count);
average = Calc_Average(Size, average);
Sort(Size, tempcount);
Calc_Median(median, Size);
Print_Array_and_Calculations(median, average);
in_size.close();
return 0;
}
void Fill_Array(int Size[], int& count, int& tempcount)
{
int size;
ifstream in_size;
string text_file;
cout << "Enter the file to read in: ";
getline(cin, text_file);
cout << endl << "The numbers in the array are:" << endl << endl;
in_size.open (text_file.c_str ());
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 tempcount = 1;
for(int index = 0; index < MAX_SIZE; index++)
cout << Size[index] << " ";
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(1);
if (tempcount == 4)
{
cout << endl;
tempcount = 1;
}
else
tempcount++;
}
double Calc_Average(int Size[], double average)
{
int total = 0;
for (int i = 0; i < 100; i++)
{
total = total + Size[i];
}
average = double(total) / 100;
return average;
}
void Sort(int Size[], int tempcount)
{
int index_of_next_smallest;
for (int index = 0; index < tempcount - 1; index++)
{
index_of_next_smallest = index_of_smallest(Size, index, tempcount);
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 tempcount)
{
int min = Size[start_index],
index_of_min = start_index;
for (int index = start_index + 1; index < tempcount; 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;
}
|
Instructions:
Write a program that allows the user to enter a filename and then puts all of the numbers into an array. It will print the array, sort the array, find the average of all of the numbers, find the median of all of the numbers, and print the sorted array and all of the answers. The array should not hold more than 100 numbers (this is the MAX_SIZE)
Below are the functions and a description of what they should do
Fill_Array – This function allows the user to type in a filename, open the file and checks to see if it opened correctly. It then puts all numbers into an array, and keeps count of how many numbers are entered. It should make sure that it doesn’t fill more than the MAX_SIZE of the array.
Print_Array - Prints all of the numbers in the array, 4 per line with one decimal point
Calc_Average – This function returns the average of all of the numbers in the array.
Sort - This function sorts the array. After this function is executed, the numbers in the array will be sorted. The order of the original array will be gone. It calls the function Swap to swap two integers in the array. If you want to call more functions from within this function, you can.
Swap – This function accepts two integers as it formal parameters and exchanges their values.
Calc_Median – This function returns the median number. The numbers must be sorted to find the median number. The median number is the number in the middle. For example, if the numbers were 75, 78, 80, 89, and 99 the median number would be an 80. If the numbers were 65, 68, 72, and 98, the median number would be the average of the two middle numbers (68 and 72) so the median would be 70. THERE IS NO LOOP IN THIS FUNCTION!!!
Print_Array_and_Calculations – This function prints the array (by calling Print_Array) and then prints the average and the median.
Note: main consists of 6 function calls, and some declarations. There is no loop in main.
This is what it is supposed to look:
Enter the file to read in: data1.txt
The numbers in the array are:
45.3 57.4 23.6 34.2
234.6 34.9 54.8 934.9
8432.5 9809.3 539.7 43.9
12.0
The numbers in the array are:
12.0 23.6 34.2 34.9
43.9 45.3 54.8 57.4
234.6 539.7 934.9 8432.5
9809.3
The average of the numbers is 1558.2
The median of the numbers is 54.8
Press any key to continue