Hasha 2 Posted March 28, 2002 Guys, please help this poor Grade 12 Computer science kid here who is struglling with the Take-Home Test. Thank YOU THANK YOU! PLEASE HELP ME!!! Please give me the full SOLUTION! FULL SOLUTION! I AM REALLY DESPERATE AND DEAD!! PLEASE HELP!! This Questions Requires the use of 2D arrays in microsoft Visual C++ Write a program that obtains 5 students first names from the user. The program should display on the screen the name of the student who would be first and last when sorted in true alphabetical order. By that I mean that E and e are the same. This means that you will have to somehow use the ASCII Character table to your advantage. Eg. the user enter DinO Robert anne jUliA PEtEr The output would be: The first name in the list is anne The last name in the list is robert Share this post Link to post Share on other sites
Ex-RoNiN 0 Posted March 28, 2002 I can't give u a complete answer (as its almost 2am), but I reckon part of the solution is the very fact that ALL characters have an equivalent number value (eg G=42 say). Share this post Link to post Share on other sites
Hasha 2 Posted March 28, 2002 CAN SOmeone please help me!! It is due TOMORROW!! HELP ME GUYS! Share this post Link to post Share on other sites
Placebo 29 Posted March 28, 2002 It's better to hand it in late than to commit plagiarism, which would be the case if someone gave you the answers, don't know about your school but my college frown very heavily on plagiarism Share this post Link to post Share on other sites
Hasha 2 Posted March 28, 2002 I AM allowed to ask for help !!! IT IS OPEN BOOK AND TAKE home test, I can ask anyone for answer. There is no late! If I cannot get it in by tomolo, it is ZERO!!! PLEASE HELP ME!!! Share this post Link to post Share on other sites
DodgeME 0 Posted March 28, 2002 That sounds easy for pascal or vb but l haven't done C++ Since all languages use the same logic but other code is input to have the same resault. Make a database at first. Input the values for all letters:A=1,B=2 and so on. Now make a read command for the user to enter 5 names. Now u can make a loop command to check each letter that starts. Now A=1 as we have said. The loop should check which letter the name starts from. Note that u should enter the output command inside the loop and u might want to add a counter too for the loop to stop when is done Is quite simple to make in other languages but in C++ it would be harder. Just tell yer mate to sit down and think the logic of the program. If he dones that and he knows the commands it would be very easy to make. Go here since there are many pros here: http://www.madonion.com Go in the gamershq and post in the off-topic. Some gurus there might even giveb u the entire code for it lol. Share this post Link to post Share on other sites
Guest Posted March 28, 2002 Ansi C quicksort is the key. (From the excellent Digital Unix man pages): NAME qsort - Sorts a table in place LIBRARY Standard C Library (libc.so, libc.a) SYNOPSIS #include <stdlib.h> void qsort( void *base, size_t nmemb, size_t size, int (*compar) (const void *, const void *)); --------------- It's really smart. base is the address of the first element in the array. nmemb is the number of entries in the table. size is the size of byte. compar is a function which compares two elements of the array. So you read in your names using cin or whatever. You store the names as char *s. You create an array of char pointers. You then right a function to compare two names and find out which one should be first in the table. you then do: qsort(names, num_of_names, named, sizeof(int), func); It will then sort it for you unbelievably quickly. If the above didn't make sense, go and read a book on C/++ programming and learn the symantics and features of the language. Share this post Link to post Share on other sites
Mister Frag 0 Posted March 28, 2002 void main(void) { #define MAXNAMES 5 int nNames; char szNames[MAXNAMES][80] = {0}; for (nNames = 0; nNames < MAXNAMES; nNames++) { printf("Please enter name #%d: ", nNames + 1); gets(szNames[nNames]); } qsort(szNames, MAXNAMES, sizeof(szNames[0]), (int (__cdecl *) (const void *, const void *)) stricmp); for (nNames = 0; nNames < MAXNAMES; nNames++) printf("Name #%d: '%s'\n", nNames + 1, szNames[nNames]); } Share this post Link to post Share on other sites
barret 0 Posted April 12, 2002 you guys sure its not $/print A=1 ?? Share this post Link to post Share on other sites
Ex-RoNiN 0 Posted April 12, 2002 </span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (barret @ April 12 2002,20:51)</td></tr><tr><td id="QUOTE">you guys sure its not $/print A=1 ??<span id='postcolor'> that don't look like C or C++ Share this post Link to post Share on other sites
General Lag 1 Posted April 14, 2002 Well, as I tend to do all the stuff myself, here is my solution. Btw, all compiles! This stuff here would go into some kind of header. </span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE"> /////////////////////////// // swaps pointers in a // pointer Array /////////////////////////// template <class dType> void swapPArr( dType** data, const int& index1, const int& index2 ) {  static dType* tmp;  tmp = data[index1];  data[index1] = data[index2];  data[index2] = tmp; } /////////////////////////// // Compares data // Arrays /////////////////////////// template <class dType> bool GreaterEqPArr( dType* v1, dType* v2, const int& dataSize ) {  if( dataSize )  {   if( *v1 < *v2 ) return true;   if( *v1 > *v2 ) return false;   return GreaterEqPArr( v1+1, v2+1, dataSize-1 );  }  return false; } /////////////////////////// // Sorts the pointer // order to data Arrays /////////////////////////// template <class dType> void quicksortPArr( dType** a, const int& l, const int& r, const int& dataSize ) {  int i, j;  dType* v;  if( r>l )  {   v = a[r];   i=l-1;   j=r+1;   for(;; )   {    while( GreaterEqPArr( a[++i], v, dataSize ) );    while( GreaterEqPArr( a[--j], v, dataSize ) );    if( i>=j ) break;    swapPArr( a, i, j );   }   swapPArr( a, i, r );   quicksortPArr(a, l, i-1, dataSize );   quicksortPArr(a, i+1, r, dataSize );  } } <span id='postcolor'> And now we can use these functions </span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE"> void main() {  // Number of entrys  int size;  // counter  int i=0;  // the Pointer array  char** PArr;  // the Data Buffer size for the names  const int buffersize=20;  // Get the  number of names  cout << "Please enter the number of records with \"1\" as first..:";  cin >> size;  PArr = new char*;  // Get the names  for( i=0; i<size; i++ )  {   cout << "Enter record nr.\"" << i+1 << "\"..:";   PArr = new char[buffersize];   cin >> PArr;  }  // sort the pointer to the names  quicksortPArr( PArr, 0, size-1, buffersize );  // Print the new ordered names  for( i=0; i<size; i++ )  {   cout << PArr << endl;  } } <span id='postcolor'> Ok, this may seem quit a bit a lot of code. But what you have here is a template sort algorithm which accepts ANY pointer array. This means that you not only can toss char* at it, but also EVERY other type of data. As long as the data is comparable with > at the level of a cell ( remember, we got 2d table here ). Damn, there's a bug !! Gotta find out. Hang on a day or two. Share this post Link to post Share on other sites
General Lag 1 Posted April 14, 2002 Ah, and before I forget. What you need to understand this. -In depth knowledge about pointers and arrays. -Familiar with Templates -Understanding of a recursive function call. And pay attention, this code is by no means error Save Share this post Link to post Share on other sites
IceFire 0 Posted April 14, 2002 Thank god I don't know a thing about computers!! I would need to know all that crap. Share this post Link to post Share on other sites
General Lag 1 Posted April 14, 2002 </span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (IceFire @ April 14 2002,21:50)</td></tr><tr><td id="QUOTE">Thank god I don't know a thing about computers!! I would need to know all that crap.<span id='postcolor'> Uhm, I don't want to rant BUT. Are you serious? I mean, I can fairly understand why you choose for yourself to not know about this "computer stuff" as it is not everbodys thing. On the other hand, if you'r posting here you may know more then you want. The thing I don't understand is why it should be crap. Without this crap you wouldn't be able to post here, to surf, to play flashpoint, to write electronic mails to everybody on the world and so and so forth. Without this crap empowering ALL the electronic devices around you ( in an averyge car are about 80-200 chips ) you would be pretty in what we call stoneage. Yes, you could live in the wilderness wthout it, but not in the todays society. Share this post Link to post Share on other sites
barret 0 Posted April 15, 2002 </span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (Thorn @ April 14 2002,23:33)</td></tr><tr><td id="QUOTE"></span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (IceFire @ April 14 2002,21:50)</td></tr><tr><td id="QUOTE">Thank god I don't know a thing about computers!! I would need to know all that crap.<span id='postcolor'> Uhm, I don't want to rant BUT. Are you serious? I mean, I can fairly understand why you choose for yourself to not know about this "computer stuff" as it is not everbodys thing. On the other hand, if you'r posting here you may know more then you want. The thing I don't understand is why it should be crap. Without this crap you wouldn't be able to post here, to surf, to play flashpoint, to write electronic mails to everybody on the world and so and so forth. Without this crap empowering ALL the electronic devices around you ( in an averyge car are about 80-200 chips ) you would be pretty in what we call stoneage. Yes, you could live in the wilderness wthout it, but not in the todays society.<span id='postcolor'> I'm learnin C and C++. I know Perl, Php(4), Java, And python. Doesn't that sound great on a 13 year old's resume? Share this post Link to post Share on other sites
General Lag 1 Posted April 15, 2002 </span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (barret @ April 15 2002,10:39)</td></tr><tr><td id="QUOTE"></span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (Thorn @ April 14 2002,23:33)</td></tr><tr><td id="QUOTE"></span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (IceFire @ April 14 2002,21:50)</td></tr><tr><td id="QUOTE">Thank god I don't know a thing about computers!! I would need to know all that crap.<span id='postcolor'> Uhm, I don't want to rant BUT. Are you serious? I mean, I can fairly understand why you choose for yourself to not know about this "computer stuff" as it is not everbodys thing. On the other hand, if you'r posting here you may know more then you want. The thing I don't understand is why it should be crap. Without this crap you wouldn't be able to post here, to surf, to play flashpoint, to write electronic mails to everybody on the world and so and so forth. Without this crap empowering ALL the electronic devices around you ( in an averyge car are about 80-200 chips ) you would be pretty in what we call stoneage. Yes, you could live in the wilderness wthout it, but not in the todays society.<span id='postcolor'> I'm learnin C and C++. I know Perl, Php(4), Java, And python. Doesn't that sound great on a 13 year old's resume?<span id='postcolor'> Sure if you are realy that age. I don't want to doubt it, but it's just unlikely. But, you never know. I am 24, and as such I have begun programming 2 years ago. I don't know anything other pretty good then C++. Sure I can html, and bit of javascript, tcl and Bash, but that's it. I spent most of my time learning C++, because my main course of action is to learn something trough and trough. Amazingly, there is always enough new to learn after you'r done with the syntax. I think you made a good choice to learn C++, because it is a realy versetaile language with many many applikation areas. Share this post Link to post Share on other sites
barret 0 Posted April 19, 2002 </span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (Mister Frag @ Mar. 28 2002,22:34)</td></tr><tr><td id="QUOTE">void main(void) {     #define MAXNAMES   5     int   nNames;     char   szNames[MAXNAMES][80] = {0};     for (nNames = 0; nNames < MAXNAMES; nNames++)       {       printf("Please enter name #%d: ", nNames + 1);       gets(szNames[nNames]);       }     qsort(szNames, MAXNAMES, sizeof(szNames[0]), (int (__cdecl *) (const void *, const void *)) stricmp);     for (nNames = 0; nNames < MAXNAMES; nNames++)       printf("Name #%d: '%s'\n", nNames + 1, szNames[nNames]); }<span id='postcolor'> That sounds visual, it doesn't have the <IOSTREAM> either. So dont post phoney codes Share this post Link to post Share on other sites
Mister Frag 0 Posted April 19, 2002 </span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (barret @ April 20 2002,05:23)</td></tr><tr><td id="QUOTE"><Snip> That sounds visual, it doesn't have the <IOSTREAM> either. So dont post phoney codes<span id='postcolor'> What the heck are you talking about, phoney[sic] codes? The example I provided is probably the simplest and easiest-to-understand implementation which will solve the problem at hand. It should work with both C and C++ compilers, which is why I used #define instead of a const. There is no mention of IOSTREAM.H in my code, but regardless, all C++ compilers should have it, since it is an integral part of the language. If you think the code is phoney because it DOESN'T include IOSTREAMS.H, why would I include the header if none of the functions are used? If you want to argue software engineering with me, go right ahead, but I've been making a living writing commercial software since before you were born, so be prepared for a spanking. Share this post Link to post Share on other sites