0% found this document useful (0 votes)
6 views16 pages

CPL Assignment 2

Uploaded by

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

CPL Assignment 2

Uploaded by

anasjadoon31
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ASSIGNMENT # 02

COMPUTER PROGRAMMING LAB

NAME: MUHAMMAD ANAS


ENROLLMENT NO: 02-134242-068
BSCS 1-B

SUBMITTED TO: MISS MEHWISH SALEEM


CPL: ASSIGNMENT # 02 | Muhammad Anas

COMPUTER PROGRAMMING LAB


ASSIGNMENT # 02

TASK 1:-
Assume that you are developing for a World Travel Planner program in C++ that
caters to globetrotters. The program should assist users in planning their international
travels by allowing them to convert distances from miles to kilometers. Users will input the
distance in miles, and the program will output the converted distance in kilometers,
considering the international metric system commonly used in many countries.
1 mile = 1.60934 kilometers

Solution:-
• Flowchart:-

• Program:-

2
CPL: ASSIGNMENT # 02 | Muhammad Anas

• Output:-

• Explanation:-
In this problem, we were basically assigned to convert miles into kilometers.
For that we used simple multiplication. As 1 mile = 1.60934 kilometers, therefore we
multiplied it with the miles we got through input from the user. We used data type
float to store the values so that the decimals work properly.

TASK 2:-
Asad is managing his bank account and intends to carry out two transactions.
Initially, he plans to withdraw Rs. 2000 in cash, followed by a deposit of Rs. 10,000 into his
account. Your task is to create a simple C++ program to facilitate these transactions. Display
the account balance after each transaction. Write a C++ program to realize the specified
scenario.

Solution:-
• Flowchart:-

3
CPL: ASSIGNMENT # 02 | Muhammad Anas

• Program:-

• Output:-

• Explanation:-
In this program, we have used basic output function cout to display the
transactions carried out by the user. We assumed initial balance as Rs. 0 and used
simple math for calculating the remaining balance after each transaction. We used
assignment operator += to increment the deposited balance in the bank balance.

TASK 3:-
Write a program that takes a date as input and prints the day of the week that date
falls on. Your program should take three input parameters: m (month), d (day), and y (year).
For m, use 1 for January, 2 for February, and so forth. Output will be 0 for Sunday, 1 for
Monday, and so forth. Use the following formulas, for the Gregorian calendar:

4
CPL: ASSIGNMENT # 02 | Muhammad Anas

Solution:-
• Flowchart:-

• Program:-

• Output:-

• Explanation:-
This program is about calculating the day of the week according to the date
that is being input by the user. We have used the formula that was provided in the
task for that purpose.

5
CPL: ASSIGNMENT # 02 | Muhammad Anas

TASK 4:-
Write a program that prints a big letter of your name’s initial using the same letter.
Sample output for letter H is shown below.

Solution:-
• Flowchart:-

• Program:-

6
CPL: ASSIGNMENT # 02 | Muhammad Anas

• Output:-

• Explanation:-
We have simply used cout function to create the output by arranging the
alphabets in the desired order. We also used endl manipulator to move to the next
line while formatting the output.

TASK 5:-
Excel Laboratories is implementing a new system for generating bills at its lab. As a
software developer, you have been assigned the task of creating a program that will help
generate detailed receipts for the patients. The Lab provides various medical services, and
the billing system needs to be able to handle different types of services with appropriate
fees.
The program should prompt the user to enter the following information for each
medical service provided at the clinic:
SERVICE DESCRIPTION (E.G., MRI, CT SCAN, BLOOD TEST, X-RAY)
QUANTITY (FOR SERVICES LIKE BLOOD TESTS OR X-RAYS)
SERVICE FEE PER UNIT

Calculate the total cost for each medical service (quantity * service fee per unit). Also
Apply 5% tax on each bill. Print Receipt which display all the information.
Sample Input:

Output:

7
CPL: ASSIGNMENT # 02 | Muhammad Anas

Solution:-
• Flowchart:-

8
CPL: ASSIGNMENT # 02 | Muhammad Anas

• Program:-

9
CPL: ASSIGNMENT # 02 | Muhammad Anas

• Output:-
This input will be taken from the user:

The receipt that will be generated based on the input will be:

• Explanation:-
In this task, we have used three new header files, <string>, <vector> and
<iomanip>. These header files are used for the following purposes:

<vector> To store multiple values dynamically


<iomanip> To format the output
<string> To perform string operations

This program contains some logics that maybe are above basic levels,
therefore I will be explaining the whole program.

First of all, we created the variables with string, int, double, char data types.

10
CPL: ASSIGNMENT # 02 | Muhammad Anas

Then we created the variables of data type <vector> to store strings and
integers dynamically. Vectors are like arrays that can store multiple values of the
same data types, but the main difference is that vectors can change their size
according to the elements added.

Then we used the do while loop to take multiple inputs if the user wants. We
used do while loop because we wanted to run the program at least one time, as do
loop checks the condition after running the loop at least one time.

Then we used the push_back() function of vector to store the data in the
vectors.

Inside loop, we used basic cin and cout functions to take input and display
the commands. We also used the formula totalServiceCost = quantity * serviceFee to
calculate the total cost of a service. We put this formula in the loop to calculate the
costs for each service separately. We also used [Link]() function to ignore any
previous input stuck in the input stream.

At the end of the loop, we gave the while condition to ask the user if he or
she wants to add any other services. We used toupper function with while condition
so that if user inputs “Y” in either lower or upper case, the condition should work.

After the loop, we calculated grand total with the formula grandTotal =
totalBill + tax and applied 5% tax using formula tax = totalBill * 0.05.

Then we displayed the receipt on the basis of inputs user gave. We used the
manipulator fixed so that we don’t get values in scientific notations and the function
precision() so that we only see two digits after decimal point. This helps us to keep our
receipt clean.

We then generated for loop to print all the services used, with their
respected fee and total fee. We used the manipulator left and setw() to make our receipt
look clean and nice.

After that we printed the sub total, tax and then finally grand total.

TASK 6:-
Write a program to convert a given time in seconds into hours, minutes, and
remaining seconds.
Hint: Use the modulus operator to perform the necessary calculations.

11
CPL: ASSIGNMENT # 02 | Muhammad Anas

Sample Output:

Solution:-
• Flowchart:-

• Program:-

• Output:-

12
CPL: ASSIGNMENT # 02 | Muhammad Anas

• Explanation:-
In Task 6 we built a time converter that is taking time in seconds as an input
from the user using the function cin and then converting time from seconds to the
format of hours, minutes, seconds. We used the following formulas for the respected
components of time:
HOURS = SECONDS / 3600
MINUTES = (GIVEN SECONDS % 3600) / 60
SECONDS = (GIVEN SECONDS % 3600) % 60

We then printed the results using standard cout function.

TASK 7:-
In the context of software development, you have been assigned the task of creating
a "Number Guessing" game in C++. The objective of the game is to entertain users through a
challenging guessing exercise. The program is designed to randomly generate a number
within a predefined range (1 to 100), prompting users to make an attempt to accurately
guess this number.
Sample Outputs:

13
CPL: ASSIGNMENT # 02 | Muhammad Anas

Solution:-
• Flowchart:-

14
CPL: ASSIGNMENT # 02 | Muhammad Anas

• Program:-

• Output:-

15
CPL: ASSIGNMENT # 02 | Muhammad Anas

• Explanation:-
In this task we used two new header files, <cstdlib> and <ctime>. These
header files serve the following purposes:

<cstdlib> It contains functions srand() and rand()


to generate random numbers
<ctime> It contains functions regarding time

First of all, we used the function srand() to generate the seed for the random
numbers. For that purpose, we used the current time using the function time(0)
which generates the seed on the basis of time in seconds. The purpose of using
srand() is to make sure that the random digits are not repeated every time the
program is run. As the random number can also be negative, therefore we used the
function static_cast<unsigned int> to explicit (intentionally) typecast (convert) from
negative to positive integer. Another reason for using unsigned int is that the
function srand() expects that data type.

After that we used the function rand() to generate the random numbers and
used the formula secretNumber = rand() % 100 + 1 to keep the number between 1
and 100.

Then we used do while loop to keep the program running until user guesses
the correct number. We used if else condition in the loop to give hints to the user.

THE END

16

You might also like