Using C++, write a program to read the 3 exams’ scores of 5 students from the gi
ID: 3712615 • Letter: U
Question
Using C++, write a program to read the 3 exams’ scores of 5 students from the given text file, ExamScore.txt, and store them into each exam’s array. Then, sort them by ascending order and display the sorted array one by one. To sort the array, you create sortArray function that gets one array as an input and return the sorted array. So that, your main method call sortArray with each exam array one by one to get the sorted arrays.
This is the text file:
Below is what I have so far, but we have to add arrays so that the scores of all of them are sorted in ascending order.
#include<iostream>
#include<fstream>
using namespace std;
//function to sort the values
void sortArray(int arr[], int N)
{
int temp;
int i, j;
//nested for loop
for(i = 0; i < N; ++i)
{
for (j = i + 1; j < N; ++j)
{
//if current element is less than than all other elemnts
if (arr[i] > arr[j])
{
//swap the elements
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
//start of main function
int main()
{
double s1, s2, s3;
//variables and arrays declrations
int score[3];
//creating a filestream object
ifstream infile;
//open file to read
infile.open("/Users/kayla/Desktop/ExamScore.txt");
//if file exist or not
if(!infile)
{
cout<<"File does not exist."<<endl;
}else{
cout<<"Student's score in sorted order are: "<<endl;
//open file to read the data
for(int i = 0; i<5; i++){
infile >>s1>>s2>>s3;
//score[] = score;
cout<<s1<<endl;
cout<<s2<<endl;
cout<<s3<<endl;
cout<<endl;
}
//inputFile.close();
/*while(infile>>s1>>s2>>s3)
{
score[0] = s1;
score[1] = s2;
score[2] = s3;
sortArray(score, 3);
for(int i = 0; i<3; i++)
{
cout<<score[i]<<" ";
}
cout<<endl;
}*/
infile.close();
}
return 0;
}
Explanation / Answer
Please try the below code..
===============================================================
#include<iostream>
#include<fstream>
using namespace std;
//function to sort the values
void sortArray(int arr[], int N)
{
int temp;
int i, j;
//nested for loop
for(i = 0; i < N; ++i)
{
for (j = i + 1; j < N; ++j)
{
//if current element is less than than all other elemnts
if (arr[i] > arr[j])
{
//swap the elements
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
//start of main function
int main()
{
double s1, s2, s3;
//variables and arrays declrations
int score[3];
//creating a filestream object
ifstream inputfile;
//open file to read
inputfile.open("/Users/kayla/Desktop/ExamScore.txt");
//if file exist or not
if(!inputfile)
{
cout<<"File does not exist."<<endl;
}
else
{
cout<<"Student's score in sorted order are: "<<endl;
//open file to read the data
while(inputfile>>s1>>s2>>s3)
{
score[0] = s1;
score[1] = s2;
score[2] = s3;
sortArray(score, 3);
for(int i = 0; i<3; i++)
{
cout<<score[i]<<" ";
}
cout<<endl;
}
inputfile.close();
}
return 0;
================================================================
Kindly Check and Verify Thanks..!!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.