| | |
2D array of strings to be sorted
Thread Solved |
•
•
Join Date: Jul 2009
Posts: 9
Reputation:
Solved Threads: 0
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)
{
}
}
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)
{
}
}
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: .
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."
•
•
Join Date: Jul 2009
Posts: 9
Reputation:
Solved Threads: 0
C Syntax (Toggle Plain Text)
# include <stdio.h> # include <string.h> # include <stdlib.h> void sorting(char a[][10],int n); int main() { char s[10][10]; int i; for(i=0;i<10;i++) { printf("Enter name:"); gets(s[i]); } sorting(s,10); printf("\nSorted Names\n"); for(i=0;i<10;i++) { printf("%s\n",s[i]); } return 0; } void sorting(char a[][10],int n) { int i,j; int choice; char temp[10]; printf("Choose\n1.Ascending\n2.Descending\n"); scanf("%d",&choice); if(choice==1) { for(i=0;i<10;i++) { for(j=i+1;j<10;j++) { if(strcmp(a[j],a[i])<0) { strcpy(temp,a[i]); strcpy(a[i],a[j]); strcpy(a[j],temp); } } } } else { } }
now im stuck as to how to display it in decending order
Here's an assignment. Search why is a no no.
What is it that you are sorting the names by?
C Syntax (Toggle Plain Text)
gets(s[i]);
What is it that you are sorting the names by?
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
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
Behind every smile is a tear.
Visal .In
![]() |
Similar Threads
- Sorting a 2D Array of Strings (C++)
- how to read an array of strings in C++ (C++)
- 2d array of strings (C++)
- Declaring an array of strings in C++ (C++)
- Initializing an array of strings and printing it. (C++)
- Is there a way to tokenize an array of strings (C++)
- filling an array with strings using gets() (C)
Other Threads in the C Forum
- Previous Thread: gtk+ installation and gcc
- Next Thread: Strict Binary Tree
| Thread Tools | Search this Thread |
#include * adobe ansi array asterisks binarysearch centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc database dynamic execv feet fgets file floatingpointvalidation fork function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide inches include incrementoperators infiniteloop input interest intmain() iso kernel keyboard kilometer license linked linkedlist linux list locate looping lowest match matrix meter microsoft number oddnumber opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprograming socketprogramming standard strchr string suggestions systemcall test threads turboc unix urboc user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






