Write a C++ program that reads in two partially filled arrays. One array contain
ID: 3690112 • Letter: W
Question
Write a C++ program that reads in two partially filled arrays. One array contains student IDs (with int type) and the other contains GPAs (with double type). The program will sort the two arrays according to the student ID and print out the results.
Here is an example:
Input: (notice that student ID are not sorted)
Student ID
GPA
10123
3
50123
3.2
30321
3.4
40432
4
20222
1.8
30001
2.7
Output: (notice that both student ID and GPA are sorted according to Student ID)
Student ID
GPA
10123
3
20222
1.8
30001
2.7
30321
3.4
40432
4
50123
3.2
Student ID
GPA
10123
3
50123
3.2
30321
3.4
40432
4
20222
1.8
30001
2.7
Explanation / Answer
#include<iostream.h>
void main()
{
int * student_id=[10123,50123,30321,40432,20222,30001];
double * cgpa=[3,3.2,3.4,4,1.8,2.7];
for(int i=0;i<6;i++)
{
for(int j=i;j<6;j++)
{
if(student_id[i]>student_id[j])
{
int temp=student_id[i];
student_id[i]=student_id[j];
student_id[j]=temp;
double temp1;
temp1=cgpa[i];
cgpa[i]=cgpa[j];
cgpa[j]=temp1;
}
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.