Diese Präsentation wurde erfolgreich gemeldet.
Die SlideShare-Präsentation wird heruntergeladen. ×

Write a C++ program to compute numeric grades for a course- The course.docx

Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige

Hier ansehen

1 von 2 Anzeige

Write a C++ program to compute numeric grades for a course- The course.docx

Herunterladen, um offline zu lesen

Write a C++ program to compute numeric grades for a course. The course records are in a file that will serve as the input file. The input file is in exactly the fol- lowing format: each line contains a student’s last name, then one space, the the student’s first name, then one space, then ten quiz scores all on one line The quiz scores are whole numbers and are separated by one space. Your program will take its input from this file and send its output to a second file. The data in the output file will be the same as the data in the input file except that there will be one additional number (of type double) at the end of each line. This number will be the average of the student’s ten quiz scores. If this is being done as a class assignment, obtain the file names from your instructor. use at least one function that has file streams as all or some of its arguments.
use the file \"6_02_in\" in the general section of Moodle. The file could include more data with the same format. Use 6_02_out for output file name.
Solution
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
ifstream infile;
ofstream outfile;
string filename;
cout<<\"Enter input filename\";
cin>>filename;
infile.open(filename.c_str());
cout<<\"Enter output filename\";
cin>>filename;
outfile.open(filename.c_str());
string firstname,lastname;
int grade[10];
double average;
int count =0;
while(infile>>lastname){
outfile<<lastname<<\" \";
infile>>firstname;
outfile<<firstname<<\" \";
int sum =0;
for(int i=0;i<10;i++){
infile>>grade[i];
sum+=grade[i];
outfile<<grade[i]<<\" \";
}
average = sum/10.0;
outfile<<average<<endl;
}
return 0;
}
.

Write a C++ program to compute numeric grades for a course. The course records are in a file that will serve as the input file. The input file is in exactly the fol- lowing format: each line contains a student’s last name, then one space, the the student’s first name, then one space, then ten quiz scores all on one line The quiz scores are whole numbers and are separated by one space. Your program will take its input from this file and send its output to a second file. The data in the output file will be the same as the data in the input file except that there will be one additional number (of type double) at the end of each line. This number will be the average of the student’s ten quiz scores. If this is being done as a class assignment, obtain the file names from your instructor. use at least one function that has file streams as all or some of its arguments.
use the file \"6_02_in\" in the general section of Moodle. The file could include more data with the same format. Use 6_02_out for output file name.
Solution
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
ifstream infile;
ofstream outfile;
string filename;
cout<<\"Enter input filename\";
cin>>filename;
infile.open(filename.c_str());
cout<<\"Enter output filename\";
cin>>filename;
outfile.open(filename.c_str());
string firstname,lastname;
int grade[10];
double average;
int count =0;
while(infile>>lastname){
outfile<<lastname<<\" \";
infile>>firstname;
outfile<<firstname<<\" \";
int sum =0;
for(int i=0;i<10;i++){
infile>>grade[i];
sum+=grade[i];
outfile<<grade[i]<<\" \";
}
average = sum/10.0;
outfile<<average<<endl;
}
return 0;
}
.

Anzeige
Anzeige

Weitere Verwandte Inhalte

Ähnlich wie Write a C++ program to compute numeric grades for a course- The course.docx (20)

Weitere von lez31palka (20)

Anzeige

Aktuellste (20)

Write a C++ program to compute numeric grades for a course- The course.docx

  1. 1. Write a C++ program to compute numeric grades for a course. The course records are in a file that will serve as the input file. The input file is in exactly the fol- lowing format: each line contains a student’s last name, then one space, the the student’s first name, then one space, then ten quiz scores all on one line The quiz scores are whole numbers and are separated by one space. Your program will take its input from this file and send its output to a second file. The data in the output file will be the same as the data in the input file except that there will be one additional number (of type double) at the end of each line. This number will be the average of the student’s ten quiz scores. If this is being done as a class assignment, obtain the file names from your instructor. use at least one function that has file streams as all or some of its arguments. use the file "6_02_in" in the general section of Moodle. The file could include more data with the same format. Use 6_02_out for output file name. Solution #include <iostream> #include <fstream> #include <string> using namespace std; int main(){ ifstream infile; ofstream outfile; string filename; cout<<"Enter input filename"; cin>>filename; infile.open(filename.c_str()); cout<<"Enter output filename"; cin>>filename; outfile.open(filename.c_str()); string firstname,lastname; int grade[10]; double average; int count =0; while(infile>>lastname){
  2. 2. outfile<<lastname<<" "; infile>>firstname; outfile<<firstname<<" "; int sum =0; for(int i=0;i<10;i++){ infile>>grade[i]; sum+=grade[i]; outfile<<grade[i]<<" "; } average = sum/10.0; outfile<<average<<endl; } return 0; }

×