Thread: Bubble sorting an array

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    4

    Bubble sorting an array

    Hi, I was wondering if anyone had any ideas on where I have been going wrong with my bubble sort. The aim of my code is to read a string of numbers and bubble sort the numbers into numerical order. I need this to happen with 3 lines of text read from a txt file.
    I have been able to read the numbers successfully from the file and assign them to the 3 different arrays. I have then printed the line1 array to screen before the bubble sort and then after to see if it works. It hasn't I have tried two ways of bubble sorting, by using one example from the C progamming for dummies book which says that the bubble sort used is incompatible with a string obtained using fgets?!? I don't see how as its an array with values stored in it, I can't see why it would matter where they would have come from. Below is a different example of a bubble sort and it compiles but has a warning about a pointer, specifically: [Warning] passing argument 1 of 'fgets' from incompatible pointer type [enabled by default]. Below is my code if anyone could help it would be greatly appreciated.
    Thanks :-)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    
    
    int main() {
    	
    	int line1[64], line2[64], line3[64],  n, c, d, swap;
    	
    	FILE* numbers;
    	numbers = fopen("list_read.txt", "r");
    	
    	fgets(line1, 64, numbers);
    	
    	fgets(line2, 64, numbers);
    
    
    	fgets(line3, 64, numbers);
    	
    	printf("%s", line1);
    	
    for (c = 0; c < n; c++)
        
      for (c = 0 ; c < ( n - 1 ); c++)
      {
        for (d = 0 ; d < n - c - 1; d++)
        {
          if (strncmp(line1[d] > line1[d+1])) 
          {
            swap       = line1[d];
            line1[d]   = line1[d+1];
            line1[d+1] = swap;
          }
        }
      }
    	printf("\n%s", line1);
    	
    	
    	
    	
    	return 0;
    }

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    fgets is used for char arrays not Int arrays.
    char *fgets(char *str, int n, FILE *stream); // prototype for fgets

    That is where your warning is coming in.

    EDIT: You need to look at the logic of your code on paper, for starters you have your loops going n times, and n is not even defined.
    Last edited by camel-man; 05-04-2015 at 09:21 AM.
    Code:
    int get_random_number(void)
    {
       return 4; //chosen by fair dice roll.
                 //guaranteed to be random
    }

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    4
    Right cool thanks. Now I'm going to bin the bubble sort out for the moment. I can see where I'm going wrong from the beginning. The issue I now have is converting a char array to an int array. I tried using atoi, but the only problem is I found that I can only store the first number from the char array into a single int. Not the whole string from the char array into an int array. I've been having a look about and can't seem to find a way of doing this.

    I keep trying to think if I could use fscanf to assign the first line of numbers to an int array. However surely if I had a loop it would continue reading the first line of numbers in the file and then drop down to the second line and keep reading/assigning to the array?!?

    I basically have three lines of 6 numbers in a file (some multi digit). Each line I need to assign to an array and then bubble sort each line so the numbers in the array are then printed to screen in numerical order...

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    use fgets to read line into some temporary buffer and then sscanf to fill your array based on values in the current line (or strtol in a loop)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bubble sorting an array using NO loops.
    By Kristyy_mariee in forum C++ Programming
    Replies: 20
    Last Post: 03-28-2012, 01:23 PM
  2. Bubble Sorting
    By yukapuka in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2008, 09:44 PM
  3. bubble sorting a 2-d array
    By stodd04 in forum C Programming
    Replies: 5
    Last Post: 03-17-2005, 01:40 PM
  4. Bubble Sorting HELP WANTED !
    By Shahram_z in forum C++ Programming
    Replies: 4
    Last Post: 03-12-2003, 08:12 PM
  5. help with my bubble sorting of arrays
    By Matt in forum C Programming
    Replies: 1
    Last Post: 12-11-2001, 04:43 PM