this is the question given by my lecturer :-
Write a program to accept 5 students (names) and their respective test marks into an array or arrays. Sort them and print the lists in:
a) alphabetical order
b) mark ranks(from highest to lowest)
and my input is
enter name : jay
enter marks : 14
enter name : bay
enter marks :10
enter name : hay
enter marks : 13
enter name : ray
enter marks : 11
enter name : say
enter marks : 12
#include <iostream>
using namespace std;
void selectionSort(string [],int[], int);
void showArray(string [],int[], int);
void entah(string [],int[],int);
int main()
const int SIZE = 5;
int num[5];
string name[5];
for(int n=0;n<5;n++)
{
cout<<"enter name:";
cin>>name[n];
cout<<endl;
cout<<"enter the marks:";
cin>>num[n];
cout<<endl;
}
selectionSort(name,num, SIZE);
cout << "\n\tThe alphabetically sorted string is: \n";
showArray(name,num, SIZE);
cout << "\n\tThe mark sorted list is: \n";
entah(name,num,SIZE);
return 0;
}
void selectionSort(string name[],int num[], int elems)
{
int startScan, minIndex;
string strName;
int strNum;
for(startScan = 0; startScan < (elems - 1); startScan++)
{
minIndex = startScan;
strName = name[startScan];
strNum = num[startScan];
for(int index = startScan + 1; index < elems; index++)
{
if(name[index] < strName)
{
strName = name[index];
strNum = num[index];
minIndex = index;
}
}
name[minIndex] = name[startScan];
num[minIndex] = num[startScan];
name[startScan] = strName;
num[startScan] = strNum;
}
}
void showArray(string name[],int num[], int elems)
{
for(int i = 0; i < elems; i++)
{
cout << i << ": " << name[i] << "\t" << num[i] << endl;
}
}
void entah(string name[],int num[],int elems)
{
for(int i = 0; i < 5; i++)
{
for(int j = i + 1; j < 6; j++)
{
if(num[i] < num[j])
{
int temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
for(int k =0; k < 5 ; k++)
{
cout << k << ": " << num[k] << "\t" << name[k] << endl;
}
}
after build and run,
the result i get is
alphabetically
0:bay 10
1:hay 13
2:jay 14
3:ray 11
4:say 12
sorted by marks
0:14 bay
1:13 hay
2:12 jay
3:11 ray
4:10 say
what i suppose to get in the results are these:
alphabetically
0:bay 10
1:hay 13
2:jay 14
3:ray 11
4:say 12
sorted by marks
0:14 jay
0:13 hay
0:12 say
0:11 ray
0:10 bbay
help me please..