sorting... i think

For everyone, just starting with C++ or programming at all. Ask newbie questions in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

sorting... i think

Postby antijock » Tue Oct 07, 2003 8:28 pm

ok, i have a 3 dimensional array. the program gets names and scores and stores them into the array accordingly. after that i want to have the students sorted by score, first or last name. after sorting have it print to the screen. right now it's only taking in 2 students just to make it easier on me. sounding like a assignment yet? well it is, I'm not denying it. lol. I have no clue as to how i would sort the info. any help would be much apprieciated. oh, if there is a more efficient way to do this would anyone plz let me know? thx again. hope i'm being clear...

Code: Select all
#include <iostream>
using namespace std;


int main()
{

   char cSort; //choice of name or score sort
   char cName [2] [2] [15]; //student 1 is in set 1, and 2 in 2
              //first name in row 1, last in row 2
   int iScore [2]; //stores scores

   cout << "STUDENT #1\n";
   cout << "Please enter first name: ";
   cin >> cName [0] [0];
   cout << "Please enter lastname: ";
   cin >> cName [0] [1];
   cout << "Score: ";
   cin >> iScore[0];

   cout << "\n\nSTUDENT #2\n";
   cout << "Please enter first name: ";
   cin >> cName [1] [0];
   cout << "Please enter lastname: ";
   cin >> cName [1] [1];
   cout << "Score: ";
   cin >> iScore [1];

   cout << "\n\nWould u like to...\nSort by [F]irst name, [L]ast name, or [S]core: ";
   cin >> cSort;

/*
   if (cSort == 'f' || 'F')
                        //sort by first name
                        //print info to screen
   else if (cSort == 'l' || 'L')
           //sort by last name
                        //print info to screen
   else
      //sort by score
                   //print info to screen
*/
   return 0;


}
antijock
 
Posts: 6
Joined: Tue Oct 07, 2003 7:43 pm
Location: my computer...

Postby Jimbo » Tue Oct 07, 2003 8:58 pm

if (cSort == 'f' || 'F')
//sort by first name
//print info to screen
else if (cSort == 'l' || 'L')
//sort by last name
//print info to screen
else
//sort by score
//print info to screen


this is the first problem. each of these will evaluate like this:

Code: Select all
   if ( (cSort == 'f') || ('F') )
                        //sort by first name
                        //print info to screen
   else if ( (cSort == 'l') || ('L') )
           //sort by last name
                        //print info to screen
   else
      //sort by score
                   //print info to screen


notice the added parentheses. for what you want, you should use something like:

Code: Select all
   if (cSort == 'f' || cSort == 'F')
                        //sort by first name
                        //print info to screen
   else if (cSort == 'l' || cSort == 'L')
           //sort by last name
                        //print info to screen
   else
      //sort by score
                   //print info to screen

or:
Code: Select all
#include <cctype>
   if (toupper(cSort) == 'F')  // toupper returns value of cSort as a capital letter
                        //sort by first name
                        //print info to screen
   else if (toupper(cSort) == 'L')
           //sort by last name
                        //print info to screen
   else
      //sort by score
                   //print info to screen


----------

to really simplify this, it would be better to implement a class for the students with the names and score as different member data. also using strings (#include <string>) for the names would also vastly improve things; right now names are limited to 14 characters (saving the 15th for '\0').

----------

since you are only dealing w/ two students (right now) sorting isnt too bad.

to sort by score, find which has the higher (or lower) value and save its index in a temporary variable. then output the variables (using your saved index) in the print code.

to sort by name, you'll probably have to come up with a sorting function as things currently stand. i believe, however, that the string class has built-in comparison operators. but if you dont use strings here's an idea of what to do: compare the first letter of each name (first or last depending on user's choice). if they are the same, compare the next and repeat this and what follows as necessary. if the values are different use the temp variable to store the index of which one comes first (or last) and then output similarly to above.

-----------

this was kinda long, but i hope it helped out a bit... if you need more help, just ask again :)
User avatar
Jimbo
 
Posts: 601
Joined: Thu Sep 25, 2003 6:48 pm
Location: Seattle

strings... why didn't i think of that?

Postby antijock » Tue Oct 07, 2003 9:10 pm

ur right, using the string class would be much easier, or so it appears. i'm just learning C++ and on my own. yes i have an instructor, but this guy is just teaching strait out of the book! i stopped comming to class, i ownly show up on lab days and when things are due, lol. i see what u were saying about eh if statement too, i haven't given that any tought, it's more like pseudo code. hah, thanks alot, cheers. oh, actually it would be much easier to write to a file and then sort... i'm so proud of myself :) later
antijock
 
Posts: 6
Joined: Tue Oct 07, 2003 7:43 pm
Location: my computer...

Postby Jimbo » Tue Oct 07, 2003 9:47 pm

also, if you wanted to spruce the if statements up a bit, you could also use a switch statement. and you dont have any way to deal with illegal input either (if they enter, say, x, you still sort by score). for this you could either do another else if or, when using a switch, use a default statement. switches are a good thing to look into... and they make a lot of things much simpler...
User avatar
Jimbo
 
Posts: 601
Joined: Thu Sep 25, 2003 6:48 pm
Location: Seattle


Return to For Beginners

Who is online

Users browsing this forum: No registered users and 1 guest

cron