Storing random values in array

Aug 5, 2018 at 8:26pm
I want to store my values in myArray but when I print out my array ı get some numbers like -84531868 where is the problem ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  int  myArray[80];
	
	void setGenerateRandNum()
	{
		srand(time(NULL));
		int randX;
		
	
		

		for (int i = 0; i <= getN2(); i++)
		{
			
			randX = rand() % getN2() + 1;
			std::cout << randX << " ";
			myArray[i] = randX;//I want to store my values in myArray butw when ı print out my array ı get some oktadecimal numbers where is the problem ??
			
		
		}

		
	
Last edited on Aug 5, 2018 at 9:02pm
Aug 5, 2018 at 8:40pm
What do you mean by "oktadecimal"? The number you show is not octal since it has an 8 in it. Octal values only have digits from 0 to 7.

Post a complete program that demonstrates the problem.

Aug 5, 2018 at 8:42pm
You do realize the code you showed doesn't show you printing your array, right? How would we know what is wrong?
Also, it's not "oktadecimal" (18?), it's hexadecimal (16).

This is how you print out an array:
1
2
3
4
5
for (int i = 0; i < getN2(); i++)
{
    std::cout << myArray[i] << " ";
}
std::cout << "\n";


Doing std::cout << myArray; just prints the value of the pointer (a memory address).
Last edited on Aug 5, 2018 at 9:55pm
Aug 6, 2018 at 7:22pm
for (int i = 0; i <= getN2(); i++)

What is getN2() it should be i < 80;
Anyways...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <ctime>

using namespace std;

void fill_with_random_values(int a[]) {
	for (int i = 0; i < 80; i++) {
		a[i] = rand() % 80;
	}
}

int main() {
	srand(time(NULL));

	int a[80];
	fill_with_random_values(a);

	for (int i = 0; i < 80; i++) {
		cout << a[i] << endl;
	}
	
	cin.get();
	return 0;
}
Topic archived. No new replies allowed.