2D array of strings to be sorted

Thread Solved

Join Date: Jul 2009
Posts: 9
Reputation: D_switch is an unknown quantity at this point 
Solved Threads: 0
D_switch D_switch is offline Offline
Newbie Poster

2D array of strings to be sorted

 
0
  #1
Sep 20th, 2009
please help
im really stuck in my sorting
you see i need to make a program that asks the user 10 names of people then the array of names gets pass to another function where it is sorted in ascending or descending depending on what the user wants. that function doesn't return anything.
here's what i got so far:
# include <stdio.h>
# include <string.h>

void sort(char [10][10],int choice);

int main()
{
int i,j,choice;
char array[10][10];
printf("Enter names");

for(i=0;i<10;i++)
{
printf("\nName#%d:",i+1);
gets(array[i]);
}
for(j=0;j<10;j++)
puts(array[j]);
printf("\nPress 1.Ascending\n2.Descending");
scanf("%d",&choice);

sort(array,choice);

return 0;
}
void sort(char array[10][10], int choice)
{

int i,k;
if(choice==1)
{

}
}
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: 2D array of strings to be sorted

 
0
  #2
Sep 20th, 2009
In your previous thread you've already been suggested to use code tags, perhaps you missed it?
Anyway here's the link which will directly bring you to the page where the use of code tags is explained (read it, I'm sure that when you've read it you'll say: "Ohhh, was it that easy?")

P.S.: Instead of manually entering the bb-tags to include code, you could also just simply use the button labeled:
[code]
.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 9
Reputation: D_switch is an unknown quantity at this point 
Solved Threads: 0
D_switch D_switch is offline Offline
Newbie Poster

Re: 2D array of strings to be sorted

 
0
  #3
Sep 21st, 2009
  1. # include <stdio.h>
  2. # include <string.h>
  3. # include <stdlib.h>
  4.  
  5.  
  6. void sorting(char a[][10],int n);
  7. int main()
  8. {
  9. char s[10][10];
  10. int i;
  11.  
  12.  
  13. for(i=0;i<10;i++)
  14. {
  15. printf("Enter name:");
  16. gets(s[i]);
  17. }
  18.  
  19.  
  20. sorting(s,10);
  21. printf("\nSorted Names\n");
  22. for(i=0;i<10;i++)
  23. {
  24. printf("%s\n",s[i]);
  25. }
  26. return 0;
  27. }
  28.  
  29. void sorting(char a[][10],int n)
  30. {
  31. int i,j;
  32. int choice;
  33. char temp[10];
  34.  
  35. printf("Choose\n1.Ascending\n2.Descending\n");
  36. scanf("%d",&choice);
  37. if(choice==1)
  38. {
  39. for(i=0;i<10;i++)
  40. {
  41. for(j=i+1;j<10;j++)
  42. {
  43. if(strcmp(a[j],a[i])<0)
  44. {
  45. strcpy(temp,a[i]);
  46. strcpy(a[i],a[j]);
  47. strcpy(a[j],temp);
  48. }
  49. }
  50. }
  51. }
  52. else
  53. {
  54.  
  55. }
  56. }

now im stuck as to how to display it in decending order
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,030
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: 2D array of strings to be sorted

 
0
  #4
Sep 21st, 2009
Here's an assignment. Search why
  1. gets(s[i]);
is a no no.

What is it that you are sorting the names by?
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 201
Reputation: yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold 
Solved Threads: 35
yellowSnow's Avatar
yellowSnow yellowSnow is offline Offline
Posting Whiz in Training

Re: 2D array of strings to be sorted

 
0
  #5
Sep 21st, 2009
Originally Posted by D_switch View Post
now im stuck as to how to display it in decending order
Well you've almost done it. Change the comparison in your strcmp() to be "greater than" (>) to sort the names in descending order. In addition to studying up on why gets() is a poor function (use fgets() instead), study up on the strcmp() function as well to understand why a "greater than" comparison will give you the names in descending order.
Manic twiddler of bits
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: 2D array of strings to be sorted

 
1
  #6
Sep 21st, 2009
For those who are lazy to search through internet here is the link: gets() vs fgets()
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 9
Reputation: D_switch is an unknown quantity at this point 
Solved Threads: 0
D_switch D_switch is offline Offline
Newbie Poster

Re: 2D array of strings to be sorted

 
0
  #7
Sep 21st, 2009
thank you guys i finally got it
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 9
Reputation: D_switch is an unknown quantity at this point 
Solved Threads: 0
D_switch D_switch is offline Offline
Newbie Poster

Re: 2D array of strings to be sorted

 
0
  #8
Sep 21st, 2009
and also about the get() and fgets() thank you for the correction
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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