CS201 Introduction to Programming Assignment 1 Solution Fall 2014

Assignment Description

In this assignment, you are provided with a small and simple game like program attached with this assignment named as “GuessGame.cpp”. Your job is to slightly modify this code as desired in this assignment.
First, you need to understand the working of the given code and execute it in Dev-C++. This program essentially generates a random number in range [1-10] and asks the user to guess that number. Appropriate messages are displayed after user input to give game like feelings. Important part in this code is the following two lines.
srand(time(NULL));
secretNumber = rand()%10+1;

This is also called pseudo random because the same sequence of random numbers can be re-generated if we use the same seed value. In this code, we have used srand() function with parameter time(NULL) to set the seed value as system current time so that we can have a different random number in each execution. In the second line, rand() function simply generates a pseudo random number in range [0 to RAND_MAX] which is divided by 10 to get the remainder (using % operator) in range [0-9], then we simply add 1 to it to get out secrete number in range [1-10].

Your Task
Your task is to slightly modify this code in order to produce desired working as shown in “Modified_GuessGame.exe”. This is simply taking two inputs from the user as lower and upper ranges. Next, the program shall generate random number in the range specified by the user. Rest of the working remains the same.
Hint: You have to use your mathematical skills in order to shift the range of random number from [1-10] to the one specified by user input [lower – upper].
Screenshot of the desired output:
Submission
You are required to submit your solution through LMS in zip format containing two files.
Your C++ program i.e. .cpp file.
An MS Word document containing screenshot of your program output/execution. You shall type your own VU_ID and that must be visible in the screenshot.

Solution:

#include <iostream>
using namespace std;
main(){
int secretNumber = 0, UpperRange = 0, lowerRange= 0, UserNumber= 0, UserMaxRand=0;

cout”My Student ID is 12345″endl;
cout”please enter the Upper Range””:”;
cin>>UpperRange;
cout”please enter the Lower Range””:”;
cin>>lowerRange;
cout”Computer is calculating a random secret number…”;
UserMaxRand=UpperRange-lowerRange;
srand(time(NULL));
secretNumber = rand()%UpperRange+lowerRange;
cout”Done!”endlendl;

cout”Please guess the secret number in the range [lower range-Upper Range]: “;
cin>>UserNumber;

if(UserNumber > secretNumber){
cout”Ohhh…your entered number not matched…Computer won”endl;
}
else if(UserNumber < secretNumber){
cout”Oooppssss…your entered number not matched …Computer won”endl;
}
else if(UserNumber == secretNumber) {
cout”Wawoooo…Congratulations!! You won”endl;
}
else{
cout”Invalid Input”endl;
}
cout”\n\nSecret Number was: “secretNumberendlendl;
system(“pause”);
}

DOWNLOAD SOLUTION HERE
loading...