Question

Write a program in C++ to: 1. Read all records from a binary file (also attached...

Write a program in C++ to: 1. Read all records from a binary file (also attached to this email) to arrays; (the record structure: student number (20 bytes), grade (integer)) 2. Sort the list according to test scores; 3. Calculate the average test score for the class and print on the screen; 4. write sorted records to a new binary file. For example:

The records in the original file:

1 89

2 95

3 76

The new file sorted by scores:

3 76

1 89

2 95

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find below the required code for above problem statement

#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
using namespace std;
//declare the struct of record
struct record{
   int stu_no;
   int stu_grade;
};

int main()
{
   //declared array of struct
   record array[100];
   //declared input and outputfile
   char input[30]="input.txt";
   char output[30]="output.txt";
   int count=0;
   string line;
   ifstream fin;
   ofstream fout;
   //calculating the grade while reading the data
   float gradesum=0;
   //opening the file for reading
   fin.open(input,ifstream::in);
   while(getline(fin,line))
   {
   //   cout<<line<<endl;
       int i=0;
       string s;
       int id,grade;
       stringstream ss(line);
       //using space delimeter breaking the id and grade
       while(getline(ss,s,' '))
       {
           if(i==0)
           {
               id=stoi(s);
              
           }
           else{
               grade=stoi(s);
               gradesum=gradesum+stof(s);
           }
           i++;
       }
   //storing the grade into record strucutre
       record r;
           r.stu_no=id;
           r.stu_grade=grade;
  
           array[count]=r;
           count++;
   }
//sorting the data based on grade
   for(int k=0;k<count-1;k++)
   {
      
       for(int l=k+1;l<count;l++)
       {
      
           if(array[l].stu_grade<array[k].stu_grade)
           {
          
               record temp=array[k];
               array[k]=array[l];
               array[l]=temp;
           }
       }
   }
   //printing average grade on console
   cout<<"Average of grade : "<<gradesum/count<<endl;
   fout.open(output);
   //writting the sorted to file
   for(int k=0;k<count;k++)
   {
      
       fout<<array[k].stu_no<<" "<<array[k].stu_grade<<endl;
   }
   return 0;
}//end of code

CONSOLE OUTPUT

CARakeshlttlc_cppCodelscore.exe verage of grade: 86.6667 Process exited after 0.4214 seconds with return value 0 Press any ke

output.txt

output.txt Notepad File Edit Format View Help 76 1 89 2 95

please do let me know if u have doubts related to solution

Add a comment
Know the answer?
Add Answer to:
Write a program in C++ to: 1. Read all records from a binary file (also attached...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ program to store and update students' academic records in a binary search tree....

    Write a C++ program to store and update students' academic records in a binary search tree. Each record (node) in the binary search tree should contain the following information fields:               1) Student name - the key field (string);               2) Credits attempted (integer);               3) Credits earned (integer);               4) Grade point average - GPA (real).                             All student information is to be read from a text file. Each record in the file represents the grade information on...

  • codeblock c++ language Write a program that reads students test scores in the range 0-200 from...

    codeblock c++ language Write a program that reads students test scores in the range 0-200 from a file until end of file (use e of() to check if ifstream reaches the End of File Stream) and store those scores in an array. It should then determine the number of students having scores in each of the following ranges: 0-24, 25-49, 50-74, 75-99, 100- 124, 125–149, 150-174, and 175–200. Finally, the program outputs the total number of students, each score range...

  • Write a modularized, menu-driven program to read a file with unknown number of records.

    ==============C++ or java================Write a modularized, menu-driven program to read a file with unknown number of records.Create a class Records to store the following data: first and last name, GPA , an Id number, and an emailInput file has unknown number of records; one record per line in the following order: first and last names, GPA , an Id number, and emailAll fields in the input file are separated by a tab (‘\t’) or a blank space (up to you)No error...

  • Write a Java program that reads a file until the end of the file is encountered....

    Write a Java program that reads a file until the end of the file is encountered. The file consists of an unknown number of students' last names and their corresponding test scores. The scores should be integer values and be within the range of 0 to 100. The first line of the file is the number of tests taken by each student. You may assume the data on the file is valid. The program should display the student's name, average...

  • Serve the user in two phases: In C++ Phase A: (1) Read all input records one...

    Serve the user in two phases: In C++ Phase A: (1) Read all input records one by one into arrays in main memory. (2) Display all the records on the screen in the order they are read from the input data file. (3) Do a Selection Sort (must be implemented as a function) to sort the records in ascending order on STUDENT_ID and display all the full records correctly in the correct order after sorting. Phase B: (1) Then, the...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • Write a complete C++ program that reads students names and their test scores from an input...

    Write a complete C++ program that reads students names and their test scores from an input text file. The program should output each student’s name followed by the test scores and the relevant grade in an output text file. It should also find and display on screen the highest/lowest test score and the name of the students having the highest/lowest test score, average and variance of all test scores. Student data obtained from the input text file should be stored...

  • Write a C++ program to keep records and perform statistical analysis for a class of 20...

    Write a C++ program to keep records and perform statistical analysis for a class of 20 students. The information of each student contains ID, Name, Sex, quizzes Scores (2 quizzes per semester), mid-term score, final score, and total score. The program will prompt the user to choose the operation of records from a menu as shown below: ==============================================                                            MENU =============================================== 1. Add student records 2. Delete student records 3. Update student records 4. View all student records 5. Calculate...

  • In C++, write a complete program that receives a series of student records from the keyboard...

    In C++, write a complete program that receives a series of student records from the keyboard and stores them in three parallel arrays named studentID and courseNumber and grade. All arrays are to be 100 elements. The studentID array is to be of type int and the courseNumber and grade arrays are to be of type string. The program should prompt for the number of records to be entered and then receive user input on how many records are to...

  • Write a C++ program that asks user number of students in a class and their names....

    Write a C++ program that asks user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’ names and ranking. Follow the Steps Below Save the project as A4_StudentRanking_yourname....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT