Thread: Help needed in this simple bubble-sort program.

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    2

    Help needed in this simple bubble-sort program.

    Hello, I've very recently started to learn to program (in general) with my first language being the ANSI C. I've found an assignment which I'm trying to work on, but without any luck so far.
    Write a short program that will calculate 10 random integers and store them in one-dimensional array, then present stored integers as they are and sorted in decreasing order. Use bubble sort algorithm and rand() function. Use array indexes only! The range of integers should be passed to your program as execution parameters.
    I encountered two problems, 1st being the fact that bubble sort procedure doesn't work at all, 2nd - the program doesn't want to calculate more than 15 random integers. Here's the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int sort (int numbers[], int array_size)
    {
     int b,j,temp;
     for(b= (array_size-1);b>0;b--)
      {
       for(j=1; j<=b; j++)
        {
         if (numbers[j-1]>numbers[j])
          {
           temp = numbers[j-1];
           numbers[j-1] = numbers[j];
           numbers[j] = temp;
          }
        }
      }
    }
    int main (int argc, char* argv[])
    {
     int a,i,rgn,in,lowerlim,upperlim,store[argc-1];
     lowerlim = atoi(argv[1]);
     upperlim = atoi(argv[2]);
     rgn = atoi(argv[3]);
     srand(time(NULL));
     i = 0;
     printf("Unsorted elements:  ");
     for (a = 0;a<(rgn);a++)
      {
       store[i] = (rand() % upperlim + lowerlim);
       printf("%d ",store[i]);
       i++;
      }
     printf("\nSorted elements:  ");
     sort(store,rgn);
     for (a=0;a<(rgn);a++)
      {
       printf("%d ",store[i]);
       i++;
      }
    printf("\n");
    return 0;
    }
    I it may look terryfing to some, but like I've said, I'm very new to programming. I worked to fix this program for almost a day, but without any results. I'd be happy for any help/comments/remarks etc. regarding any part of the program that can be made better/simpler.

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    1. Use more horizontal space (yes, it's a style thing)
    2. store[argc - 1] is very probably not what you want. You know there will only be up to 10 numbers, so use store[10].
    3. To calculate a random number between two limits the formula is (rand() % (hi - lo + 1)) + lo (thanks to GReaper).

    I haven't checked your sort function.

    Have fun!

    Edit: formula corrected
    Last edited by qny; 09-13-2012 at 03:19 AM. Reason: formula corrected

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    41
    Code:
    for (a=0;a<(rgn);a++)
      {
       printf("%d ",store[i]);
       i++;
      }
    This is wrong as alredy i is greater than rgn, it won't print the sorted array.
    Sorting is working fine.

    Initialize i again to '0' or the following code also works fine.
    Code:
    for (a=0;a<(rgn);a++)
      {
       printf("%d ",store[a]);
      }

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    2
    It worked! Thank you both for your help, I can't believe the solution was that easy.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by qny View Post
    To calculate a random number between two limits the formula is (rand() * (hi - lo)) + lo.
    rand() % (high - low + 1) + low

    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Quote Originally Posted by GReaper View Post
    rand() % (high - low + 1) + low

    Unless you interpret "between" to mean a half-open interval [low,high), which includes the low number but excludes the high number. But this really depends on context and actual requirements for the program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 04-19-2012, 01:24 AM
  2. Bubble sort program stumped
    By matthughes in forum C Programming
    Replies: 12
    Last Post: 05-17-2007, 01:28 PM
  3. simple bubble sort
    By recluse in forum C Programming
    Replies: 4
    Last Post: 12-05-2004, 09:11 AM
  4. problem(program error) with bubble sort(code included)
    By choykawairicky in forum C++ Programming
    Replies: 6
    Last Post: 05-16-2004, 08:54 AM
  5. Bubble Sort, Qucik Sort
    By insomniak in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 04:54 PM