-2

My instructor put us in charge of compiling a code where we make a Selection Sort Code in C, like this one I found online,

#include <stdio.h>

int main()
{
   int array[100], n, c, d, position, swap;

   printf("Enter number of elements\n");
   scanf("%d", &n);

   printf("Enter %d integers\n", n);

   for ( c = 0 ; c < n ; c++ )
      scanf("%d", &array[c]);

   for ( c = 0 ; c < ( n - 1 ) ; c++ )
   {
      position = c;

      for ( d = c + 1 ; d < n ; d++ )
      {
         if ( array[position] > array[d] )
            position = d;
      }
      if ( position != c )
      {
         swap = array[c];
         array[c] = array[position];
         array[position] = swap;
      }
   }

   printf("Sorted list in ascending order:\n");

   for ( c = 0 ; c < n ; c++ )
      printf("%d\n", array[c]);

   return 0;
}

Instead of inputting an array of numbers, we have to use the srand() command to generate a random set of numbers.

I have been at it for about 4 hours now and I just can't seem to get it.
Please I really need help with using srand() and rand()

Kara
  • 6,115
  • 16
  • 50
  • 57
Angel Solis
  • 1
  • 1
  • 1
  • 1
    You use [`srand()`](http://en.cppreference.com/w/c/numeric/random/srand) to seed the random number generator (**once**). You use [`rand()`](http://en.cppreference.com/w/c/numeric/random/rand) to actually generate the numbers. I see neither in this code so I'm not sure what has been going on for the last four hours. – WhozCraig Mar 19 '15 at 07:22
  • your instructor, poor guy! – Anil Bhaskar Mar 19 '15 at 08:28

1 Answers1

0

First of all, srand() is used to seed the random number generator. You can generate random numbers with rand()

Read this for more information

in C, how does srand relate to rand function?

You need to use srand() to seed the random number generator ( so that we get different values on each run of the program ) and use rand() for generating the numbers.

Instead of the scanf() , just use array[c] = rand() % 100 ; ( this produces random numbers between 0 and 99 , you can change the 100 to any other integer ). This code can be used as reference

#include <stdio.h>
#include<time.h>                  //  for time()
#include<stdlib.h>                // for rand()
int main()
{
    srand(time(NULL));                       // seeding the random number generator
   int array[100], n, c, d, position, swap;

   printf("Enter number of elements\n");
   scanf("%d", &n);

   printf("Enter %d integers\n", n);

   for ( c = 0 ; c < n ; c++ )
      array[c]=rand()%100;                 // storing a random number between 0 and 100 ( Note that this might produce the same number more than once )

   for ( c = 0 ; c < ( n - 1 ) ; c++ )
   {
      position = c;

      for ( d = c + 1 ; d < n ; d++ )
      {
         if ( array[position] > array[d] )
            position = d;
      }
      if ( position != c )
      {
         swap = array[c];
         array[c] = array[position];
         array[position] = swap;
      }
   }

   printf("Sorted list in ascending order:\n");

   for ( c = 0 ; c < n ; c++ )
      printf("%d\n", array[c]);

   return 0;
}

I'm also adding the links from @WhozCraig 's comment because it will be useful

srand()

rand()

Community
  • 1
  • 1
Arun A S
  • 6,421
  • 4
  • 29
  • 43