cplusplus.com cplusplus.com
cplusplus.com   C++ : Forums : Beginners : 41!
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Forums
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programm...
Articles
Lounge
Jobs

-

question  41!

TheAlliedCentralAxis (23)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <cstdlib>

using namespace std;

void printArray(int anArray[], int arrayLength);
void inputData(int anArray[], int arrayLength);
void selectionSort(int list[], int length);

const int LIST_LENGTH = 20;

int main()
{
	int anArray[LIST_LENGTH];
	inputData(anArray, LIST_LENGTH);
	printArray(anArray, LIST_LENGTH);
	selectionSort(anArray, LIST_LENGTH);
	printArray(anArray, LIST_LENGTH);

	return 0;
}

void printArray(int anArray[], int arrayLength)
{
	for (int index = 0; index < arrayLength; index++)
		cout << "anArray[" << index << "] = " << anArray[index] << endl;
}

void inputData(int anArray[], int arrayLength)
{
	int aNum = 100;

	for (int index = 0; index < arrayLength; index++)
	{
		while (aNum > 50)
			aNum = rand();
		anArray[index] = aNum;
	}
}

void selectionSort(int list[], int length)
{
	int index, smallestIndex, minIndex, temp;

	for (index = 0; index < length - 1; index++)
	{
		smallestIndex = index;

		for (minIndex = index + 1; minIndex < length; minIndex++)
			if (list[minIndex] < list[smallestIndex])
				smallestIndex = minIndex;

		temp = list[smallestIndex];
		list[smallestIndex] = list[index];
		list[index] = temp;
	}
}
I keep getting the number 41 into the array. Why is that?
|
mikeb570 (186)
You have to call srand(time(0)); once before you call rand().

Also, you may or may not realize this, but you will be getting a number between 0 and RAND_MAX. To get a random number within a given range, you need to do something like this:

http://www.cplusplus.com/reference/clibrary/cstdlib/rand.html

( value % 100 ) is in the range 0 to 99
( value % 100 + 1 ) is in the range 1 to 100
( value % 30 + 1985 ) is in the range 1985 to 2014
|
TheAlliedCentralAxis (23)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

void printArray(int anArray[], int arrayLength);
void inputData(int anArray[], int arrayLength);
void selectionSort(int list[], int length);

const int LIST_LENGTH = 20;

int main()
{
	int anArray[LIST_LENGTH];
	inputData(anArray, LIST_LENGTH);
	printArray(anArray, LIST_LENGTH);

	return 0;
}

void printArray(int anArray[], int arrayLength)
{
	for (int index = 0; index < arrayLength; index++)
		cout << "anArray[" << index << "] = " << anArray[index] << endl;
}

void inputData(int anArray[], int arrayLength)
{
	int aNum = 100;
	srand(time(0));

	for (int index = 0; index < arrayLength; index++)
	{
		while (aNum > 50)
			aNum = rand() + static_cast<int>(time(0)) % 100;
		anArray[index] = aNum;
	}
}

void selectionSort(int list[], int length)
{
	int index, smallestIndex, minIndex, temp;

	for (index = 0; index < length - 1; index++)
	{
		smallestIndex = index;

		for (minIndex = index + 1; minIndex < length; minIndex++)
			if (list[minIndex] < list[smallestIndex])
				smallestIndex = minIndex;

		temp = list[smallestIndex];
		list[smallestIndex] = list[index];
		list[index] = temp;
	}
}
Like this?
|
mikeb570 (186)
On line 36, you don't add the seed to it, just do rand() % 100;

So it works now, but you get the same number under 50.

You're logic is a bit messy on line 35. So the first time, the while is true so it runs. If the random number was between 51 and 100, it generates a new number until it gets one under 50. The the while loop never runs again and the whole array is that same number. Just get rid of the while loop and you're good.

Sorry for the hold up, I was a bit puzzled to be honest.

| Last edited on
TheAlliedCentralAxis (23)
I was puzzled too. Thanks! :)
| Last edited on

This topic is archived - New replies not allowed.
Home page | Privacy policy
© cplusplus.com, 2000-2009 - All rights reserved - v2.2
Spotted an error? contact us