Question

In: Computer Science

Hi, I want to programm a c++ gamr without the using of any array. only by...

Hi, I want to programm a c++ gamr without the using of any array. only by using FUNCTIONS, WHILE LOOPS, FOR LOOPS, IF ELSE, SRADN() and RAND(), BREAK STATEMENT, CIN and COUT. Please help me out.

Solutions

Expert Solution

  • Below is the detailed implementation of the above problem in C++ with code and output shown.
  • For better understanding please read the comments mentioned in the code.
  • The basic idea of the game mentioned below is that player is asked for a range to enter [a, b] then user is prompted to guess a number between the range which computer has generated randomly if player guesses the same number he/she wins otherwise loses.And beside this all the conditions are checked like if player enters invalid range or if player wants to quit the game then also an option is provided.
  • So using Function, while/for loops, if/else, rand() and srand(), break cin, cout this game is designed in a simple manner, for details refer below.
  • CODE:

//include headers
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
//function to generate a random number between given range [a,b]
int rand_int(int a,int b){
//using rand()
int num = (rand()%(b - a + 1)) + a;
return num;
}
//driver function
int main(){
//welcome message
cout<<"Welcome to the game of random numbers\n\n";
//seeding to current time so as to generate a different random number every time
srand(time(NULL));
//Play till player quits the game
while(true){
//ask user to enter a range or to quit
cout<<"Please input a range i.e, [a,b] such that a<=b (or enter both a and b as -1 to quit)\n";
//input range [a, b]
cout<<"Enter a: ";
int a;
cin>>a;
cout<<"Enter b: ";
int b;
cin>>b;
//if user wants to quit then break from loop
if(a==-1 && b==-1){
cout<<"Quitting game!\n";
break;
}
//if range is valid
if(a<=b){
//generate a random number between [a, b]
int num=rand_int(a,b);
//ask player to guess the number between range [a, b]
cout<<"Now guess a number between your provided range(if you guessed the number generated by computer randomly then you win otherwise you lose the game): ";
int input;
//input number form player
cin>>input;
//if guessed number equals random number then player wins.
if(input==num){
cout<<"Yay! you won!\n";
}
//otherwise loses.
else{
cout<<"Oops! You lose!\n";
}
}
//if range invalid then print invalid message and continue
else{
cout<<"Invalid range entered!\n";
}
}
return 0;
}

  • INPUT/OUTPUT:

Welcome to the game of random numbers

Please input a range i.e, [a,b] such that a<=b (or enter both a and b as -1 to quit)
Enter a: 1
Enter b: 5
Now guess a number between your provided range(if you guessed the number generated by computer randomly then you win otherwise you lose the game): 2
Oops! You lose!
Please input a range i.e, [a,b] such that a<=b (or enter both a and b as -1 to quit)
Enter a: 1
Enter b: 5
Now guess a number between your provided range(if you guessed the number generated by computer randomly then you win otherwise you lose the game): 5
Oops! You lose!
Please input a range i.e, [a,b] such that a<=b (or enter both a and b as -1 to quit)
Enter a: 1
Enter b: 5
Now guess a number between your provided range(if you guessed the number generated by computer randomly then you win otherwise you lose the game): 2
Oops! You lose!
Please input a range i.e, [a,b] such that a<=b (or enter both a and b as -1 to quit)
Enter a: 1
Enter b: 5
Now guess a number between your provided range(if you guessed the number generated by computer randomly then you win otherwise you lose the game): 3
Oops! You lose!
Please input a range i.e, [a,b] such that a<=b (or enter both a and b as -1 to quit)
Enter a: 1
Enter b: 5
Now guess a number between your provided range(if you guessed the number generated by computer randomly then you win otherwise you lose the game): 2
Oops! You lose!
Please input a range i.e, [a,b] such that a<=b (or enter both a and b as -1 to quit)
Enter a: 1
Enter b: 5
Now guess a number between your provided range(if you guessed the number generated by computer randomly then you win otherwise you lose the game): 2
Yay! you won!
Please input a range i.e, [a,b] such that a<=b (or enter both a and b as -1 to quit)
Enter a: -1
Enter b: -1
Quitting game!

  • Below are the screenshot attached for the code and input/output for better clarity and understanding.

CODE

INPUT/OUTPUT

So if you still have any doubt regarding this solution please feel free to ask it in the comment section below and if it is helpful then please upvote this solution, THANK YOU.


Related Solutions

I want the code in the following three PHP files without any CSS. Thank you Create...
I want the code in the following three PHP files without any CSS. Thank you Create a Input.php. Create a PHP script that will perform the following tasks: 1. Divide the content area of this page into two sections (Side by Side) 2. On the left side: create a form that accepts the following information from the user a. Text box, Employee Name b. Text box, Employee ID c. Text box, Telephone Number d. Text box, Email Address e. Radio...
Exercises on Arrays –using C++programming 1. You want an array with the numbers 100 – 105....
Exercises on Arrays –using C++programming 1. You want an array with the numbers 100 – 105. In the boxes below, fill in what your array should have. Fill in the index of each element below it. Array Index Open up your editor and write the following program: Declare an array that has the numbers 100 to 105. (How many elements are there in the array?) Print the array. Save your file and test it. Compare your results with your table...
Use Titanic dataset and perform EDA on various columns. Without using any modeling algorithms, and only...
Use Titanic dataset and perform EDA on various columns. Without using any modeling algorithms, and only using basic methods such as frequency distribution, describe the most important predictors of survival of Titanic passengers, e.g. were males or females more likely to survive, were young and rich females more likely to survive than old poor males etc? Submit the  response in a fully "knit" R Markdown file.
Explain this code: The structure used is max heap using array. C++ (i is the index...
Explain this code: The structure used is max heap using array. C++ (i is the index of the element to be deleted) void del(int i) {    int left,right,temp;    arr[i]=arr[n-1];    n=n-1;    left=2*i+1; /*left child of i*/    right=2*i+2; /* right child of i*/    while(right < n)    {        if( arr[i]>=arr[left] && arr[i]>=arr[right] )            return;        if( arr[right]<=arr[left] )        {            temp=arr[i];            arr[i]=arr[left];   ...
The question I have is that I want to draw without having to drag and then...
The question I have is that I want to draw without having to drag and then clicking the button whether I want a line or rectangle. It should work first by hitting either the line or rectangle button and then making a drag on the panel with mouse, then after releasing the drag it creates the graphic. Here is my current code. package Mod1; import java.awt.*; import java.awt.event.*; import javax.swing.*; class paintGUI extends JComponent { // Image in which we're...
c++ I need a code that will fill an array size of 1000, an array of...
c++ I need a code that will fill an array size of 1000, an array of size 2000, and an array size of 10000, with random int values. Basically like this: array1[1000] = filled all with random numbers array2[2000] = filled all with random numbers array3[10000] = filled all with random numbers C++ no need for print
Hi I have an exercise and I want to you to solve it step by step,...
Hi I have an exercise and I want to you to solve it step by step, please.. I found the solution here but it did not have steps .. *Answer the following questions using the information below: Tiger Pride produces two product lines: T-shirts and Sweatshirts. Product profitability is analyzed as follows: T-SHIRTS SWEATSHIRTS Production and sales volume 60,000 units 35,000 units Selling price $16.00 $29.00 Direct material $ 2.00 $ 5.00 Direct labor $ 4.50 $ 7.20 Manufacturing overhead...
Hi , I want some information about age of nanotechnology
Hi , I want some information about age of nanotechnology
How to make an array without setting size?c++ cant figure this out without wanting to make...
How to make an array without setting size?c++ cant figure this out without wanting to make a vector or set a fixed size to the numbers. Prompt the user to ask how many numbers you wish to read. Then based on that fill out the elements of one dimensional array with integer numbers. Then sort the numbers and print the original and sorted numbers in ascending order side by side.
is there any benefits if I make Taq polymerase heat resistant at 100 C°? (not only...
is there any benefits if I make Taq polymerase heat resistant at 100 C°? (not only 94 C°)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT