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;
}