Nov 20, 2017 at 1:27am UTC
This program allows the user to enter a name followed by a grade. It stores them in separate arrays then sorts them side by side. It should display in ascending order. It only manages to do it sometimes.. anyone know why?
// Example program
#include <iostream>
#include <string>
using namespace std;
void getData(int *,string *,int);
void sorting(int *,string *,int);
void average(int *,string *,int);
void display(int *,string *,int);
int main()
{
int size;
cout << "how many students?: ";
cin >> size;
int grades[size];
string student[size];
getData(grades,student,size);
sorting(grades,student,size);
display(grades,student,size);
}
void getData(int *grades, string *students, int size)
{
for(int count = 0; count < size; count ++)
{
cout << "Enter the students nane: ";
cin >> *(students + count);
cout << "Enter the students grade: ";
cin >> *(grades + count);
//validate grade
while(*(grades + count) < 0 || *(grades + count) > 100)
{
cout << "Enter a valid grade/n";
cin >> *(grades + count);
}
}
}
void display(int *grades, string *students, int size)
{
for(int count = 0; count < size; count ++)
{
cout << *(students + count) << " " << *(grades + count) << " \n";
}
}
void sorting(int *grades, string *students, int size)
{
int startScan, minIndex, minValue;
for(startScan = 0; startScan < (size -1); startScan++)
{
minIndex = startScan;
for( minValue = startScan + 1; minValue < size; minValue++)
{
if(*grades+minValue<*(grades+minValue))
{
minIndex = minValue;
}
int temp = *(grades + minIndex);
*(grades + minIndex) = *(grades + startScan);
*(grades + startScan) = temp;
string temp1 = *(students + minIndex);
*(students + minIndex) = *(students + startScan);
*(students + startScan) = temp1;
}
}
}