473,385 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Array of strings sorted

Expand|Select|Wrap|Line Numbers
  1. #include<string.h>
  2. main (void){
  3.     char name[20][20];
  4.     int i;
  5.     clrscr();
  6.     for(i=0;i<=4;i++){
  7.         printf("Enter the Names:\n",i);
  8.         scanf("%s",&name[i]);
  9.         }
  10.  
  11.         //sorting//
  12.  
  13.        for(i=0;i<=4;i++){
  14.         printf("%s\n",name[i]);
  15.         }
  16.     getch();
  17.     return 0;
  18. }
here's my running program, but my instructor want it to be sorted alphabetically... so someone help me with this...
its 2 dimensional array of strings but not yet sorted...
pls give me some function how to sort it by alphabetical order...
Feb 13 '07 #1
6 3481
horace1
1,510 Expert 1GB
can you use the sort library funtion, e.g.
http://www.cplusplus.com/reference/clibrary/cstdlib/qsort.html

or do you have to write you own?

you can use function strcmp() to compare a pair of strings
http://www.cplusplus.com/reference/clibrary/cstring/strcmp.html
Feb 13 '07 #2
Expand|Select|Wrap|Line Numbers
  1. #include<string.h>
  2. main (void){
  3.     char name[20][20];
  4.     int i,x;    
  5.     char hold;
  6.     clrscr();
  7.         for(i=0;i<=4;i++){
  8.         printf("Enter the Names:\n",i);
  9.         scanf("%s",&name[i]);
  10.         }
  11.  
  12.         //sorting//
  13.  
  14.  
  15.         for(i=0;i<=4;i++){
  16.           for(x=i;x<=4;x++){
  17.             name[i]="abc";
  18.             hold[x]="afs";
  19.             if(name[i]>hold[x]){
  20.              printf("%s\n",name[i]);
  21.             else
  22.             printf("%s\n",hold[x]);
  23.             }
  24.             }
  25.          }
  26.        for(i=0;i<=4;i++){
  27.         printf("%s\n",name[i]);
  28.         }
  29.     getch();
  30.     return 0;
  31. }

anybody can sort by this by alphabetical order??
to sort pls start at the statement //sorting//
Feb 14 '07 #3
RedSon
5,000 Expert 4TB
#include<string.h>
main (void){
char name[20][20];
int i,x;
char hold;
clrscr();
for(i=0;i<=4;i++){
printf("Enter the Names:\n",i);
scanf("%s",&name[i]);
}

//sorting//


for(i=0;i<=4;i++){
for(x=i;x<=4;x++){
name[i]="abc";
hold[x]="afs";
if(name[i]>hold[x]){
printf("%s\n",name[i]);
else
printf("%s\n",hold[x]);
}
}
}
for(i=0;i<=4;i++){
printf("%s\n",name[i]);
}
getch();
return 0;
}


anybody can sort by this by alphabetical order??
to sort pls start at the statement //sorting//
No one is going to write your code for you. Horace gave you the tool that you need to compare two strings, why don't you try to use it to compare a few strings and see if you can determine how you might be able to tell which one comes before the other in alphabetical order.
Feb 14 '07 #4
AdrianH
1,251 Expert 1GB
No one is going to write your code for you. Horace gave you the tool that you need to compare two strings, why don't you try to use it to compare a few strings and see if you can determine how you might be able to tell which one comes before the other in alphabetical order.
You should also state what you ment by sorting a 2D array as this could mean a few different things. Also describe any restrictions and post your solution (working or not) so that we can see that you are trying.


Adrian
Feb 14 '07 #5
thats it, i don't know how to do it the sorting part, thats y i need your help...
Feb 20 '07 #6
Ganon11
3,652 Expert 2GB
I assume you are sorting the 2D char array (char name[20][20]) as above.

Then I would assume this means that as you increase the first index (name[0], name[1], ... , name[19]), the strings stored in the second index (name[0][0] through name[0][19]) should be getting lexicographically larger, or alphabetically larger. If you have "Animal" and "Baseball", then "Baseball" is lexicographically larger - thus, "Baseball"s index will be greater than "Animal"s.
Feb 20 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Dr John Stockton | last post by:
Q1 : Given an array such as might have been generated by var A = is there a highly effective way of reducing it to - i.e. removing the undefineds and shifting the rest down? ...
3
by: SilverWolf | last post by:
I need some help with sorting and shuffling array of strings. I can't seem to get qsort working, and I don't even know how to start to shuffle the array. Here is what I have for now: #include...
3
by: Niklas Uhlin | last post by:
Why does the code below output this: "v a" (118,32,97) "w a" (119,32,97) "v b" (118,32,98) "w b" (119,32,98) "v c" (118,32,99) "w c" (119,32,99) and not this (as one would expect if...
1
by: Stuart | last post by:
I am converting a VB6 App. In the VB6 app, I have a Type structure of about 30 elements (mixture of booleans, integers, strings and some doubles), in an array of say 20 to 100+ records. The...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
25
by: Rainmaker | last post by:
Hi, Can anyone tell me an efficient algorithm to sort an array of strings? Keep in mind that this array is HUGE and so the algorithm should me efficient enough to deal with it. Thanks
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
8
by: David Lazos | last post by:
Hi All, I use Contains method of String object to determine if a string variable has another string, like that. *************************** ipAddress.Contains("127.0.0")...
4
by: Jerry Spence1 | last post by:
I'm using VB2005 I have an array of some 500,000 items which are of the form 080715_175327_312.jpg. These are camera frames of the form YYMMDD_HHmmss_ms.jpg I want to create another array...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.