Question on using random numbers with array index

Hi,

I'm working on a little program to help me study for my history class and
gets some practice in C++. My question is:

Is it possible to randomly go through an array, but NOT choose the same number twice?

I'm essentially trying to get my program to work so that the questions will appear in a random order, rather than in the order I call them from the array.

My Code:
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <iostream>
#include <string>
using namespace std;


	struct Problem
	{
		string question;
		int answer;
		string f_answ1;
		string f_answ2;
		string f_answ3;
	};


void question(Problem, int &, int &);


int main()
{
	//variables
	const int NUM_QUESTIONS = 16;
	int correct = 0;
	int incorrect = 0;

	//array of Problem structures
	Problem prob_list[NUM_QUESTIONS];


//***********************************************
// Here is where the structures are defined
// with the questions for the quizzer
//***********************************************
	//Problem 1 structure
	prob_list[0].question = "During what years did the civil war take place?";
	prob_list[0].answer = 3;
	prob_list[0].f_answ1 = "1)1841-1845";
	prob_list[0].f_answ2 = "2)1862-1864";
	prob_list[0].f_answ3 = "3)1861-1865";

	//Problem 2 structure
	prob_list[1].question = "Who became President after Lincoln was assasinated?";
	prob_list[1].answer = 3;
	prob_list[1].f_answ1 = "1)Ulysses S. Grant";
	prob_list[1].f_answ2 = "2)Andrew Jackson";
	prob_list[1].f_answ3 = "3)Andrew Johnson";

	//Problem 3 structure
	prob_list[2].question = "Who taught Andrew Johnson how to read?";
	prob_list[2].answer = 1;
	prob_list[2].f_answ1 = "1)His wife";
	prob_list[2].f_answ2 = "2)His father";
	prob_list[2].f_answ3 = "3)His best friend";

	//Problem 4 structure
	prob_list[3].question = "After learning to read, what did Andrew Johnson first study?";
	prob_list[3].answer = 2;
	prob_list[3].f_answ1 = "1)Politics";
	prob_list[3].f_answ2 = "2)Law";
	prob_list[3].f_answ3 = "3)Medicine";

	//Problem 5 structure
	prob_list[4].question = "Which state was the best strategic location for the Union in the Civil War";
	prob_list[4].answer = 3;
	prob_list[4].f_answ1 = "1)Illinoise";
	prob_list[4].f_answ2 = "2)Georgia";
	prob_list[4].f_answ3 = "3)Tenessee";

	//Problem 6 structure
	prob_list[5].question = "After getting involved with the Tenessee State Legislature, Andrew Johnson \nserved in the (blank) for 2 terms.";
	prob_list[5].answer = 2;
	prob_list[5].f_answ1 = "1)Governor of Tenessee";
	prob_list[5].f_answ2 = "2)House of Representatives";
	prob_list[5].f_answ3 = "3)Tenessee State Legislature";

	//Problem 7 structure
	prob_list[6].question = "After serving terms for the House of Representatives, Andrew Johnson \nserved in 2 terms as?.";
	prob_list[6].answer = 2;
	prob_list[6].f_answ1 = "1)Senate";
	prob_list[6].f_answ2 = "2)Governor of Tenessee";
	prob_list[6].f_answ3 = "3)President";

	//Problem 8 structure
	prob_list[7].question = "Which state seceded from the Union in 1861";
	prob_list[7].answer = 3;
	prob_list[7].f_answ1 = "1)Kentucky";
	prob_list[7].f_answ2 = "2)Georgia";
	prob_list[7].f_answ3 = "3)South Carolina";

	//Problem 9 structure
	prob_list[8].question = "When and where was the Confederate States of America formed?";
	prob_list[8].answer = 2;
	prob_list[8].f_answ1 = "1)April 1861, Georgia";
	prob_list[8].f_answ2 = "2)May 1861, Alabama";
	prob_list[8].f_answ3 = "3)May 1861, Texas";

	//Problem 10 structure
	prob_list[9].question = "In what year did the Union devote an entire army to capture Tenessee?";
	prob_list[9].answer = 2;
	prob_list[9].f_answ1 = "1)1861";
	prob_list[9].f_answ2 = "2)1862";
	prob_list[9].f_answ3 = "3)1863";
	
	//Problem 11 structure
	prob_list[10].question = "In what year did the Union capture Tenessee?";
	prob_list[10].answer = 3;
	prob_list[10].f_answ1 = "1)1863";
	prob_list[10].f_answ2 = "2)1865";
	prob_list[10].f_answ3 = "3)1864";

	//Problem 12 structure
	prob_list[11].question = "Who did Lincoln appoint as \"War Governor\" of Tenessee?";
	prob_list[11].answer = 3;
	prob_list[11].f_answ1 = "1)Ulysses S. Grant";
	prob_list[11].f_answ2 = "2)Henry Davis Winter";
	prob_list[11].f_answ3 = "3)Andrew Johnson";

	//Problem 13 structure
	prob_list[12].question = "Who was Lincolns running mate in the 1864 election?";
	prob_list[12].answer = 1;
	prob_list[12].f_answ1 = "1)Andrew Johnson";
	prob_list[12].f_answ2 = "2)Robert E. Lee";
	prob_list[12].f_answ3 = "3)Benjamin Wade";
	
	//Problem 14 structure
	prob_list[13].question = "Why did Henry Winter Davis oppose slavery?";
	prob_list[13].answer = 1;
	prob_list[13].f_answ1 = "1)He believed it created an anti-democratic form of government in the South";
	prob_list[13].f_answ2 = "2)He felt that slavery was morally wrong";
	prob_list[13].f_answ3 = "3)He simply went along with everyone else to preserve face";

	//Problem 15 structure
	prob_list[14].question = "Who was the Senatorial leader of the Radical Republicans?";
	prob_list[14].answer = 2;
	prob_list[14].f_answ1 = "1)Henry Winter Davis";
	prob_list[14].f_answ2 = "2)Charles Sumner";
	prob_list[14].f_answ3 = "3)Thadeus Stevens";

	//Problem 16 structure
	prob_list[15].question = "Who was the Senatorial leader of the Radical Republicans?";
	prob_list[15].answer = 2;
	prob_list[15].f_answ1 = "1)Henry Winter Davis";
	prob_list[15].f_answ2 = "2)Benjamin Wade";
	prob_list[15].f_answ3 = "3)Thadeus Stevens";


	
	//This is where my dilemma comes into play
	for (int index = 0; index < NUM_QUESTIONS; index++)
	{
                //I want to try and go through "prob_list[index]" randomly
                //but not repeat any of the problems I may have already done
		question(prob_list[index],correct,incorrect);
	}








	return 0;
}

void question(Problem p, int &c, int &ic)
{

	int user_answ;
	
	cout << "History Quizzer\n\n";
	cout <<"Question:\n";
	cout << p.question << endl;
	cout << p.f_answ1 << endl;
	cout << p.f_answ2 << endl;
	cout << p.f_answ3 << endl;
	do
	{
		cout << "Choose 1 2 or 3\n";
		cin >> user_answ;

		if ( user_answ == p.answer)
		{
			cout << "Correct!\n";
			c++;
		}
		else
		{
			cout << "Incorrect, the answer was " << p.answer << endl;
			ic++;
		}
	}
		while(user_answ < 1 || user_answ > 3);
		system ("pause");
		system ("cls");
}


For example: Say I randomly generate the number 5 as the index for prob_list[index],I'd like to set it up so that 5 will not occur again during any of the random number generations, this would eventually continue on until there are no more questions to answer.

If anyone could point me in the right direction on this one, I'd be really grateful.

Thanks in advance.
Shuffle the array instead. It'll be easier to randomize the array's elements and then go through it. However, that will change the element numbers that your elements are bound to.
If you want to do something else.... you could always create an array that contains the indices in question, in the order that they will be covered. Then shuffle that instead.
Last edited on
Thanks, I'll give it a go.
Topic archived. No new replies allowed.