943,859 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 823
  • C RSS
Sep 20th, 2009
0

2D array of strings to be sorted

Expand Post »
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)
{

}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
D_switch is offline Offline
15 posts
since Jul 2009
Sep 20th, 2009
0

Re: 2D array of strings to be sorted

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:
Quote ...
[code]
.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Sep 21st, 2009
0

Re: 2D array of strings to be sorted

  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
D_switch is offline Offline
15 posts
since Jul 2009
Sep 21st, 2009
0

Re: 2D array of strings to be sorted

Here's an assignment. Search why
  1. gets(s[i]);
is a no no.

What is it that you are sorting the names by?
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Sep 21st, 2009
0

Re: 2D array of strings to be sorted

Click to Expand / Collapse  Quote originally posted by D_switch ...
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.
Reputation Points: 651
Solved Threads: 35
Posting Whiz in Training
yellowSnow is offline Offline
201 posts
since Jul 2009
Sep 21st, 2009
1

Re: 2D array of strings to be sorted

For those who are lazy to search through internet here is the link: gets() vs fgets()
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005
Sep 21st, 2009
0

Re: 2D array of strings to be sorted

thank you guys i finally got it
Reputation Points: 10
Solved Threads: 0
Newbie Poster
D_switch is offline Offline
15 posts
since Jul 2009
Sep 21st, 2009
0

Re: 2D array of strings to be sorted

and also about the get() and fgets() thank you for the correction
Reputation Points: 10
Solved Threads: 0
Newbie Poster
D_switch is offline Offline
15 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: gtk+ installation and gcc
Next Thread in C Forum Timeline: Strict Binary Tree





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC