Develop a C++ program that will ask the user to enter 5 student names and grades
ID: 670868 • Letter: D
Question
Develop a C++ program that will ask the user to enter 5 student names and grades. Once the user has entered the names and grades the program must ask the user how the information should be sorted before displaying it. The options are sorted by (N)ame or sorted by (G)rade. If the user selects sorted by name, the names and grades are displayed sorted alphabetically by student name. If the user selects sorted by grade, the names and grades are displayed sorted numerically by grade. Each student’s name and grade should be stored in a structure element of an array of structures.
The structure should have two members: a name which will allow for up to a 40 character student name and a grade which is a float value. Define this structure.
In order to sort the names/grades, the program will call a function called sort. The sort function will take three parameters: an array of structures, the number of elements, and a boolean parameter that indicates whether the values should be sorted by name (false) or grade (true). This third parameter should be a default argument with a default value of false. This makes sorting by name the default.
The sort function should use a bubble sort to sort the values. Depending on which value is being used to sort (name or grade), the bubble sort routine will have to determine if the values should be swapped. Use two overloaded functions named doSwap that return bool. The first one takes two char[] parameters and returns true if the structure array elements should be swapped based on name (otherwise false is returned). The second one takes two float parameters and returns true if the structure elements should be swapped based on grade (otherwise false is returned). Make sure your bubble sort routine calls these overloaded functions to determine if a swap of the structure array elements should take place.
Your main() should define an array of 5 structure elements. Use a const variable to define the 5. This allows the number of student names and grades to be easily changed (note that the const must be used throughout the program). The main will prompt the user for the student names and grades, prompt the user for the sort option (by name or by grade), and then call the sort function. If sorting by name, only pass two parameters to the sort function (the array and the number of elements). The default argument will then be used for the third parameter. If sorting by grade, pass three elements (the array, the number of elements, and true for the third parameter to indicates sorting by grade).
Explanation / Answer
// ConsoleApplication7.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include <cstdlib>
#include<string>
#include<iomanip>
#include <fstream>
using namespace std;
struct student
{
char name[40];
float grade;
};
bool doSwap(char c[],char c1[]){
bool k=false;
int result=strcmp(c,c1);
cout<<result;
if(result==1){
k=true;
}if(result==-1){
k=false;
}
return k;
}
bool doSwap(float f1,float f2){
return f1>f2;
}
void Print(student s[],int n){
cout<<"name grade"<<endl;
cout<<"------------------------------------"<<endl;
for(int i=0;i<n;i++){
cout<<s[i].name<<" "<<s[i].grade<<endl;
}
}
void sort(student s[],int n,bool sor){
int i=0,j=0;
if(sor==false){
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++){
if(doSwap(s[j].name,s[j+1].name))
{
char temp[40];
strcpy_s(temp,40,s[j].name);
strcpy_s(s[j].name,40,s[j+1].name);
strcpy_s(s[j+1].name,40,temp);
float temp1;
temp1=s[j].grade;
s[j].grade=s[j+1].grade;
s[j+1].grade=temp1;
}
}
}
}else if(sor==true){
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++){
if(doSwap(s[j].grade,s[j+1].grade))
{
float temp;
temp=s[j].grade;
s[j].grade=s[j+1].grade;
s[j+1].grade=temp;
char temp1[40];
strcpy_s(temp1,40,s[j].name);
strcpy_s(s[j].name,40,s[j+1].name);
strcpy_s(s[j+1].name,40,temp1);
}
}
}
}
Print(s,n);
}
int _tmain(int argc, _TCHAR* argv[])
{
student s[5];
char d;
const int n=5;
bool sor=false;
for(int i=0;i<n;i++){
cout<<"enter student name: ";
cin>>s[i].name;
cout<<"enter student grade: ";
cin>>s[i].grade;
}
cout<<"enter how to sort the students 'N'for name or 'G' for grade";
cin>>d;
if(d=='N'){
sort(s,n,sor);
}else if(d=='G'){
sor=true;
sort(s,n,sor);
}else{
sort(s,n,sor);
}
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.