Latest Activity In Study Groups

Join Your Study Groups

VU Past Papers, MCQs and More

We non-commercial site working hard since 2009 to facilitate learning Read More. We can't keep up without your support. Donate.

CS201 Assignment No.1 Solution & Discussion Due Date: 18/11/2014

Please read the following instructions carefully before submitting assignment.

It should be clear that your assignment will not get any credit if:

 

  • The assignment is submitted after due date.
  • The submitted assignment does not open or file is corrupt.
  • Assignment is copied(partial or full) from any source (websites, forums, students, etc)

 

 

Objective:

 

The objective of this assignment is to provide an on hand experience of:

  • Learn to code, edit, compile and execute C++ program
  • Learn to use decision control (If-Else) in C++
  • Learn to use rand() function for generating random numbers.

Guidelines:

 

  • Code should properly be indented and well commented.
  • Follow C/C++ rules while writing variable names, function names etc.
  • Use only Dev-C++ for this assignment. If you are using any other tool then mention it at top of your code file as comments.

 

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.

Views: 14366

Replies to This Discussion

A.o.a....koi mujy bata sakta hai es code mein kia galti hai??

Attachments:

o pai teri merbani das day ki masla wa es code mein......

Attachments:

azahar bhai

ap codes match kro ap ko full solution dia gya hy 

#include <iostream>
using namespace std;
main(){
    int secretNumber = 0, userNumber = 0, lowerLimit=0, upperLimit=0, range=0;
    cout"My Student ID is MC140000000 "endl;
    cout"Please Enter Lower limit: ";
    cin>>lowerLimit;
    cout"Please enter Uper Limit: ";
    cin>>upperLimit;
    cout"Computer is calculating a random secret number in the given range...";
    srand(time(NULL));
    range= ( upperLimit -lowerLimit)+1;
    secretNumber = lowerLimit+int(range * rand()/(RAND_MAX + 1.0));
    cout"Done!"endlendl;
    
    cout"Please guesss the secret number in the range ["lowerLimit" - "upperLimit"]: ";
    cin>>userNumber;
    
    if(userNumber > secretNumber){
        cout"Oooppssss...your entered number is too high...Computer won"endl;
    }    
    else if(userNumber < secretNumber){
        cout"Oooppssss...your entered number is too low...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");
    

Read more at http://vustudents.ning.com/forum/topics/cs201-assignment-no-1-solut...

you have to do one thing add condition
if(upRange > lowRange ) than do all work else print statment upRange must greater than lowRange

Chek it

check it

Attachments:

this is complete totaly running program

Attachments:

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
main()
{
int secretNumber= 8, userNumber = 0,lowerrange=0,higherrange=0;

cout"My Student ID is MC140402680 "endl;
cout"Please give the lower range: ";
cin>>lowerrange;
cout"Please give the higher range:";
cin>>higherrange;

cout"Computer is calculating a random secret number...";
srand(time(NULL));
secretNumber = rand() %higherrange+lowerrange;
cout"Done!"endlendl;



cout"Please guesss the secret number in the range:";
cin>>userNumber;

cout"Computer is calculating a random secret number...";
if(userNumber>secretNumber)
{
cout"Oooppssss...your entered number is too high...Computer won"endl;
}

else if(userNumber < secretNumber){
cout"Oooppssss...your entered number is too low...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");

}

O very Good!

Naina Your program is not working. lower 15 aur upper 20 dey kr dekho zara.

g han bilkul galat hy

Check this

Attachments:

#include <iostream>
using namespace std;
main(){
    int secretNumber = 0, userNumber = 0;
    int maxl = 0 ,minl = 0;
    //Here My personal Roll No.
    cout"My Student ID is BC140400540 "endl;
    //first input
    cout "Enter Minimum Limit : ";
    cin >> minl;
    // second input
    cout "Enter maximum Limit : ";
    cin >> maxl;
    //computer part of caculation logic
    cout"Computer is calculating a random secret number...\n";
    srand(time(NULL));
    secretNumber = rand()% maxl+minl;
    cout"Done!\n"endl;
    //user input for guess number
    cout"Please guesss the secret number in the range ""["minl "-" maxl "] : ";
    cin>>userNumber;
    
    if(userNumber > secretNumber){
        cout"Oooppssss...your entered number is too high...Computer won"endl;
    }    
    else if(userNumber < secretNumber){
        cout"Oooppssss...your entered number is too low...Computer won"endl;
    }         
    else if(userNumber == secretNumber) {
        cout"Wawoooo...Congratulations!...You won"endl;
    }    
    else{
        cout"Invalid Input"endl;        
    }    
    cout"\n\nSecret Number was: "secretNumberendl;
    
    system("pause");
    
}    


RSS

© 2021   Created by + M.Tariq Malik.   Powered by

Promote Us  |  Report an Issue  |  Privacy Policy  |  Terms of Service