I have an array of numbers that I read in from the user. I know that they have been read in correctly because I am able to print them. Only problem is I can't sort my array. Can someone please help?
Code:#include <stdio.h> #include <string.h> int main (void) { char fname[256]; FILE* fin; int n; //Size of the set allowed digits int k; //Length of the target numbers int numbers[10]; int i, j; int temp; printf("Enter the name of the file to read from: \n"); scanf("%s", fname); fin = fopen(fname, "r"); //Check to make sure the file exists and opens correctly if(fin == NULL) { printf("Unable to open the file %s\n", fname); system("PAUSE"); return 0; } /*Read in the two integers on the first line of the text file*/ //How many numbers to read in fscanf(fin, "%d ", &n); //0 in the file means stop! if (n == 0) return; //How many digits for each number to be fscanf(fin, "%d ", &k); //Make sure array is empty for (i = 0; i <= n; i++) { numbers[i] = 0; } //Second line for(j = 0; j < n; j++) fscanf(fin, "%d \n", &numbers[j]); //Sort the array for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (numbers[i] > numbers[j]) { temp = numbers[i]; numbers[i] = numbers[j]; numbers [j] = temp; printf("We are in the sort loop now \n"); printf("%d \n", numbers[i]); } } } fclose(fin); system("PAUSE"); return 0; }



LinkBack URL
About LinkBacks


