0

I'm trying to make the letters of a matrix sort alphabetically and then be written out in a single string.For instance you type ten words,which are then stored in an array,and every letter has its place in the matrix then,right?But after I've written the words I want to bunch all the letters of all words together and then type all the letters out in alphabetical order.This is what I have so far:

#include <stdio.h>
#include <conio.h>

int main(void){
    int i, j, k, f, n, m; 
    //was trying out various things,that's why I have so many useless ints up there
    char word[10][15],temp;

    for(i=0;i<=9;i++)
    {
        printf("Type in wword number %d: ", i+1);
        gets(word[i]);
    }

    for(k=i-1;k>=0;k--)
    {
        for(m=0;m<k;m++)
            if(word[k][f] > word[m][n])
            {
                temp=word[k][f];
                word[k][f]=word[m][n];
                word[m][n]=temp;
            }
    }
    printf("Letters alphabetically sorted: ");
    for(i=0;i<=9;i++){
        for(j=0;j<=14;j++){
            printf("%d",word[i][j]);
        }
    }
    printf("\n");
    getch();
}

I'm still in the process of learning about matrixes and I've gotten pretty familiar with arrays by now.But the sorting thing is confusing me,this was my attempt but it doesn't work.It lets you write all the words,and then it crashes.

What am I doing wrong here?And how do I correct it?

4
  • I notice f and n not initialised which is dangerous. Commented Sep 21, 2011 at 6:38
  • Wait nevermind,figured it out.Thank you for the comment though. Commented Sep 21, 2011 at 6:45
  • Great happy coding then! Commented Sep 21, 2011 at 7:24
  • What's the difference between "array" and "matrix"? Commented Sep 22, 2011 at 8:42

4 Answers 4

3

In your code here:

        temp=word[k][f];
        word[k][f]=word[m][n];
        word[m][n]=temp;

the variables n and f are used uninitialised. That will most likely be the cause of the crash.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok thnx :) Now it doesn't crash.But at the end,instead of the letters it displays some odd row of numbers.Why is that?
You want to print out with "%c": that's "character". You're printing "%d", which are numbers.
Ok,put it to %c.Now it displays characters though hmmm,I was figuring it would type out letters from the words.Like I type in the words "one,two,three" and it types out: "eeehnoorttw"
1

f,n are uninitialized. It has garbage and is the reason for crashing at this point.

for(k=i-1;k>=0;k--)
{
    for(m=0;m<k;m++)

    if(word[k][f] > word[m][n]) // f,n are uninitialized and are error prone

1 Comment

Thank you for the help,I've fixed the initializing now.And I changed the last %d to a %c.But now it displays characters.I was hoping it would type out letters from the words.Example: I type in the words "one two three" and it types out: "eeehnoorttw".How do I make it so?
0

I think this will work..Please excute and tell me..

void main()    
{

char word[10][15],temp,sorted_word[15];

int i,j,ii,k,l=0;

for(i=0;i<=9;i++)             
{                  
printf("Type in wword number %d: ", i+1);                 
gets(word[i]);            
}


for(i=0;i<=9;i++)

{

for(j=0;word[i][j]!='\0';j++)

{

ii=i;
for(k=j+1;1;k++)
{
if(ii==9 && word[ii][k]=='\0')
break;
if(word[ii][k]=='\0')
{
ii++;
k=0;
}
if(word[i][j]>word[ii][k])
{
temp=word[i][j];
word[i][j]=word[ii][k];
word[ii][k]=temp;
} 
}
sorted_word[l++]=word[i][j];
}

}

sorted_word[l]='\0';

printf("%s",sorted_word);

getch();

}

Comments

-1

here the

for(i=0;i<=9;i++)
{ printf("type in wword %d: ",i+1);
gets(word[i]); 
}

gets (word[1]);

stores the value from word[1] onwards but where as the character array starts from word[0].

may be this is not the full solution for u problem this issue may help u in solving your doubt.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.