sorting an array

This is a discussion on sorting an array within the C Programming forums, part of the General Programming Boards category; hey guys !! i have to search an array of struct students on the basis of their first names, last ...

  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>
    Code:
    #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
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    This is C++. This a C thread. The C++ thread is above the C thread. If you do not care using your algorithm, you can always use the std::sort.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,565
    This is a C forum, your code is C++.

    Also, you claim you need to search an array, yet your function is called SortArray and your post title says "sort", so which is it: search or sort?
    I also recommend you read some Wikipedia articles on searching and sorting. Both have very thorough descriptions and pseudo code to help you.
    The most basic search: Linear search - Wikipedia, the free encyclopedia
    One of the most basic sorts: Bubble sort - Wikipedia, the free encyclopedia

    *** Mods, please move.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,571
    *** Mods, please move.
    FYI, this is a double post and already exists in that forum.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    333
    Look up the qsort() function.

    It's rather hard to use, because it takes a function pointer.

    So you might like to code your own, bubblesort.

    Step through the array from 0 to N-2. If the record index i and i + 1 are inthe wrong order, swap them, and set a flag to true.
    If the flag was never set, your array is sorted. Otherwise, pass through the array again, doing the same thing, until the flag remains false.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Malcolm McLean View Post
    Look up the qsort() function.
    A C++ code using qsort... Not good.... As I said, if you want to use something ready, then std::sort is the solution for C++.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting an array
    By waqas94 in forum C++ Programming
    Replies: 1
    Last Post: 02-28-2013, 03:19 PM
  2. sorting array
    By bJudy in forum C Programming
    Replies: 9
    Last Post: 11-09-2010, 10:28 AM
  3. 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
  4. Replies: 9
    Last Post: 04-07-2010, 10:03 PM
  5. Sorting Array
    By eun-jin in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2006, 03:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21