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 145 146
|
/*
I'm supposed to be able to create two arrays and a text file, then store the
20 numbers(grades) into one array and transfer that to the second array of
which will distribute and display the distribution, all through functions.
Along with that I also must use the data to calculate the largest, smallest,
and average in a separate function, but my output is just hella crazy. Just
dove into programming this semester and its tough. Any suggestions will help.
Please and thank you!
NOTE: The output is showing negative and exaggerated numbers i.e. (-8990377)
and it is looping 20 times in both double calcResults and void displayResults
functions.
The content of the txt file was:
88 85 89 64 78 85 92 60 91 96 63 59 83 89 74 93 92 92 63 100
cpp.sh/5adm6
*/
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
//data type int to capture the integer values into the function
int getDataFromFile(int data[], int size);
//double data type needed for calculating sums and the average
double calcResults(int data[],int size, double& smallest, double& largest, double& average);
//int data type to display all results
void displayResults(int data[],int size);
int main()
{
const int dataFromFile = 19; //array for storing the data from the file
int data[dataFromFile];
// kbw: We don't need a duplicate, do we?
// const int gradeCount = 19; //array for distributing grades
// int grades[gradeCount];
double smallest = 0,
largest = 0,
average = 0.0;
// int i;
// kbw: pass in array and its size, get number of items used
int n = getDataFromFile(data, dataFromFile);
// kbw: we don't need a seperate grades
// for(i = 0; i < n; i++)
// {
// grades[i] = data[i];
// }
// kbw: pass in data we read from file, and its size,
// kbw: pass results by reference
calcResults(data, n, smallest, largest, average);
// kbw: print the grades we read in
displayResults(data, n);
// for(i = 0; i < gradeCount; i++)
// {
// displayResults(grades[i]);
// }
system("pause");
return 0;
}
int getDataFromFile(int data[],int size)
{
// int NUM_GRADES = 19; // kbw: we pass in NUM_GRADES as a parameter
// data[NUM_GRADES]; // kbw: we use data[] as a parameter, we don't need another one
ifstream file("input.txt"); // kbw: initialise the stream with the filename
// file.open("input.txt"); // kbw: don't call open/close
if (!file)
{
cout << "Error opening file\n";
}
// kbw: loop while there's more to read and we have space for it
int i; // kbw: i isn't local to the loop
for(i = 0; i < size && file; i++)
{
file >> data[i];
// file.close(); // kbw: we don't close() until we're done
}
return i; // kbw: return number items in the array
}
double calcResults(int data[],int size, double& smallest, double& largest, double& average)
{
// const int NUMS = 19; // kbw: use the size passed in
// int calculate[NUMS]; // kbw: use the data passed in
// kbw: assign reasonable start values
average = 0.0;
largest = 0.0;
smallest = 100.0;
//finding the sums
double totalNum = 0.0;
for (int x = 0; x < size; x++) // kbw: use size passed in
{
totalNum += data[x]; // kbw: use data passed in
// average = totalNum / NUMS; // kbw: calculate the average out the loop
// kbw: we don't do min/max like this
// largest = smallest = calculate[0];
// for (int x = 1; x < NUMS; x++)
// {
// if (calculate[x] > largest)
// largest = calculate[x];
// else if (calculate[x] < smallest)
// smallest = calculate[x];
// }
// kbw: we do min/max like this
if (data[x] > largest)
largest = data[x];
if (data[x] < smallest)
smallest = data[x];
}
average = totalNum / size; // kbw: all this stuff moved from inside the loop
cout << fixed << showpoint << setprecision(2) << endl;
cout << "Smallest: " << setw(9) << smallest << endl;
cout << "Largest: " << setw(9) << largest << endl;
cout << "Average: " << setw(9) << average << endl;
return totalNum;
}
void displayResults(int data[],int size)
{
cout << "The final grade results are:" << endl;
// kbw: write all the grades in a loop
for (int i = 0; i < size; ++i)
cout << " " << data[i] << endl;
}
|