a number guessing game how to initialize number in an array??

a number guessing game. Your task is to crack a code which
has been randomly created by the computer. To crack the code, you make guesses and the
computer responds with clues.
• At the beginning, player will be asked to input a number n (an integer value), which
corresponds to the size of an array.
• Then, an array of size n will be created and each array element will be initialized to a
number in range 1 to n.
• Next, the array will be shuffled with the following function:
• Making Guesses
To make a guess of the number sequence, simply fill in a row with digits between 1 – n
separated by a space.
• Using the clues
A “O” symbol means “Right value” and “Right position”.
A “X” symbol means either “Wrong value” or “Wrong position”.
// "randNoArr" is a constant pointer to a dynamically created array
// "size" is the size of the dynamic array pointed by randNoArr
void shuffle( int* const randNoArr, int size )
{
for( int i=0; i<(size-1); i++ )
{
int r = i + (rand() % (size-i));
int temp = randNoArr[i];
randNoArr[i] = randNoArr[r];
randNoArr[r] = temp;
}
}
CCCU – Division of Computer Studies (DCO), City University of Hong Kong
9
• Example output should look like this:
Enter total number: 6
Number Guessing
Enter 6 digits (1-6) separated by a space
-----------------------------------------------------------
Round 1
Enter Guess: 1 2 3 4 5 6
X X X X X X
-----------------------------------------------------------
Number Guessing
Enter 6 digits (1-6) separated by a space
-----------------------------------------------------------
Round 2
Enter Guess: 1 3 2 5 6 4
X X X X O X
-----------------------------------------------------------
Number Guessing
Enter 6 digits (1-6) separated by a space
-----------------------------------------------------------
Round 3
Enter Guess: 3 2 1 4 6 5
O X X X O X
-----------------------------------------------------------
Number Guessing
Enter 6 digits (1-6) separated by a space
-----------------------------------------------------------
Round 4
Enter Guess: 3 4 5 2 6 1
O O O X O X
-----------------------------------------------------------
Number Guessing
Enter 6 digits (1-6) separated by a space
-----------------------------------------------------------
Round 5
Enter Guess: 3 4 5 1 6 2
O O O O O O
-----------------------------------------------------------
Congratulations! You win in 5 steps


These are the work so far, but I don't know how to initialize number in an array.

1
2
3
for(int i= 1 ; i < size ; i++){
		int num1 = i;
	}

How to initialize 1, 2, 3, 4, 5, 6 in the array if the user input 6 or 1, 2, 3, 4, 5, 6, 7, 8 in the array if the user input 8.
Thanks!!



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
58
59
60
#include <iostream>
#include <ctime>
#include <limits>
#include <cstdlib>
using namespace std;

// "randNoArr" is a constant pointer to a dynamically created array
// "size" is the size of the dynamic array pointed by randNoArr
void shuffle( int* const randNoArr, int size )
{
for( int i=0; i<(size-1); i++ )
{
int r = i + (rand() % (size-i));
int temp = randNoArr[i];
randNoArr[i] = randNoArr[r];
randNoArr[r] = temp;
}
}


int main()
{   

	int roundn = 1;
	int size = 0; // intialize to zero so if cin fails, it's within invalid bounds.

	cout << "Enter total number: ";
	cin >> size;

	int* arr = new int[size];
	
	for(int i= 1 ; i < size ; i++){
		int num1 = i;
	}

	cout << "Number Guessing"<<endl;
	cout << "Enter 6 digits (1-6) separated by a space "<<endl;
	cout << "----------------------------------------------------------- "<<endl;
	cout << "Round "<<roundn++<<endl;
	cout << "Enter Guess: ";

	



		for(int i= 0 ; i < size ; i++){
		cin >> arr[i];}


		

		

	
	
    
	// Hold the command window
	system("pause");
	return 0;
}
closed account (o1vk4iN6)
You clearly do not understand the fundamentals, at all.

This, may as well be a pile of turd on your lawn:

1
2
3
4
for(int i= 1 ; i < size ; i++)
{
	int num1 = i;
}


Go read how static scoping works and learn why this does nothing. So that next time you just go copying and pasting stuff off the internet you don't need to make 20 threads to get someone to figure out why the stuff you botched together doesn't work.
Last edited on
Yeah, but I really don't know how to initialize value in an array that I did not know the size(depends on user). I open a 5 space array but how can I put 1 to 5 in it?
If I open a 50 space array but how can I put 1 to 50 in it?

I read the Arrays chapter but I cant find the ans. Thanks!
Last edited on
closed account (o1vk4iN6)
You know the size of the array in this case,

1
2
cout << "Enter total number: ";
cin >> size; // There it is!! 


Or you can use std::vector, which will automatically expand each time it runs out of capacity, where you use the push_back() member function to add more variables, or resize() member function to allocate more memory.
"How to initialize 1, 2, 3, 4, 5, 6 in the array if the user input 6 or 1, 2, 3, 4, 5, 6, 7, 8 in the array if the user input 8."
1
2
3
4
for (int i=0; i<size; i++) //will do this "size" times
{
arr[i] = i+1;
}


This will put arr[0] = 1, arr[1] = 2, arr[2] = 3, etc. If the size is 6, the array will have the numbers 1->6. Remember that it starts from arr[0].
Last edited on
Topic archived. No new replies allowed.