Question: I need to create a code for this prompt: Write a C++ program that prompts the user for the filename of an input data file
I need to create a code for this prompt:
| Write a C++ program that prompts the user for the filename of an input data file that contains a list of double-precision numbers representing test scores. The program should then prompt the user for the number of test scores in the input data file. After the user enters the number of test scores in the input data file, the program must declare an array, named scores, which will hold the user-specified number of double- precision test scores. The program must then read the user-specified number of |
| double-precision test scores from the input data file into the program storing each score in the array named scores. After all scores have been stored in the array, your program must invoke a function named sort which sorts the scores in the array into ascending order (low to high) you may use the selectionSort function given on pages 449 and 451 of the textbook. The program must then compute the sum and average of the scores. Finally, the program must prompt the user for the filename of an output data file. The program must open the user-specified output data file and then write the following data to the output data file: |
The total number of scores which were processed. This will be the user-entered number that was used as the size of the array named scores.
The sum of the scores which were processed.
The average of the scores which were processed.
The list of scores in ascending numeric order. For each score in the list,
place an asterisk (*) in front of each score that is below the average. Additionally each score should be followed by its letter grade equivalent using the following scale:
Between 90 and 100 = A Greater than or equal to 80 and less than 90 = B Greater than or equal to 70 and less than 80 = C Greater than or equal to 60 and less than 70 = D Less than 60 = F
Here is an example run showing what your programs console input and output must look like:
Please enter the name of the input file: inputFile.txt Please enter the number of scores in inputFile.txt: 10 Please enter the name of the output file: outputFile.txt Press any key to continue . . .
| Here are the ten scores contained in my inputFile.txt: 90 100 79.0 70 69.5 61 80.5 89.9 0 51 |
| Note: In order to properly test your program, you must generate your own input data file. DO NOT test your program with only my inputFile.txt data file. And here are the contents of outputFile.txt after the program has processed all ten scores in the array named scores: A total number of 10 scores were processed. The sum of these scores is: 690.9. The average of these scores is: 69.09. The individual scores processed were: *0F * 51 F * 61 D 69.5 D 70 C 79 C 80.5 B 89.9 B 90 A 100 A Does my code meet the requirements?: #include "stdafx.h" #include #include #include
using namespace std;
void sort(double grades[], int size); char calGrade(double);
int main() { int n = 0; double avg, sum = 0;; string in_file, out_file;
cout << "Please enter the name of the input file: "; cin >> in_file;
cout << "Please enter the number of scores in inputFile.txt: "; cin >> n;
cout << "Please enter the name of the output file: "; cin >> out_file;
ofstream out; out.open(out_file.c_str());
double *scores = new double[n];
ifstream in; in.open(in_file.c_str());
if (in.fail()) { cout << "Unable to open file" << in_file << endl; exit(0); } for (int i = 0; i < n; i++) { in >> scores[i]; }
sort(scores, n);
for (int i = 0; i < n; i++) sum += scores[i];
avg = (double)sum /(double) n;
out << "A total number of " << n << " scores were processed " << endl; out << "The sum of these scores is: " << avg << endl; out << "The average of these scores is: " << avg << endl; out << "The individual scores processed were: " << endl;
char grade;
for (int i = 0; i < n; i++){ grade = calGrade(scores[i]); if (scores[i] out << " * "; out << scores[i] << " " << grade << endl; } out.close(); cout << "Please check output file " << out_file << endl; cin.ignore(); cin.get(); return 0; } char calGrade(double score){ if (score >= 90 && score < 100) return 'A'; else if (score >= 80 && score < 90) return 'B'; else if (score >= 70 && score < 80) return 'C'; else if (score >= 60 && score < 70) return 'D'; else return 'F'; } void sort(double grades[], int size){ int i, j; double temp; for (i = 0; i < size; i++) { for (j = i + 1; j < size; j++) if (grades[i]>grades[j]) { temp = grades[i]; grades[i] = grades[j]; grades[j] = temp; } } } Also, how do I open an outputfile? where does it need to go, in VS? our my desktop? i guess what I am asking is how to get my final result? i ask becasue when I compile the code, it lets me enter inputFile.txt and total amount of numbers in that file, but when I enter outputFile.txt the programs stops compiling. Thank you for you assistance. |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
