0% found this document useful (0 votes)
5 views

Lab1 Solution

Uploaded by

sahilpurewal1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab1 Solution

Uploaded by

sahilpurewal1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

CPSC 1103

S10
Lab 1

Sahil Purewal
100460278

May 11, 2024

Lab 1A: Calculate exam percentage


//Prorgram: Lab1A
//Programmer: Sahil Purewal CPSC 1103 Section: S10
//Purpose: Calculate exam percentage.

#include <iostream>
#include <string>

using namespace std;

int main()
{
string userName; //user's name
double examScore; //score obtained on exam
int examMax; //maximum score possible on
exam
double examPercentage; //percentage achieved on exam

//Get input data

cout << "Hi!, Type in your first name and press enter: " << endl;
cin >> userName;
cout << "What score did you get on the exam?" << endl;
cin >> examScore;
cout << "What was the exam out of?" << endl;
cin >> examMax;

examPercentage = examScore / examMax * 100;

cout << endl;


cout << userName << ", your percentage is " << examPercentage << "%." << endl;
cout << endl << endl;
cout << "By: Sahil Purewal CPSC 1103 Sectoin: S10" << endl;

system("pause");
return 0;
//end main

}
Lab 1B: Guessing game
//Program: Lab1B
//Programmer: Sahil Purewal CPSC 1103 Section: S10
//Purpose: Play a guessing game.

#include <iostream>
#include <cstdlib> //for random number generator
#include <ctime> //for time functions, random numbers

using namespace std;

int main()
{
int number; // number of user to try to guess
int guessCount; // number of guesses the user has
made
int userGuess; // the user's guess

srand(unsigned(time(NULL))); // initilize the random number generator


number = rand() % 100 + 1; // pick a ranfom number between 1 and 100

cout << "I picked a number between 1 and 100. Try to guess it!" << endl << endl;

guessCount = 1; // set counter for number of


guesses
userGuess = -1; // set user's first guess to -1 so
loop will start

while (userGuess != number) // loop until user guesses the number


{
cout << "Guess #" << guessCount << ": ";
cin >> userGuess;
if (userGuess < number)
{
cout << "You guessed too low." << endl;
guessCount++;
}
else if (userGuess > number)
{
cout << "You guessed too high." << endl;
guessCount++;
}
} //end loop when user guesses correctly

cout << "You guessed right! It took you " << guessCount << "tries!" << endl;

cout << "By: Sahil Purewal CPSC 1103 Section: S10" << endl;

system("pause");
return 0;

// end main
}
Self reflection:

The overall experience of downloading and installing


visual studio code was very smooth for me . All I did was
follow the instructions given in the pdf. Writing the code
was also not a problem but I did face an issue while
writing the code for Lab1B. I was supposed to write
“number = rand() % 100 + 1;” but I missed the “=”
Because of which the code didn’t work. But I was able to
find this mistake quickly enough and solve it. So, all in all
it was a good learning experience.

You might also like