sorting arrays

Discussion in 'C++' started by zfareed, Aug 29, 2006.

  1. zfareed

    zfareed Guest

    I managed to sort a 2-dim array of lastnames but I am having trouble
    with printing the corresponding first names which are stored in a
    different 2-dim array. Here is my code for:

    bubbleSort(Lstname,j);
    // Printing array
    cout << "STUDENT " << setw(8) << "ID " << setw(20)<< "S1 S2
    S3 S4"
    << " S5 S6 S7 S8 AVG"<< endl;
    cout <<
    "--------------------------------------------------------------"
    << "-----------------"<<endl;
    k=j;
    for(j=0;j<k;j++)
    {

    cout << Lstname[j] << Fstname [j] << " " << Id[j] << " ";

    for(i=0;i<8;i++)
    if(scores[j] == 0)
    cout << "00.0" << " ";
    else
    cout << fixed << setprecision(1) << scores[j] << "
    ";


    cout << average[j];
    cout << endl;

    }



    void bubbleSort (char Lstname[][10], int j)
    {

    int n;
    char save[10];
    int min;

    for(n=0; n < j;n++)
    cout << Lstname[n] << endl;
    cout << endl;
    cout << "Sorted list" << endl;

    //Sort
    for (int i = 0; i < j-1 ; i++)
    {
    min = i;
    for (int t = i + 1; t < j; t++)
    {
    if (strcmp (Lstname[t],Lstname[min]) < 0 )
    min = t;
    }


    memcpy ( save, Lstname, 10 );
    memcpy ( Lstname, Lstname[min], 10 );
    memcpy ( Lstname[min], save, 10 );

    }

    }

    Any ideas?
     
    zfareed, Aug 29, 2006
    #1
    1. Advertisements

  2. zfareed

    Daniel T. Guest

    You have two choice depending on what your teacher has already covered.
    (a) use a struct or class.

    struct Person {
    char firstName[10];
    char lastName[10];
    };

    Then have a single array of Person objects and sort the array, or if
    your teacher hasn't covered structs or classes yet, swap the first names
    at the same time you swap the last names.
     
    Daniel T., Aug 29, 2006
    #2
    1. Advertisements

  3. zfareed

    Ron Natalie Guest

    You don't show us the declaration of Lstname. I see you still haven't
    taken our advice to use the real string type rather than trying to
    deal with arrays of characters. If your names are 10 characters or
    longer you're going to write off the end of the array you give.

    Anyhow, the other thing you've not told us is what you saw versus
    what you were expecting.
    Single letter variable names make things hard to understand. Further
    it's not clear why you are apparently changing the meaning of k from
    "the number of names" to an index at this points. Integer variables
    are cheap and so are letters in variable names.
    OK, now where did Firstname and ID come from aall of a sudden.


    This is silly. If you set the field size and precision, you could just
    put out scores[j] even if it had zero.

    Further, the fact that you seem to have about four arrays here that are
    all dimensioned by k, kind of begs that a class (or struct) should
    have been used to keep all these together. If you sort the last names
    don't you want all the other information to travel with it?

    By the way, unless the goal here is to learn how to write
    inefficient sorting algorithms, why not use std::sort?


    OK...I'll give you a hint how a real C++ programmer might write
    things. I'll leave out some things so you can still make a
    contribution to this program.

    class Student {
    public:
    bool ReadStudent(); // read info from stream
    // return false on end of input
    static void PrintHeader(); // print column headings.
    void PrintStudent(); // Output student

    bool operator<(const Student& other) {
    return LastName < other.LastName;
    }
    private:
    std::string FirstName;
    std::string LastName;
    ind ID;
    std::vector<double> Scores;
    };

    int main() {
    std::vector<Student> students;
    while(true) {
    Student s;
    if(!s.ReadStudent()) break;
    students.push_back(s);
    }
    std::sort(s.begin(), s.end());
    Student::printHeader();
    for(std::vector<Student>::iterator it = students.begin(): it !=
    students.end(); ++it)
    it->PrintStudent();
    }
     
    Ron Natalie, Aug 29, 2006
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.