sorting arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • zfareed@mail.com

    #1

    sorting arrays

    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(Lstn ame,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][i] == 0)
    cout << "00.0" << " ";
    else
    cout << fixed << setprecision(1) << scores[j][i] << "
    ";


    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[i], 10 );
    memcpy ( Lstname[i], Lstname[min], 10 );
    memcpy ( Lstname[min], save, 10 );

    }

    }

    Any ideas?

  • Daniel T.

    #2
    Re: sorting arrays

    In article <1156867705.555 976.295520@p79g 2000cwp.googleg roups.com>,
    zfareed@mail.co m wrote:
    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.
    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.

    Comment

    • Ron Natalie

      #3
      Re: sorting arrays

      zfareed@mail.co m wrote:
      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:
      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.
      k=j;
      for(j=0;j<k;j++ )
      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.
      {
      >
      cout << Lstname[j] << Fstname [j] << " " << Id[j] << " ";
      OK, now where did Firstname and ID come from aall of a sudden.
      >
      for(i=0;i<8;i++ )
      if(scores[j][i] == 0)
      As for scores.
      cout << "00.0" << " ";
      else
      cout << fixed << setprecision(1) << scores[j][i] << "
      This is silly. If you set the field size and precision, you could just
      put out scores[j][i] 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?
      >
      void bubbleSort (char Lstname[][10], int j)

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

      int main() {
      std::vector<Stu dentstudents;
      while(true) {
      Student s;
      if(!s.ReadStude nt()) break;
      students.push_b ack(s);
      }
      std::sort(s.beg in(), s.end());
      Student::PrintH eader();
      for(std::vector <Student>::iter ator it = students.begin( ): it !=
      students.end(); ++it)
      it->PrintStudent() ;
      }


      Comment

      Working...