Thread: Unsure what is wrong...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    2

    Unsure what is wrong...

    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.
    Last edited by acv; 04-01-2010 at 01:45 AM.

  2. #2
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64
    Just change these lines ,

    Code:
     
     if(sort_m = 1){
      to
    if(sort_m == 1){ // Use == for comparison
    and

    Code:
     if(sort_m = 1){
      to
    if(sort_m == 1){ // Use == for comparison
    Use == is for comparison and = for assignment .Try to use functions in your code.
    Last edited by karthigayan; 04-01-2010 at 01:43 AM.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    2
    Quote Originally Posted by karthigayan View Post
    Just change these lines ,

    [stuff]

    Use == is for comparison and = for assignment .Try to use functions in your code.
    Okay thank you, and for your explanation too!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM