| | |
Sorting 2d Array
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2008
Posts: 7
Reputation:
Solved Threads: 0
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
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
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> int main(void) { char Word[10][19]; int i, j, k, l, z; int value; char swap[19]; char ch; for(z=0 ; z<10 ; z++) { if (z == 0) printf("Enter the 1st word: "); else if (z == 1) printf("Enter the 2nd word: "); else if (z == 2) printf("Enter the 3rd word: "); else printf("Enter the %dth word: ",z+1); i=0; while(( ch = fgetc(stdin)) != '\n') { i=i+1; Word[z][i]= ch; } l=10; for(k=i ; k>=1; k--) { Word[z][l]=Word[z][k]; l=l++; } Word[z][19]=i; } printf("\n"); printf("Unsorted Input: \n"); printf("\n"); for (z=0 ; z<10 ; z++) { for(j=10 ; j<=9+Word[z][19]; j++) { value = Word[z][j]; Word[z][j] = '\0'; if (value >= 97 && value<=122) { value = value - 32; } else if (value >=67 && value<=90) { value = value + 32; } printf("%c", value); } for(j=1; j<=Word[z][19]; j++) { value = Word[z][j]; if (value >= 97 && value<=122) { value = value - 32; } else if (value >=67 && value<=90) { value = value + 32; } printf("%c", value); } putchar('\n'); } printf("\n"); printf("Sorted Input: \n"); printf("\n"); }
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.
Use strcmp() to compare the two strings.
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.
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.
•
•
Join Date: Mar 2008
Posts: 7
Reputation:
Solved Threads: 0
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.
Any suggestions? Im also not to sure what variables I am printing after it is sorted, this kind of confuses me.
C Syntax (Toggle Plain Text)
printf("\n"); printf("Sorted Input {TESTER}: \n"); printf("\n"); for(z=0; z<j; z++) printf("%c", Word[z][j]); for(z=9; z>0; z--) { for(j=0; j<z; j++) { if (strcmp(Word[j],Word[j+1])>0) { strcpy(swap,Word[j]); strcpy(Word[j],Word[j+1]); strcpy(Word[j+1],swap); } } } }
Any suggestions? Im also not to sure what variables I am printing after it is sorted, this kind of confuses me.
Well, so far I have assumed that Word is an array of char pointers.
If that is correct, then to swap you need something like:
However, if your Word is a 2D array:
then your swap should work fine.
Your bubble sort looks great.
To print the strings, use one of the following:
Hope this helps.
char *Word[ 10 ];If that is correct, then to swap you need something like:
C Syntax (Toggle Plain Text)
char *swap; ... // swapping part: swap = Word[j]; Word[j] = Word[j+1]; 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.
•
•
Join Date: Mar 2008
Posts: 7
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- sorting array (C++)
- bubble sorting in an array (C)
- Sorting arrays and then checking for duplicate characters (Java)
- Sorting arrays of pointers with function? (C)
- sorting an array of string (C)
Other Threads in the C Forum
- Previous Thread: Median.c Please Help
- Next Thread: Core Segmentation Error
| Thread Tools | Search this Thread |
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest kernel km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming stack standard string strings structures systemcall testautomation turboc unix user variable voidmain() wab win32api windows.h






