Hy guys. My English is not so good, sorry for any mistakes.
I'm a bigginer in C, and I have a problem. I want to sort ascending n strings, but I can't read the strings. Here is what I've done so far:
Code:
//sort ascending n strings
#include <stdio.h>
int main()
{
int n,i,j;
char a[20];
printf("How many strings?");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
printf("a[%d]=",i);
scanf("%s",&a[i]);
}
printf("\n");
for (j=1;j<=n;j++) //Here I want to write them, so I'm sure I've read them good
printf("%s\n",a[j]); //the problem apears here: Segmentation fault
return 0;
}
If someone could help me, I would be grateful.
ps: I have read in this forum something about sscanf(), gets(), but I don't know how to use them... How do I use them in this problem?
#include <stdio.h>
int main(int argc, char **argv)
{
int n=0,
i=0,
j=0;
char a[20][80]={0x0}; /* this makes twenty 80 character-long strings */
printf("How many strings?");
scanf("%d",&n);
for (i=1;i<=n && i < 20;i++)
{
printf("a[%d]=",i);
scanf("%s", a[i]);
}
printf("\n");
for (j=1;j<=n && j < 20;j++) //Here I want to write them, so I'm sure I've read them good
printf("%s\n",a[j]); //the problem apears here: Segmentation fault - do not go past 20
return 0;
}
I made some changes. You still need a sorting algorithm.
Not homework, I do this for fun, but sometimes it doesn't turn out like I want. I'm going to look over the changes you made, and learn from my mistake Thank you jim mcnamara.
By the way, it's most likely that I would make some mistakes on the sorting algorithm.
To sort 2 strings, I have to break down intro chars every string and compare the 2 chars?
String 1: abc
String 2: abd
a==a, b==b ,c!=d, resuts string 1<string 2. Something like that?
---------- Post updated at 10:39 AM ---------- Previous update was at 07:24 AM ----------
I made a sorting algorithm for 2 strings:
Code:
//sort ascending two strings
#include <stdio.h>
int main()
{
char a[10],b[10];
char s1,s2;
int i=0;
printf("First string:"); scanf("%s",&a);
printf("Second string:");scanf("%s",&b);
printf("\n");
printf("String 1:%s\n",a);
printf("String2:%s\n\n",b);
printf("Ascending order:\n");
do
{
s1=a[i];
s2=b[i];
i++;
}
while (s1==s2);
if (s1<s2)
{
printf("%s ",a);
printf("%s",b);
}
else
{
printf("%s ",b);
printf("%s",a);
}
printf("\n\n");
return 0;
}
Then I tryed to apply it to my problem.
Code:
//sort ascending n strings
#include <stdio.h>
int main(int argc, char **argv) //why int argc, char **argv ?
{
int n=0,
i=0,
j=0,
l=0;
char s1,s2;
char a[20][80]={0x0}; //what does {0x0} means?
char aux[80];
printf("How many strings?");
scanf("%d",&n);
for (i=1;i<=n && i < 20;i++)
{
printf("a[%d]=",i);
scanf("%s", a[i]);
}
printf("\n");
for (j=1;j<=n && j < 20;j++)
printf("%s\n",a[j]);
for (i=1;i<=n-1 && i<20;i++) //a[i] and a[i+1] so i goes to i<=n-1 ?
{
do
{
s1=a[i][l];
s2=a[i+1][l];
l++;
}
while(s1==s2);
if (s1<s2)
{
aux=a[i]; //invalid array assignment
a[i]=a[i+1]; //invalid array assignment
a[i+1]=aux; //invalid array assignment
}
}
for (j=1;j<=n && j < 20;j++)
printf("%s\n",a[j]);
return 0;
}
It has 3 errors. I declared wrong the aux variable, yes? How can I interchange the 2 strings compared?
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 2,288
Thanks Given: 430
Thanked 480 Times in 395 Posts
Hi.
Quote:
Originally Posted by 1/0
I'll try. I have never used qsort, but i'll google it and come back with new questions
Meta-help:
There are man pages available here at unix.com. I would not discourage your use of Google, but when you know the name of an item, such as qsort, you might save some time by looking here.
Click the main-menu-bar item Man Pages, enter a string, check the appropriate controls, and you will see (for qsort and apropos) a number of choices.
Thank you. I didn't know that until now. It sure is useful. I only need the sorting algorithm now, but it is giving me problems. Sorry for the english... some words might be writen wrong (I'm romanian).
Hi,
i had a data block (coming from pipe from other codes) as:
H YF_CO.dat 77164 11/17/2013 04:00:02 731374590.96 1 1 731374590.96 76586 77164 578 2988 Y
H YF_CO.dat 77164 11/17/2013 04:00:07 731374590.96 1 4 731374590.96 76586 77164 578 2988 Y
H YF_CO.dat 77178 ... (5 Replies)
Hi,
I have a sequence which has 30000 strings which looks like this
>string2991 234445
>string224 470561
>string121 675386
>string4098 177229
>string8049 255838
>string8 672382
>string1115 578415
I want it to be arranged in ascending order
>string8 672382
>string121... (5 Replies)
I have a column of numbers in the following format:
1.722e-05
2.018e-05
2.548e-05
2.747e-05
7.897e-05
4.016e-05
4.613e-05
4.613e-05
5.151e-05
5.151e-05
5.151e-05
6.1e-05
6.254e-05
7.04e-05
7.12e-05
7.12e-05 (6 Replies)
Hi there
I have to enhance my current file looping to ensure the oldest file being processed first.
current command:
for FILENAME in `find $MY_DIRECTORY -follow -type f`
I manage to get command for order by date modified descending, just can't get the ascending order. Please help
for... (3 Replies)
Hello,everyone. I am learning some Info commands.I put all commands and their explanations in a file.
This is a part of it:
ESC PgUp (scroll-other-window-backward)Scroll the other window backward
ESC Right (forward-word) Move forward a word
ESC r (move-to-window-line)
ESC TAB... (3 Replies)
I have a FILE1.DAT with the following information
21111111110001343 000001004OLF-AA029100020091112
21111111110000060 000001004ODL-CH001000020091112
24444444440001416 000001045OLF-AA011800020091112
23333333330001695 000001039OLF-AA030600020091112
23333333330000111... (5 Replies)
HI everyone,
I am trying to use the unix sort command to get a list of numbers sorted in ascending order but having trouble in getting it to work.
An example of this issue would be when i am trying to sort the following three
number each on a different line "1" , "2" and "116" the sort command... (3 Replies)