Search code examples
cbubble-sort

Bubble sorting a matrix to get some characters in ascending order


I have a file called testing.txt where I have the following matrix of characters:

h 3 l l 0 t h
3 r 3 h 0 w a
r e y 0 u d 0
1 n g 2 d a y

With the code that you can see at the end of the question I am separating the characters into two lists named as follows: numbers and characters so that I can print them in my screen as follows (1st the numbers and then the letters):

0 0 0 0 1 2 3
3 3 h l l t h
r h w a r e y
u d n g d a y

Please note that I want the numbers to appear in ascending order, while the letters I want to keep them in the same order as they were originally in the matrix.

The question I have: everything is working fine except for the ordering of the numbers. Meaning, the numbers are getting ordered in the code, however when I print the matrix in my screen I only get this:

h l l t h r
h w a r e y
u d n g d a
y

So the numbers are missing. How can I solve this one?

#include <stdio.h>
#include <stdlib.h>
#include <string.h> 

int main()
{

FILE *fich;
char c = 'f', *characters = NULL, *numbers = NULL, *final_list = NULL, aux = 'a';
int character = 0, number = 0, i = 0, j = 0, rows = 1, columns = 0;

fich = fopen("testing.txt", "r");

/*Check if file is available*/

if(fich == NULL)
{
   puts("The file couldn't be opened");
   return 1;
}

/*Allocate dynamic memory*/

characters = (char*) calloc(character, sizeof(char));
numbers = (char*) calloc(number, sizeof(char));

/*Save the list of characters and numbers*/

rewind(fich);

number = character=0;

while ((c = fgetc(fich)) != EOF)
{
   if (c >= 'a' && c <= 'z')
   {
      *(characters+number) = c;
      number++;
   } else if (c >= '1' && c <= '9')
      {
         *(numbers+character) = c;
         character++;
      } else if (c == '\n') 
         {
            rows++;
         }
} 

fclose(fich); 
fich = NULL;

/*Order the lists*/

for (i = 0; i < (number-1); ++i)
{
   for (j = i+1; j < number; ++j)
   {
      if (*(numbers+i)>*(numbers+j))
      {
         aux = *(numbers+j);
         *(numbers+j) = *(numbers+i);
         *(numbers+i) = aux;
      }
   }
}

/*Concat the lists*/

final_list = (char*) calloc((number+character), sizeof(char));

strcat(final_list, numbers);
strcat(final_list, characters);

/*Print the new matrix*/

columns = (number+character)/rows;

for (i = 0; i < (number+character); ++i)
{  
   printf("%c ", *(final_list+i));
   if ((i+1)%columns == 0)
         printf("\n");
}

return 0;
}

Solution

  • You have interchanged number and character counter inside the while loop used to read the file. Change it as follows,

    while ((c = fgetc(fich)) != EOF)
    {
       if (c >= 'a' && c <= 'z')
       {
           *(characters+character) = c;
           character++;
       } else if (c >= '1' && c <= '9')
       {
           *(numbers+number) = c;
           number++;
       } else if (c == '\n') 
       {
           rows++;
       }
    }
    

    Also, you need to call calloc for numbers and characters with size at least equal to no. of numbers and characters in the file respectively.