NO

Author Topic: How can I sort an array of random numbers?  (Read 10687 times)

rudametc

  • Guest
How can I sort an array of random numbers?
« on: August 25, 2011, 05:11:37 AM »
I have a program for homework that says:
Write a program that fills an integer arrar with 100 random numbers with the values from 600 to 799.  Print these numbers 8 per line, skip two lines, then sort the numbers in ascending order.

My question is how can I sort the 100 random numbers in ascending order?  Here's my program so far, any tips would be great help.  Thanks in advance.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>


int main(void)
{

   int var [ 100 ];
   int x=0;
   int n=0, temp;
   srand (time(NULL));

   for (int x = 0; x < 100; x++)
   {       
      var [ x ]=rand () % 199 + 600 ;
      printf ("%10d", var [ x ]);
   }
   printf ("\n\n\n");


   for (int x = 0; x < 100; x++)
   {
   if ( var[ x ] > var[ x + 1 ])
      {
      
         temp = var[ x ];   
         var[ x ] = var[ x + 1];
         var[ x + 1 ] = temp;
         n++;
      }
      if ( x % 10 == 0)
      {
         printf("%10d", var[ x ] );          
      }
      else
      {   
         printf("%10d", var[ x ] );
      
      }
      //printf ("%10d", var [ x ]); // printf("\n\n");
   }

   printf ("\n\n");
   return (0);
}
   
« Last Edit: August 25, 2011, 06:00:11 AM by rudametc »

CommonTater

  • Guest
Re: How can I sort an array of random numbers?
« Reply #1 on: August 25, 2011, 06:16:54 AM »
There are a large number of algorythms for sorting values in an array, and almost as many tutorials.  A little Google time should get you hooked up with both tuturials and examples...

This will be as good a place to start as any...

http://www.cprogramming.com/tutorial/computersciencetheory/sorting1.html

There might be a problem in your existing code... When  you creat an array in C the indexing starts at 0, not 1, thus an array of 100 elements has valid indexes from 0 to 99 ... if you look at your sort, you will find that your  loop is taking you to 99 correctly but you are testing element x+1 which would be out of bounds. 

Moreover, you're going to have to run that loop more than once to complete the sort which generally involves something like...
Code: [Select]
int max;
int temp;

for (x = 99; x > 0; x--)
  { max = x;
     for (y = 0; y < x; y++)
       if (array[y] > array[max])
         max = y;

     if (max != x)
       {  temp = array[max];
          array[max] = array[x];
          array[x] = temp; } }

The inner y loop locates the highest number in the array, up to the current position of  the x index... that number is swapped with the one at the x index, x is decremented and the y loop runs again... until x = 1...

This is untested so if you find a glitch I'm going to claim innocense  ;D





« Last Edit: August 25, 2011, 06:32:00 AM by CommonTater »

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: How can I sort an array of random numbers?
« Reply #2 on: August 25, 2011, 06:42:38 AM »
I have a program for homework that says:
Write a program that fills an integer arrar with 100 random numbers with the values from 600 to 799.  Print these numbers 8 per line, skip two lines, then sort the numbers in ascending order.

My question is how can I sort the 100 random numbers in ascending order?  Here's my program so far, any tips would be great help.  Thanks in advance.
Well, as already hinted, nobody will do homework for you on a forum. You get hints at best...

The more precise questions you ask/state what you actual coding problem is, the more likely you will get more precise answers...

Beside the hint that you already for about the sorting, I would suggest you check also the rest of the program if it fits the task description, you are slacking a bit on that part as well...  ;)

Ralf

rudametc

  • Guest
Re: How can I sort an array of random numbers?
« Reply #3 on: August 25, 2011, 07:29:31 AM »
Thx for all the replies, they help out a lot! =D

leospy

  • Guest
Re: How can I sort an array of random numbers?
« Reply #4 on: September 20, 2011, 06:37:21 PM »
when i am running a simple hello world program in Pelles C
the output in the console is coming..
 as ***Process returned 17***

how can i remove this..?? please help..
i am using pelles c in 64bit win 7

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: How can I sort an array of random numbers?
« Reply #5 on: September 20, 2011, 06:53:35 PM »
The process return value is an IDE diagnostic.
To remove it simply run your program outside the IDE.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

leospy

  • Guest
what's wrong with this..,???
« Reply #6 on: September 20, 2011, 06:56:03 PM »
#include<stdio.h>
void main()
{
   int i;
   int a[5]={1,2,3,4,5}
   for(i=0;i<5;i++)
   
   printf("%d",a);

}


i am getting error as..

warning #2181: Incorrect signature for entry-point 'main'; expected 'int __cdecl function(void)' but found 'void __cdecl function(void)'.
c(6): error #2001: Syntax error: expected ';' but found 'for'.
c(6): error #2001: Syntax error: expected ')' but found ';'.
c(6): error #2001: Syntax error: expected ';' but found ')'.
c(6): error #2061: Illegal statement termination.
*** Error code: 1 ***

??????

CommonTater

  • Guest
Re: How can I sort an array of random numbers?
« Reply #7 on: September 20, 2011, 10:24:43 PM »

warning #2181: Incorrect signature for entry-point 'main'; expected 'int __cdecl function(void)' but found 'void __cdecl function(void)'.
c(6): error #2001: Syntax error: expected ';' but found 'for'.
c(6): error #2001: Syntax error: expected ')' but found ';'.
c(6): error #2001: Syntax error: expected ';' but found ')'.
c(6): error #2061: Illegal statement termination.
*** Error code: 1 ***

Read what the errors are telling you... it's no mystery, the compiler is telling you what it doesn't like and you need to fix the problems...

Code: [Select]
#include<stdio.h>
void main()                   <---- 2181 ....   should be   int main (void) 
{
    int i;
    int a[5]={1,2,3,4,5}   <----- 2001 ...  missing semicolon
    for(i=0;i<5;i++)   
       printf("%d",a[i]);

   return 0;  <---- main() returns an error level to Windows
}