Array of random numbers

Hi,
I seem to be having a problem with my code and I’m unsure of how to fix it. The first time the program executes, it delivers one number yet the iterations after that, seem to print out more than one. I want it to print one number out per iteration and store the number, so i can output it after (commented out section) but it seems to not be storing the numbers.

EDIT: Fixed the issue of the numbers printing out more than one - error was within the for loop. The only issue now is when i uncomment the commented out section and execute a mass amount of 0's print out that don't end - how do i get the last part to work?

Thanks in advance.


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
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
int i = 0,size = 0, seed, temp;
int array[size];
char GetNum; 
 
cout << "Enter Seed" << endl;
cin >> seed;

 srand(seed); 
    

while (size <4)
{cout << "Would you like another number?(Y/N)" << endl;
cin >> GetNum; 
	
 if ((GetNum == 'Y'|| GetNum == 'y'))
	{ size++;
  		  for( i=0; i<size; i++)
			{ 
        			array[i] = rand()%13+1; 
         			cout << array[i] << endl;
				}
	}
else if (GetNum == 'N' || GetNum == 'n') {break;}

/*
if ( i > 0 && i < 5 )
	{ cout <<" Your Cards are: \n";
	while (i != 0) 
		{  temp = array[i];
		cout<< temp << ", " ;
			temp = array[i]-1; 
;					 }

cout << endl;} */

}
return 0;
}



output:

Enter Seed
1234
Would you like another number?(Y/N)
y
11 (this is how i want it)
Would you like another number?(Y/N)
y
5
3 (don't want the second number + i want to be able to store the first number to later show both 11 and 5, and so on for the following)
Would you like another number?(Y/N)
y
5
9
9
Would you like another number?(Y/N)
y
12
8
1
2

Last edited on
Line 35 expects the 'i' to change in the loop.
I changed the code so it now looks like this :

1
2
3
4
5
6
7
8
9
10

if ( i > 0 && i < 5 )
	{ cout <<"Your Cards are: \n";
	while (i > 0) 
		{  cout<< array[i] << ", " ;
			i--; 
;					 }

cout << endl;} */


But the output of that is giving me

Your Cards are:
32767,


At least it's outputting something, alas it's not the numbers
Show the current version of whole program, with rational indentation.
Okay, here's the current version.

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
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
	int i = 0,size = 0, seed, temp;
	int array[size];
	char GetNum; 
 
	cout << "Enter Seed" << endl;
	cin >> seed;

 	srand(seed); 
    

	while (size <4)
	{
		cout << "Would you like another number?(Y/N)" << endl;
		cin >> GetNum; 
	
			 if ((GetNum == 'Y'|| GetNum == 'y'))
				{ size++;
			  		  for( i=0; i<1; i++)
						{ 
							array[i] = rand()%13+1; 
				 			cout << array[i]<< endl;
						}
				}
			else if (GetNum == 'N' || GetNum == 'n') {break;}


			if ( i > 0 && i < 5 )
				{ cout <<" Your numbers are: \n";
					while (i > 0) 
						{ 
							cout<< array[i] <<"," ;
							i--;
						}

					cout << endl;
				}

	}
return 0;
}
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
int i = 0,size = 0, seed, temp;
int array[size]; // size==0, so this line requests an int array[0],
// an array that has 0 elements
// Therefore, accessing element array[0] later is an error
// and accessing element array[1] later is an error

while (size <4)
{
	cout << "Would you like another number?(Y/N)" << endl;
	cin >> GetNum; 

	if ((GetNum == 'Y'|| GetNum == 'y'))
	{
		size++;
		for( i=0; i<1; i++)
		{
			// This loop body will always have i==0
			array[i] = rand()%13+1; // updates nonexistent array[0] for every 'size'
			cout << array[i]<< endl;
		}
	}
	else if (GetNum == 'N' || GetNum == 'n') {break;}

	// i==1
	if ( i > 0 && i < 5 )
	{
		cout <<" Your numbers are: \n";
		while (i > 0) 
		{ 
			cout<< array[i] <<"," ; // prints uninitialized and nonexistent array[1] for every 'size'
			i--;
		}
		cout << endl;
	}
}
8
9
	int i = 0,size = 0, seed, temp;
	int array[size];


The variable array is an array of 0 size. Changing the variable size after the fact will not affect the number of elements your array can hold. You've already dictated its limit. (This is not mentioning how variable-sized static arrays are not allowed at least until C++14).

You can instead:
- Create excess storage space in the hopes the user does not exceed the limit
- Use dynamic arrays and reallocate when a larger size is needed
- Take advantage of the containers provided by the standard (which manage those dynamic arrays for you)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//Excess storage space
char stream_buf[80] = {'\0'); //Let's hope 80 is sufficient

//Dynamically sized arrays
int data* = new int[capacity]; //Remember that changing capacity afterward will 
                                              // not change the size limit of the array
unsigned size(0);
//...
if(size >= capacity){
    //1) Allocate memory for new array that has a larger capacity
    //2) Copy data from old array to new array
    //3) Free the memory from the old array
    //4) Update capacity and size information
}

//Containers ~ std::vector
std::vector<int> data; //0 size
data.push_back(rand()%13+1); //size increased by 1


Extra:
If you don't need to store all the numbers, you can use a FILO (First In Last Out) system with a static sized array (with a set size). With this system, each new data point sits at the back (or end) of the array. Whenever the size limit is reached, the oldest data point can be thrown away to make room for the new data point.

If you want to go crazy with different ways to deal with this issue, you can even go for a circular list design, which is like FILO but doesn't involve shifting all the elements.
Topic archived. No new replies allowed.