Thread: sorting an array

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    27

    sorting an array

    hey guys !!

    i have to search an array of struct students on the basis of their first names, last names and roll numbers ..
    so far i am done with the input of the student first , last names and roll numbers . i have problem in searching the array .. my code is :
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    struct Student
    {
        string FirstName ;
        string LastName ;
        int RollNo ;
        int semester ;
        
    };
    void inputFromUser (Student S[] );
    void SortArray (Student S[]);
    void showStudents (Student S[]);
    int main ()
    {
        Student *S = new Student [3] ;
        
        inputFromUser(S);
        
        showStudents(S);
        SortArray(S);
        system("pause");
        return 0 ;
    }
    void inputFromUser  (Student S[])
    {
        
        for ( int i = 0 ; i<3 ; i++)
        {
            
            cout << " enter the first name " << endl;
            cin >> S[i].FirstName ;        
            cout << " enter last name " << endl;
            cin >> S[i].LastName ;
            cout << " enter roll number " << endl;
            cin >> S[i].RollNo ;
            
        }
        
        
    }
    void showStudents (Student S[])
    {
        cout << " the names entered are " << endl;
        for ( int i = 0 ; i<3 ; i++ )
        {
            cout << S[i].RollNo << "" << S[i].FirstName << " " << S[i].LastName << endl;
        }
        
    }
    void SortArray (Student S[] )
    {
        
        for (int i = 0 ; i<3 ; i++ )
        {
            
        }
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I suggest you don't use new. It opens up a can of worms. Use std::vector for arrays that does not have a fixed length; it's size() member can be used to find the size in subsequent functions instead of your hard coded size. Use std::array if need a fixed length array. Again, its size() member can tell you its size.

    Now, as for sorting... you need to think of an actual algorithm for it first. There are a couple of popular easy ones. Bubble sort and induction sort are two.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting array
    By bJudy in forum C Programming
    Replies: 9
    Last Post: 11-09-2010, 11:28 AM
  2. Adding nodes to an array at the top & array sorting
    By Wolf` in forum C++ Programming
    Replies: 1
    Last Post: 06-25-2010, 12:48 PM
  3. Replies: 9
    Last Post: 04-07-2010, 10:03 PM
  4. Sorting an array
    By cashmerelc in forum C Programming
    Replies: 2
    Last Post: 12-05-2007, 01:57 AM
  5. Sorting an array
    By FastFish in forum C Programming
    Replies: 15
    Last Post: 01-28-2002, 05:47 PM