0

So I'm making a selection sort program where I have to input two values: one for the numbers to use in the array, and the seed to use for the random number generator. I'm just a little confused on how to adjust the number used, since the maximum number of elements we can put in is 15. The array currently has 8. Each element should be a number between 20 and 40.

#include <iostream>
using namespace std;

int selectionSort(int[], int);

int selectionSort(int numbers[], int numbersSize) {
int i = 0;
int j = 0;
int indexSmallest = 0;
int temp = 0;  // Temporary variable for swap

for (i = 0; i < numbersSize - 1; ++i) {

// Find index of smallest remaining element
indexSmallest = i;
for (j = i + 1; j < numbersSize; ++j) {

if (numbers[j] < numbers[indexSmallest]) {
indexSmallest = j;
}
}

// Swap numbers[i] and numbers[indexSmallest]
temp = numbers[i];
numbers[i] = numbers[indexSmallest];
numbers[indexSmallest] = temp;
}
return indexSmallest;
}

int main() {
int numbers[] = {10, 2, 78, 4, 45, 32, 7, 11, 0, 0, 0, 0, 0, 0, 0};
const int NUMBERS_SIZE = 15;
int i = 0;
int nums = 0;
int seed = 0;

cin >> nums;
cin >> seed;

srand(seed);

for (int i = 0; i < nums; i++) {
numbers[i] = (rand() % 20) + 20;
}

cout << "UNSORTED: ";
for (i = 0; i < nums; ++i) {
cout << numbers[i] << " ";
}
cout << endl;

selectionSort(numbers, nums);

cout << "SORTED: ";
for (i = 0; i < nums; ++i) {
cout << numbers[i] << " ";
}
cout << endl;

system("pause");
return 0;
}

The only problem is that the random numbers being generated are the wrong numbers. For example, when I input "10 100" (10 for the numbers being used in the array and 100 for the seed), it outputs

UNSORTED: 25 36 35 24 24 34 38 22 29 29

SORTED: 22 24 24 25 29 29 34 35 36 38

when it should be outputting

UNSORTED: 28 28 34 37 36 27 33 36 36 39

SORTED: 27 28 28 33 34 36 36 36 37 39

I'm not really sure how to fix this.

Ilya
  • 4,583
  • 4
  • 26
  • 51
John Smith
  • 19
  • 5
  • 4
    Why do you think srand(100) should produce 28 as the first number? I think it depends on the compiler. https://stackoverflow.com/questions/3503825/does-stdlibs-rand-always-give-the-same-sequence – Jerry Jeremiah Sep 26 '18 at 05:27
  • 3
    How can random values be right or wrong? – P.W Sep 26 '18 at 05:35

2 Answers2

2

There is a lot to say about this code:

  • The initial values put into the array numbers[] will of course be overwritten.
  • NUMBERS_SIZE is not used.
  • If you want to be able to specify the array length as input parameter, you should use a std::vector<>, or create the array dynamically, or at least check that the given number of values doesn't exceed the array size.
  • rand() is a random number generator, srand() sets some kind of seed for these random numbers. It is guaranteed that with the same seed you will always get the same sequence of random numbers.
    But, why/how do you expect to know the numbers that will be generated?
  • Indention.
  • Don't do using namespace std;

The answer to your question would be: Don't use random numbers.

Rene
  • 2,466
  • 1
  • 12
  • 18
  • I kind of have to though. My instructions say "The second input should be the seed for the random number generator." – John Smith Sep 26 '18 at 05:38
  • See also: https://stackoverflow.com/questions/28374800/c-srand-does-not-give-same-sequences-of-random-numbers and https://stackoverflow.com/questions/3503825/does-stdlibs-rand-always-give-the-same-sequence In short: if you use the same seed in the **same environment** (i.e. same machine, same library at the same execution period), you will have the same sequence. So it is expected that you have diffent sequences in different environments. – Ilya Sep 26 '18 at 05:46
  • @JohnSmith There is nothing wrong with that, but then you cannot expect to get specific values! – Rene Sep 26 '18 at 13:35
  • @Ilya Thank you for the clarification. I silently assumed that the OP is running the program always in the same environment. – Rene Sep 26 '18 at 13:37
-1

You may try rand function

/* initialize random seed: */ srand (time(NULL)); /* generate random number betwee 1 and 10: */ iSecret = rand() % 10 + 1;

you need also to include 3 libraries

#include <stdio.h> /* printf, scanf, puts, NULL */

#include <stdlib.h> /* srand, rand */

#include <time.h> /* time */

Alex
  • 17
  • 3