Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

GHULAM ISHAQ KHAN INSTITUTE OF ENGINEERING SCIENCES AND TECHNOLOGY

BS Data Science
Computer Programming (CS103) (Fall 2021)

Marks (20)
Assignment #03 (Sol) Semester 1st Submission Date: 20th -Dec-2021

Instructions for solution:


You need to execute your code on IDE such that first line will print your registration number. Copy
your code and executed output in your assignment and submit hardcopy.

#include <iostream>
using namespace std;
int main()
{
cout<< ”your registration number”;
\\ Your code for problem set program.
return 0;
}
Problem set 1: [10]
Write a function in C++ that rolls a pair of dice until the sum of the numbers rolled is a specific
number. We also want to know the number of times the dice are rolled to get the desired sum.
Note that: The smallest number on each die is 1, and the largest number is 6. So the smallest
sum of the numbers rolled is 2, and the largest sum of the numbers rolled is 12. Use the random
number generation approach to randomly generate a number between 1 and 6.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int num, rollDice(int num);
int main()
{
cout << "Enter the number you want the two dice to
equal:"<<endl;
cin >> num;
while(num>12 || num<2)
{
cout<<endl<<"Please Enter valid number between 2 &
12"<<endl;
cin>>num;
}
cout<<endl<<"The number of times the dice are rolled to get
the sum"<< num << " = " <<
rollDice(num)<< endl;
return 0;
}
GHULAM ISHAQ KHAN INSTITUTE OF ENGINEERING SCIENCES AND TECHNOLOGY
BS Data Science
Computer Programming (CS103) (Fall 2021)

Marks (20)
Assignment #03 (Sol) Semester 1st Submission Date: 20th -Dec-2021

int rollDice(int num)


{
int die1;
int die2;
int sum;
int rollCount = 0;
srand(time(0));

do
{
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
sum = die1 + die2;
rollCount++;
}
while (sum != num);
return rollCount;
}

Output:

Problem set 2: [10]


Write a program using functions to determine the number of odds and evens from a given set
of integers enter by user.
The main algorithm will work as:
GHULAM ISHAQ KHAN INSTITUTE OF ENGINEERING SCIENCES AND TECHNOLOGY
BS Data Science
Computer Programming (CS103) (Fall 2021)

Marks (20)
Assignment #03 (Sol) Semester 1st Submission Date: 20th -Dec-2021

1. Call the function initialize to initialize the variables.


2. Prompt the user to enter 20 numbers.
3. For each number in the list:
a. Call the function getNumber to read a number.
b. Output the number.
c. Call the function classifyNumber to classify the number and increment the appropriate count.
4. Call the function printResults to print the final results
Note that: Because the values of the formal parameters in most of desired functions must be
passed outside of the function, these formal parameters must be reference parameters.

// Program: Classify Numbers


// This program reads 20 numbers and outputs the number of
// zeros, odd, and even numbers.
//*************************************************************
#include <iostream>
#include <iomanip>
using namespace std;
const int N = 20;
//Function prototypes
void initialize(int& zeroCount, int& oddCount, int& evenCount);
void getNumber(int& num);
void classifyNumber(int num, int& zeroCount, int& oddCount,
int& evenCount);
void printResults(int zeroCount, int oddCount, int evenCount);
int main()
{
//Variable declaration
int counter; //loop control variable
int number; //variable to store the new number
int zeros; //variable to store the number of zeros
int odds; //variable to store the number of odd integers
int evens; //variable to store the number of even integers
initialize(zeros, odds, evens); //Step 1
cout << "Please enter " << N << " integers."
<< endl; //Step 2
cout << "The numbers you entered are: "
<< endl;
for (counter = 1; counter <= N; counter++) //Step 3
{
getNumber(number); //Step 3a
GHULAM ISHAQ KHAN INSTITUTE OF ENGINEERING SCIENCES AND TECHNOLOGY
BS Data Science
Computer Programming (CS103) (Fall 2021)

Marks (20)
Assignment #03 (Sol) Semester 1st Submission Date: 20th -Dec-2021

cout << number << " "; //Step 3b


classifyNumber(number, zeros, odds, evens); //Step 3c
} // end for loop
cout << endl;
printResults(zeros, odds, evens); //Step 4
return 0;
}
void initialize(int& zeroCount, int& oddCount, int& evenCount)
{
zeroCount = 0;
oddCount = 0;
evenCount = 0;
}
void getNumber(int& num)
{
cin >> num;
}
void classifyNumber(int num, int& zeroCount, int& oddCount,
int& evenCount)
{
switch (num % 2)
{
case 0:
evenCount++;
if (num == 0)
zeroCount++;
break;
case 1:
case -1:
oddCount++;
} //end switch
} //end classifyNumber
void printResults(int zeroCount, int oddCount, int evenCount)
{
cout << "There are " << evenCount << " evens, "
<< "which includes " << zeroCount << " zeros"
<< endl;
cout << "The number of odd numbers is: " << oddCount
<< endl;
} //end printResults

You might also like