I want to make a program that can display the name and marks that user put and then the name will be sorted alphabetically. According to these two example i still cant get it. I have go through many examples on the internet but i still dont know how. Someone please at least give me advice or start giving me a clue. Im still learning please help me till i can truly understand. Thanks :)
Example 1
This program will display the name and marks. The name will be sorted from the highest to the lowest marks.
#include<iostream>
#define bilp 5
#define saiznama 20
using namespace std;
int main()
{
//initialization
int mark[bilp],i,j,k,semen;
char nama[bilp][saiznama];
char mama[1][saiznama];
//input data
for(i=0; i<bilp; i++)
{
cout << "Masukkan Nama[" << i+1 << "] dan juga markahnya : ";
cin >> nama[i] >> mark[i];
}
// sorting
for(i=0; i<bilp-1; i++)
{
for(j=i+1; j<bilp; j++)
{
if (mark[i] < mark[j])
{
semen= mark[i];
mark[i]= mark[j];
mark[j]= semen;
for(k=0;(mama[0][k]= nama[i][k]) != '\0';k++);
for(k=0;(nama[i][k]= nama[j][k]) != '\0';k++);
for(k=0;(nama[j][k]= mama[0][k]) != '\0';k++);
}
}
}
// output data
cout << "\n\nNama yang disusun mengikut markah tertinggi adalah:\n\n";
for(i=0; i<bilp; i++)
{
cout << i+1 << " " << nama[i] << " " << mark[i] << "\n";
}
return 0;
}
Example 2
This program will display the name sorted alphabetically.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string arnList[9]={"John", "Dave", "Steve", "Kevin","Andrew","Scott","Colin","Timothy","Zenon"};
int nLength=9;
string nTemp;
int iCv;
int k;
for (iCv = 1; iCv < nLength; ++iCv)
{
//the new value to be inserted into a temporary location
nTemp = arnList[iCv];
// k is the index of the number to the left of the iCv.
for (k = iCv-1; k >= 0 && arnList[k] > nTemp; k--)
{
arnList[k+1] = arnList[k];
}
arnList[k+1] = nTemp;
}
for(iCv=0;iCv<nLength;iCv++) cout<<arnList[iCv]<<" ";
cout<<endl;
return 0;
}