Sorting 2d Array

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2008
Posts: 7
Reputation: -genESIS- is an unknown quantity at this point 
Solved Threads: 0
-genESIS- -genESIS- is offline Offline
Newbie Poster

Sorting 2d Array

 
0
  #1
Mar 1st, 2008
Hi everyone. I'm new to the forums, as well a C programming as well, so bare with me for any noob mistakes. I have a homework assignment I am working on now, and am just completely stumped. The assignment calls for the following

The user is to input 10 words. It then takes those words, changes the lower and upper case around (were not allowed using the toupper() or tolower () functions), as well as spelling those words backwards. Heres a brief example

Enter Word 1: tHe
Enter Word 2: seCOND
Enter Word 3: wOrD
... etc etc up until the 10th word

The output would then be the following:

EhTThE
dnocESSEcond
dRoWWord

Everyone catch my drift. Now from this, I must take the unsorted output, and bubble sort it in ascending order. Here is the code I have come up with. Any help is greatly appreciated

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main(void)
  5. {
  6. char Word[10][19];
  7. int i, j, k, l, z;
  8. int value;
  9. char swap[19];
  10. char ch;
  11.  
  12.  
  13. for(z=0 ; z<10 ; z++)
  14. {
  15. if (z == 0)
  16. printf("Enter the 1st word: ");
  17. else if (z == 1)
  18. printf("Enter the 2nd word: ");
  19. else if (z == 2)
  20. printf("Enter the 3rd word: ");
  21. else
  22. printf("Enter the %dth word: ",z+1);
  23. i=0;
  24. while(( ch = fgetc(stdin)) != '\n')
  25. {
  26. i=i+1;
  27. Word[z][i]= ch;
  28.  
  29. }
  30. l=10;
  31. for(k=i ; k>=1; k--)
  32. {
  33. Word[z][l]=Word[z][k];
  34. l=l++;
  35. }
  36. Word[z][19]=i;
  37. }
  38. printf("\n");
  39. printf("Unsorted Input: \n");
  40. printf("\n");
  41. for (z=0 ; z<10 ; z++)
  42. {
  43. for(j=10 ; j<=9+Word[z][19]; j++)
  44. {
  45. value = Word[z][j];
  46. Word[z][j] = '\0';
  47. if (value >= 97 && value<=122)
  48. { value = value - 32; }
  49. else if (value >=67 && value<=90)
  50. { value = value + 32; }
  51. printf("%c", value);
  52. }
  53. for(j=1; j<=Word[z][19]; j++)
  54. {
  55. value = Word[z][j];
  56. if (value >= 97 && value<=122)
  57. { value = value - 32; }
  58. else if (value >=67 && value<=90)
  59. { value = value + 32; }
  60. printf("%c", value);
  61. }
  62. putchar('\n');
  63. }
  64. printf("\n");
  65. printf("Sorted Input: \n");
  66. printf("\n");
  67.  
  68. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,500
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1479
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Sorting 2d Array

 
0
  #2
Mar 1st, 2008
So I guess all you need is the bubble sort algorithm ?
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 7
Reputation: -genESIS- is an unknown quantity at this point 
Solved Threads: 0
-genESIS- -genESIS- is offline Offline
Newbie Poster

Re: Sorting 2d Array

 
0
  #3
Mar 1st, 2008
Yes, Im just unclear on how to perform the bubble sort to sort the list of words. Any info would be great.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Sorting 2d Array

 
0
  #4
Mar 1st, 2008
Use strcmp() to compare the two strings.

if (strcmp( words[j], words[k] ) < 0) { swap j and k }

The strcmp() function is nice because the operator you use (less-than, equal-to, or greater-than) is exactly the same as it would be if you could put it between the strings like:

if (words[j] < words[k]) ...

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 7
Reputation: -genESIS- is an unknown quantity at this point 
Solved Threads: 0
-genESIS- -genESIS- is offline Offline
Newbie Poster

Re: Sorting 2d Array

 
0
  #5
Mar 2nd, 2008
Ok, well I took a look at the strcpy and strcmp functions, and tried to my ability to put a piece of code together to sort, and then print the sorted list, but for the life of me cannot get it to work.

  1. printf("\n");
  2. printf("Sorted Input {TESTER}: \n");
  3. printf("\n");
  4.  
  5. for(z=0; z<j; z++)
  6. printf("%c", Word[z][j]);
  7.  
  8. for(z=9; z>0; z--) {
  9. for(j=0; j<z; j++) {
  10. if (strcmp(Word[j],Word[j+1])>0) {
  11. strcpy(swap,Word[j]);
  12. strcpy(Word[j],Word[j+1]);
  13. strcpy(Word[j+1],swap);
  14. }
  15. }
  16. }
  17. }

Any suggestions? Im also not to sure what variables I am printing after it is sorted, this kind of confuses me.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Sorting 2d Array

 
0
  #6
Mar 2nd, 2008
Well, so far I have assumed that Word is an array of char pointers.
char *Word[ 10 ];

If that is correct, then to swap you need something like:
  1. char *swap;
  2. ...
  3. // swapping part:
  4. swap = Word[j];
  5. Word[j] = Word[j+1];
  6. Word[j+1] = swap;

However, if your Word is a 2D array:
char Word[ 10 ][ 100 ];
char swap[ 100 ];
then your swap should work fine.

Your bubble sort looks great.

To print the strings, use one of the following:
puts( Word[z] );
printf( "%s\n", Word[z] );

Hope this helps.
Last edited by Duoas; Mar 2nd, 2008 at 1:38 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 7
Reputation: -genESIS- is an unknown quantity at this point 
Solved Threads: 0
-genESIS- -genESIS- is offline Offline
Newbie Poster

Re: Sorting 2d Array

 
0
  #7
Mar 2nd, 2008
Hmmm... for some reason, I still cannot get it. When I compile, this is what I'm gettin:

Error E2342 new4.c 69: Type mismatch in parameter '__s' (wanted 'const signed char *', got 'int') in function main
Warning W8070 new4.c 81: Function should return a value in function main
*** 1 errors in Compile ***

Maybe this is due to the incorrect variable in my for loops or something, I'm not quite sure. I think I'm almost getting this though
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC