my program suppose input:
23 5 87 -3 29 87 0 104 300 100 55 -34 -3 101 33 23
suppose output the bad scores:
-3 104 300 -34 -3 101
suppose output the good scores:
23 5 87 29 87 0 100 55 33 23
when i calculate the sum of good scores, and finding the average of the good scores, it cannot output the correct scores.
here is the code:
Code:#include <iostream> #include <fstream> #include <iomanip> using namespace std; #define inFile "c:\\input.txt" #define outFile "c:\\output.txt" const int arraySize = 16; void getInput(ifstream& indata, int list[], int listSize); void badSocres(ofstream& outdata, int listOne[], int listTwo[], int listSize); void goodScores(ofstream& outdata, int listOne[], int listThree[], int listSize); void calScores(ofstream& outdata, int listThree[]); int main () { ifstream indata; ofstream outdata; int listA[arraySize]; int listB[arraySize]; int listC[arraySize]; indata.open(inFile); if (indata.fail()) { cout << "***ERROR:Cannot open" << inFile << " for input." << endl; return EXIT_FAILURE; } outdata.open(outFile); if (outdata.fail()) { cout << "***ERROR:Cannot open" << outFile << " for output." << endl; indata.close(); return EXIT_FAILURE; } if (indata.eof()) { cout << "Sorry there are no integers" << endl; indata.close(); outdata.close(); return EXIT_FAILURE; } getInput(indata, listA, arraySize); badSocres(outdata, listA, listB, arraySize); goodScores(outdata, listA, listC, arraySize); calScores(outdata, listC); indata.close(); outdata.close(); return 0; } void getInput(ifstream& indata, int list[], int listSize) { int index; for (index = 0; index < listSize; index++) indata >> list[index]; } void badSocres(ofstream& outdata, int listOne[], int listTwo[], int listSize) { int index; outdata << "6 bad scores:" << endl; for (index = 0; index < listSize; index++) { if(listOne[index] < 0 || listOne[index] > 100) { listTwo[index] = listOne[index]; outdata << setw(4) << listTwo [index]; } } } void goodScores(ofstream& outdata, int listOne[], int listThree[], int listSize) { int index; outdata << endl; outdata << "10 good scores:" << endl; for (index = 0; index < listSize; index++) { if(listOne[index] >= 0 && listOne[index] <= 100) { listThree[index] = listOne[index]; outdata << setw(4) << listThree[index]; } } } void calScores(ofstream& outdata, int listThree[]) { int index, average; int sum = 0; outdata << endl; for (index = 0; index < 10; index++) { sum = sum + listThree[index]; if(listThree[index] == 100) { outdata << "The hightest good score is:" << listThree[index]; outdata << endl; } if(listThree[index] == 0) { outdata << "The lowest good socre is:" << listThree[index]; outdata << endl; } } average = sum / index; outdata << "The average of good scores is:" << average; }



LinkBack URL
About LinkBacks



You should also read my other posts, especially the one about finding the maximum number in an array.