So, I'm rather new to C, and as part of a lab homework we are supposed to read a list of numbers from a file and sort them in either ascending or descending order (user gets to choose). After doing some internet searching I decided to use a bubble sort (I can actually understand the code for the most part). However, when I compile and run, now matter what number I put in for sort_m (see code below), it always sorts it in ascending order. So:
- I assume I am misusing the if/else if/else statements, but I'm lost as to the specifics, can anyone tell me what I'm doing wrong? [FIXED]
- When doing the bubble sort, what is "for(x = 0; x < count; x++)" (again see below for code) used for?
- When I don't have "#include <stdlib.h>", I get the warning "incompatible implicit declaration of built-in function ‘exit’ ", but with it I do not, why is this?
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
main() {
/* Declare main program variables */
char filename_i[256]; /* Declare character filename_i, 256 characters max */
char filename_o[256]; /* Declare character filename_o, 256 characters max */
int sort_m; /* Declare integer sort_m */
FILE *in_fd,*out_fd; /* Declare the variables you will call the input & output files */
int count,x,y,z,temp_a,temp_d; /* Declare count,i,x,temp_a,temp_d as integers */
count = x = y = z = temp_a = temp_d = 0; /* Intialize all the previously declared integers at 0 */
int array_s[MAX]; /* Declare array_s, 100 integers max */
int next_number; /* Declare integer next_number */
/* Get name of input file from user */
printf("Enter input file name: ");
scanf("%s",filename_i);
/* Open file, if it fails, print error message and exit */
if ((in_fd = fopen(filename_i,"r")) == 0) {
fprintf(stderr,"%s: No such file.\n",filename_i);
exit(0);
}
/* This will read the numbers from the file until it reaches */
/* end of file. As it does this, it rights them to an array */
while(fscanf(in_fd,"%d\n",&next_number)!=EOF){
/* Now, we write the number we are currently reading from the file into an array */
array_s[count] = next_number;
count++; /* Increase variable count by 1 */
}
/* Get sorting method from user */
printf("Sort [1] Ascending or [2] Descending?\nSort Method: ");
scanf("%d",&sort_m);
/* Now for the sorting algorithm */
if(sort_m = 1){
for(x = 0; x < count; x++)
for(y = 0; y < count-1; y++)
if(array_s[y] > array_s[y+1]){
temp_a = array_s[y]; /* Write array_s[y] to another variable temporarily */
array_s[y] = array_s[y+1]; /* Overwrite array_s[y] with the smaller value, array_s[y+1] */
array_s[y+1] = temp_a; /* Overwrite array_s[y+1] with the larger value you temporarily stored in temp_a */
}
}
else if(sort_m = 2){
for(x = 0; x < count; x++)
for(y = 0; y < count-1; y++)
if(array_s[y] < array_s[y+1]){
temp_d = array_s[y]; /* Write array_s[y] to another variable temporarily */
array_s[y] = array_s[y+1]; /* Overwrite array_s[y] with the larger value, array_s[y+1] */
array_s[y+1] = temp_d; /* Overwrite array_s[y+1] with the smaller value you temporarily stored in temp_d */
}
}
else{
printf("Invalid selection, please run the program again"); /* Print an error message if there was no valid choice */
}
for(z = 0; z < count; z++){
printf("%d\n",array_s[z]); /* Print the array so you can see if it worked */
}
}
Edit - Sorry for not making my title descriptive, completely slipped my mind.