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;
}

Recommended Answers

All 2 Replies

Glances over first example
There needs to be a list of variables name you don't use when asking for help in an English speaking forum.

#include<iostream>
#include<string>
#define bilp 5 //Total number of students
//#define saiznama 20 //student name length //Nope. Gonna use strings.
using namespace std;
int main()
{
    //initialization

    int mark[bilp],i,j,k,semen;
    //char nama[bilp][saiznama];
    //char mama[1][saiznama];
    string nama[bilp];
    string mama;
    //input data
    for(i=0; i<bilp; i++)
    {
        cout << "Masukkan Nama[" << i+1 << "] dan juga markahnya : ";
        cin >> nama[i] >> mark[i];
    }
    // (bubble) sorting
    for(i=0; i<bilp-1; i++)
    {
        for(j=i+1; j<bilp; j++)
        {
            if (mark[i] < mark[j]) //TO SORT BY NAMES, THIS LINE MUST BE CHANGED TO COMPARE NAME DATA.
                     {
                     //Swap the grades
                     semen= mark[i];
                     mark[i]= mark[j];
                     mark[j]= semen;

                     //Swap the Names
                     //for(k=0;(mama[0][k]= nama[i][k]) != '\0';k++);
                     mama=nama[k];
                     //for(k=0;(nama[i][k]= nama[j][k]) != '\0';k++);
                     nama[i]=nama[j];
                     //for(k=0;(nama[j][k]= mama[0][k]) != '\0';k++);
                     nama[j]=mama;
                     }
        }
    }


      // 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;
}

OK, so simply by changing the first example to use strings, I all but did your homework for you. You need to change 1 line.
However, this is the C w/classes paradigm. This isn't really C++.
EDIT:
Here's a few examples of what I mean:
http://www.cplusplus.com/reference/algorithm/sort/
http://www.cplusplus.com/articles/NhA0RXSz/ (Look for "Sorting User Made Types.")

There are multiply implementations for what we call strings. One version is C style strings, also called nulled terminated char arrays. You can't compare or display all elements of an array at once using the == or the << operators, with one expcept. Because of the terminating null char, you can display C style strings with the << operator. But to compare them or copy one to another, etc, you need to use a function---either one you write yourself or a standard one like strcmp() or strcpy(), many of of which are found in the header file called cstring.

Objects of the std::string class can be compared with ==, < , >, or assigned (copied) with =, and displayed with <<, because that's how the class is implemented.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.