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

Assignment 1

The document provides guidelines for an assignment in CS 200 Introduction to Programming course. It includes details about submission format and deadline. It also provides two practice questions - one on time conversion between 12-hour and 24-hour formats and another on defining a structure for triangular prism and its related functions.

Uploaded by

alirazza.10001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Assignment 1

The document provides guidelines for an assignment in CS 200 Introduction to Programming course. It includes details about submission format and deadline. It also provides two practice questions - one on time conversion between 12-hour and 24-hour formats and another on defining a structure for triangular prism and its related functions.

Uploaded by

alirazza.10001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

CS 200 – Introduction to Programming

Assignment 1, Fall 2021


Due Date: Sunday, 17 October 2021, 11:55PM

Please keep in mind the following guidelines:


• Do not share your program code with anyone.
• Do not copy code from the internet.
• If you receive any assistance, mention the part of the code in which you received
assistance.
• You must be able to explain any part of your submitted code.
• Properly comment on your code, so that it’s easy to be evaluated.
• All submissions are subject to automated plagiarism detection.
• You have to submit all the .cpp files containing the source code. Zip all .cpp files into one
file named <rollnumber>_Assignment1_CS200.zip and submit the zip file.
o For example: 23100075_Assignment1_CS200.zip
• Submission Policy:
o 1 day late = 10% deduction
o 2 days late = 15% deduction
o No assignments will be accepted 2 days after the deadline
• For problems and issues, you must join Piazza link mentioned on LMS so that your
queries will be answered there quicker instead of by email.
Prac ce Ques ons:
These ques ons will be included in your post-assignment viva with the TAs

Question #1:
Write a program that converts Military Time into Standard Time, and vice versa. Take input from
the user and do the conversion according to the selected option.

Options:
1. Military Time into Standard Time, enter 1
2. Standard Time into Military Time, enter 2

Military Time representation is a 24-hour clock while regular/Standard Time representation is a


12-hour clock with AM/PM.
Your program should make sure that the specific format for each input is taken. For the 12-hour
clock, it should take hours and minutes along with AM/PM, while for the 24-hour clock it should
only takes hours and minutes. The seconds can be ignored in both representations.

Time Representation
12-hour Time 24-hour Time

12:00 am 00:00

1:00 am 01:00

2:00 am 02:00

3:00 am 03:00

4:00 am 04:00

5:00 am 05:00

6:00 am 06:00

7:00 am 07:00

8:00 am 08:00

9:00 am 09:00
ti
ti
ti
12-hour Time 24-hour Time

10:00 am 10:00

11:00 am 11:00

12:00 pm 12:00

1:00 pm 13:00

2:00 pm 14:00
Question #2:
1. Write a structure called triangularPrism having four oat members variables: hypotenuse,
base, perpendicular, and height.
2. Write se er func ons to set the base, height, and perpendicular separately. The value for
hypotenuse should be calculated and set using Pythagoras’ Theorem.
Note: height, base and perpendicular are non-nega ve oat values, you will have to make sure
of that during input
3. Write ge er func ons for all member variables (func ons that return values of the objects).
4. Write constructors. These should include:

• Default constructor. Use default values of zero for hypotenuse, height, base and
perpendicular.

• Constructor to ini alize base only


• Constructor to ini alize base and hypotenuse both
• Constructor to ini alize hypotenuse and base both
• Constructor to ini alize hypotenuse, base, height and perpendicular
5. Write a member func on Area to print the area of the triangular prism.
6. Write a member func on Volume to print the volume of the triangular prism
7. Write a member func on printPrism to print the measurements of the triangular prisms
7. Declare an array for ten triangular prisms, and ini alize them with measurements input by
the user.
8. Sort the triangular prisms on the basis of their area
9. Print out the sorted triangular prisms using printPrism
Write a main func on that test all the above func ons.
tt
tt
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
fl
ti
fl
Assignment Ques ons:

Question # 1: (20 Marks)


Write a program to calculate the number of mes each element is repeated in the array. You can
assume that the values in the array are between -100 and 100, both inclusive. For example, the
following output should be displayed for the array A = {7, 8, -1, 4, 7, 10, -1, 5, 8, 10, 4, 7, -1}
-1: 3
4: 2
5: 1
7: 3
8: 2
10: 2
The program should be general enough to display the counts of each integer for an array of any
length.
ti
ti
Question # 2: (30 Marks)
Write a program that takes an integer input n from the user and then asks the user to enter n
strings. All the n strings must contain just the English alphabets (may contain both capital and
small le ers).
In case a user enters any other character for a par cular string, the program should ask the user
to enter that par cular string again. A er the user is done entering those n strings, the program
should sort those all n strings alphabe cally (considering only the rst element of the string for
sor ng). Small alphabets have more precedence than capital alphabets, so small alphabets
come rst. Display the sorted output on the screen.
Sample Program:
Enter a value for n: 7
Enter string 1: HaiDer
Enter string 2: BismaH
Enter string 3: aLeEm
Enter string 4: Aleena
Enter string 5: sALman
Enter string 6: Alizeh
Enter string 7: haiSam
The correct order for the strings is:
aLeEm
Aleena
Alizeh
BismaH
HaiDer
haiSam
sALman
ti
fi
tt
ti
ft
ti
ti
fi
Question # 3: (50 Marks)
A:
1. Write a structure called Time, having three integer member variables: seconds, minutes
hours, and one double member variable called totalsecs.
2. Write default constructor, use zeroes for seconds, minutes and hours.
3. Write parameterized contractor to assign values (from user input) to seconds, minutes
and hours.
4. Write a member func on called inputTime that prompts the user to enter a me value
in hours, minutes, and seconds. The format should be like 12:59:59 (each number can be
entered at a separate prompt like “Enter hours:”, “Enter minutes:”, and “Enter
seconds:”). The func on should store the input me in the member variables described
above.
5. Write a member func on called printTime which displays the result in 12:59:59 format.
6. Finally, calculate, store, and print out the total number of seconds represented by this
me value in main():
long totalsecs = t1.hours*3600 + t1.minutes*60 + t1.seconds
7. In main(), create two instances of Time and use inputTime to obtain two me values
from the user in 12:59:59 format (hours-minutes-seconds) and store them in their
respec ve objects.
8. Convert each value into seconds format (step 5), and add the seconds of both Time
values, and store this in the totalsecs of a third Time object.
9. Use the newly-calculated value of totalsecs and convert it back to the hours-minutes-
seconds format, and store the result in the third Time object.
10. Display the result using printTime.
B:
1. Keep the same func onality, but modify the program so that it uses two func ons. The
rst, me_to_secs(), it takes its only argument a structure of type me, and returns the
equivalent me in seconds (type long).
2. The second func on, secs_to_ me(), takes as its only argument a me in seconds (type
long), and returns a structure of type Time.
ti
fi
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
3. Write a main func on that test above func ons.

ti
Question # 4: (70 Marks)
Part B isn’t necessarily an extension of Part A; you’re meant to do both of them separately, so
just write the exact same angle class for Part B (in step 4).
Part A:
1. Suppose you are sailing towards Island XYZ, your exact loca on facing towards XYZ is
158° 45.9’ W, 28°42.6’ S [1]. The marine naviga on system is measured in degrees and
minutes of longitude and la tude.
2. Write a class name angle, that have three data members named degrees, minutes and
direc on. The data type for each member is int, oat and char respec vely.
3. angle can have either la tude or longitude variable, since only one of these is su cient
to describe a loca on in the marine naviga on system.
4. Write a parametrized constructor of class angle for all data members.
5. angle should have a member func on called inputLoca on that takes angle value (in
degrees and minutes) and direc on from user.
• Make sure of the following input constraints:
i. Direc on we have these four char N, W, E and S.
ii. In a degree we have 60 minutes.
iii. To measure la tude we have 0 to 90 degrees.
iv. To measure longitude we have 0 to 180 degrees.
6. Write a member func on called printLoca on to display the input values in the following
format: degrees ° minutes’ direc on
Example: 124° 12.7’ E
7. Write a main() that display the values of an angle object ini alized using inputLoca on.
8. Use a sen nel control loop, that stops when the user enters 9999, otherwise it will
con nue to take in angle values and display them as aforemen oned.
[1] ° for degrees, ’ for minutes, W for west longitude, and S for south la tude
Part B:
1. Write a class named ship.
2. ship should have ship_ID and loca on as its data members.
3. Use the serial number approach (explained below) to assign the ship_ID when a new
ship object is created.
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
ti
fl
ti
ti
ti
ti
ti
ti
ffi
ti
4. For loca on you can use the representa on of the angle class (made in Part A).
5. Write a member func on called inputShipLoca on for ship that takes the loca on of the
ship from the user and store it in ship’s member variable.
6. Write a member func on called printShipDetails to display the ship_ID and loca on.
7. In main(), create three objects of ship class, asks the user to input the loca on of each
ship using inputShipLoca on, and then display each ship’s ID and its current loca on
using printShipDetails.

Serial Number Approach:


When you write a class, you need a data member that holds the ID value for every object
created from that class. Like when the rst object will be created it will be numbered 1, and
for second object it will be 2 and so on.
To make sure of this, you’ll need to have another data member that records the count of the
objects of that class (which details how many objects have been created un l now). So,
every me an object is created, its constructor checks the count global (or sta c) variable for
assigning it an ID number.
ti
ti
ti
ti
ti
fi
ti
ti
ti
ti
ti
ti
ti
ti
Bonus Ques on: (30 Marks)
A company need to enter the record of its employees. For this the company need a class named
employee_data that includes following data members:
First_name (string)
Last_name, (string)
Quali ca on, (string)
Age (int)
Gender (char)
Employee_Id (int)
Employee_type , ???
here the company need a customized data type “enumerated variable“ that help the data entry
operator to type rst le er and it will save the corresponding employee type.
The organiza on have following employee types: laborer, secretary, manager, accountant,
execu ve, researcher and l’, ‘s’, ‘m’, ‘a’, ‘e’, ‘r’ represents them respec vely.
Write main(), using loop save the data of 5 di erent employee, make sure use enumerated/
enum data type to enter the value in Employee_type.
Write a member func on that display the entered record of 5 employees.

Good Luck!
fi
ti
ti
ti
ti
fi
ti
tt
ff
ti

You might also like