Reputation: 3
I need to create a loop to generate a random number 1-75 and output BINGO based on the number, and complete only whenever all the letters are output.
have not been able to come up with a loop
/*
Section 1 : Generate a random number from 1 to 75.
Section 2 : If the number is between 1 to 15, attach B and output the number.
Section 3 : If the number is between 16 to 30, attach I and output the number.
Section 4 : If the number is between 31 to 45, attach N and output the number.
Section 5 : If the number is between 46 to 60, attach G and output the number.
Section 6 : If the number is between 61 to 75, attach O and output the number.
Section 7 : Repeat Sec 1 through 6 until the program generates all B I N G O.
*/
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
int B1;
bool B = false, I = false, N = false, G = false, O = false, bingo = false;
srand(time(NULL)); // This line set the random number seed.
int num = (rand() % 75) + 1;
if (num <= 15)
{
cout << "B-" << num << endl;
B = true;
}
else if (num <= 30)
{
cout << "I-" << num << endl;
I = true;
}
else if (num <= 45)
{
cout << "N-" << num << endl;
N = true;
}
else if (num <= 60)
{
cout << "G-" << num << endl;
G = true;
}
else if (num <= 75)
{
cout << "O-" << num << endl;
O = true;
}
return 0;
} // main
I am not sure where to go or how to proceed in creating the loop. This is as far as I have gotten.
Upvotes: 0
Views: 428
Reputation: 118
As per my understanding, you want to continue the loop till you fill all the letters of BINGO, so I have added a while loop with that condition. It stops when all the letters are found.
Section 1 : Generate a random number from 1 to 75.
Section 2 : If the number is between 1 to 15, attach B and output the number.
Section 3 : If the number is between 16 to 30, attach I and output the number.
Section 4 : If the number is between 31 to 45, attach N and output the number.
Section 5 : If the number is between 46 to 60, attach G and output the number.
Section 6 : If the number is between 61 to 75, attach O and output the number.
Section 7 : Repeat Sec 1 through 6 until the program generates all B I N G O. */
#include<iostream>
#include<ctime>
using namespace std;
int main() {
int B1;
bool B = false, I = false, N = false, G = false, O = false, bingo = false;
srand(time(NULL)); // This line set the random number seed.
while (!(B && I && N && G && O)) {
int num = (rand() % 75) + 1;
if(num <= 15) {
cout << "B-" << num << endl;
B = true;
} else if (num <= 30) {
cout << "I-" << num << endl;
I = true;
} else if (num <= 45) {
cout << "N-" << num << endl;
N = true;
} else if (num <= 60) {
cout << "G-" << num << endl;
G = true;
} else if (num <= 75) {
cout << "O-" << num << endl;
O = true;
}
}
return 0;
} // main
Upvotes: 1
Reputation: 463
Add a while
loop with your flag bingo
. At the end of loop do check if we got all the
Letters.
#include<iostream>
#include<ctime>
using std::cout;
using std::endl;
int main()
{
int B1;
bool B = false, I = false, N = false, G = false, O = false, bingo = false;
srand(time(NULL)); // This line set the random number seed.
while (!bingo)
{
int num = (rand() % 75) + 1;
if (num <= 15)
{
cout << "B-" << num << endl;
B = true;
}
else if (num <= 30)
{
cout << "I-" << num << endl;
I = true;
}
else if (num <= 45)
{
cout << "N-" << num << endl;
N = true;
}
else if (num <= 60)
{
cout << "G-" << num << endl;
G = true;
}
else if (num <= 75)
{
cout << "O-" << num << endl;
O = true;
}
bingo= B && I && N && G && O;
}
return 0;
} // main
Upvotes: 0