Assignment # 06: National University of Computer and Emerging Sciences
Assignment # 06: National University of Computer and Emerging Sciences
Assignment # 06
Programming Fundamentals
Fall 2021
Deadline
02 January 2022
Write a C++ program for the following problems. Follow an efficient approach to solve
each problem and your code should be readable and/or commented (e.g. add comments
for readability). Printed/handwritten assignments will get you ZERO marks.
Problem Statements
Task-1 /15
1. Write a function that will return whether the number is Perfect or not (bool isPerfect() ). /4
2. Write a function that will count numbers of zeros and non-zeros digits in a number. /5
a. Note: Function will not print anything.
3. Write a function that will return whether the password is strong or not. /6
a. Strong passwords have four characteristics (upper case, lower case, digit, and
Special Character).
Task-2: /10
Write two functions having the same name Add and both take two parameters. When called
from main, they will print “I have been called n times” where n is the number of calls, they have
received from main, and will return the sum of arguments passed.
Note: Keep taking input until the user enters -1.
Example
Output: Add(12,12);
Sum = 24, I have been called 1 time
Add(45,1254);
Sum = 1299, I have been called 2 times
Add(45.89,12.45);
Sum = 58.34, I have been called 1 time
Add(1,15);
Sum = 16, I have been called 3 times
(This was dummy output; you are supposed to take input from the user and add them until the
user enters -1)
Page 2 of 7
National University
of Computer & Emerging Sciences-Faisalabad
Task-3: /15
Given an array A of length N should be greater than 0 and less than 100, your task is to find the element
which repeats in A maximum number of times as well as the corresponding count. In the case of ties, choose
the smaller element first. Details of the input are below:
Input :
The first line of input contains an integer T, denoting the number of test cases. Then follows a description of
T cases. Each case begins with a single integer N, the length of A. Then following N space-separated
integers in next line.
Output:
For each test case, output two space-separated integers V & C. V is the value that occurs maximum
number of times and C is its count.
Example
Input:
2//test case
1 2 3 2 5 //first array
122112
Output:
22
13
Note:
Don’t get confuse with multiple test cases, you just need to repeat the working in same array for
each test case.
Page 3 of 7
National University
of Computer & Emerging Sciences-Faisalabad
Task-4: /10
Write a program that uses a two-dimensional array to store the highest and lowest
temperatures for each month of the year. The program should output the average high, average low,
and the highest and lowest temperatures for the year. Your program must consist of the following
functions
1. Function getData: This function reads and stores data in the two-dimensional array
2. Function avg_High: This function calculates and returns the average high temperature for
the year
3. Function avg_Low: This function calculates and returns the average low temperature for the
year
4. Function indexHigh_Temp: This function returns the index of the highest high temperature
in the array
5. Function indexlow_Temp: This function returns the index of the lowest low temperature in
the array
Task-5: /5
Tell while defining function with parameter of Two D-array which of the two rows or columns have
to be (compulsory) mentioned while defining the function and why, explain?(attach the handwritten snap.
Use diagrams if needed)
Or
Task-6: /12
There are n bikes, and each can cover 100 km when fully fueled. What is the maximum amount of
distance you can go using n bikes? You may assume that all bikes are similar, and a bike takes 1
litre to cover 1 km. You have n bikes and using one bike you can only cover 100 km. so if n bikes
start from the same point and run simultaneously you can go only 100 km. Let’s think a bit
differently, trick is when you want to cover the maximum distance, you should always try to waste
minimum fuel. Minimum wastage of fuel means to run a minimum number of bikes. Instead of the
Page 4 of 7
National University
of Computer & Emerging Sciences-Faisalabad
parallel running of n bikes, you can think of serially running them. That means if you transfer some
amount of fuel from the last bike to other bikes and throw the last bike i.e., don’t run the last bike
after a certain point. But the question is, after what distance the fuel transfer must be done so that
the maximum distance is covered, and the fuel tank of the remaining bikes do not overflow.
Let us take the following base cases and then generalize the solution.
• Base Case 1: There is one bike: This is simple, we can cover 100 km only.
• Base Case 2: There are two bikes: What is the maximum distance we can cover when there
are 2 bikes? To maximize the distance, we must drop the second bike at some point and transfer
its fuel to the first bike. Let us do the transfer after x km.
Total distance covered = Distance covered by 100 ltr in first bike + Distance covered by fuel
transferred from the first bike.
The remaining fuel in the second bike is 100 – x. If we transfer this much fuel to the first bike, then
the total distance would become 100 + 100 – x which is 200 – x. So, our task is to maximize 200-x.
The constraint is, 100 – x must be less than or equal to the space created in the first bike after x km,
i.e., 100 – x <= x. The value of 200 - x becomes maximum when x is minimum. The minimum
possible value of x is 50. So, we can travel 150 km.
• Base Case 3: There are three bikes: Let the first transfer is done after x km.
After x distance, all bikes contain the 100 - x amount of fuel. If we take 100 - x amount of fuel
from 3rd bike and distribute it among 1st and 2nd bike so that fuel tanks of 1st and 2nd bikes get
full. So 100-x <= 2*x; or, x=33.333 so we should transfer the remaining fuel of the third bike and
distribute that amount of fuel among the 1st and 2nd bike after exactly 33.33 km. Let us generalize
it. If we take a closer look at the above cases, we can observe that if there are n bikes, then the first
transfer is done (or a bike is dropped) after 100/n km. To generalize it more, when we have x litre
remaining fuel in every bike and n remaining bikes, we drop a bike after x/n km.
Example Output:
The maximum distance possible with 5 bikes is 1141.666667.
Task-7: /8
Write a function that finds whether the 3x3 matrix is symmetric or not. The function will not print
any output.
Additional function: Function to take input.
Page 5 of 7
National University
of Computer & Emerging Sciences-Faisalabad
Task-8: /15
Write a program that will take a Month as input and print the calendar for the month of 2022. Your
input will be 1 for January, 2 for Feb and so on. You have to write a function getDays(month) to get
the number of days for this specific month. First month (January) will be start from Friday and it
will end on Sunday so next month February will start from Monday.
Hint: It is a very simple program so think simply (for use of % operator, “/t”)
Page 6 of 7
National University
of Computer & Emerging Sciences-Faisalabad
Guidelines
• A single violation of guideline will lead to Zero mark in your assignment.
• Make a word file and put all your codes with proper screenshots in it.
• The Questions should be in order, otherwise -5.
• Prepare a Word document which contains: each question’s source code
(copy/paste your written code) and screen shot of its output.
• Word file naming format should be ‘Rollno_Section_A#6’ and submit this
document on Slate/Google Classroom.
• You can submit your assignment late, within three days after your due date
with 30% marks deduction per day.
• You will have maximum marks if you have done the entire task.
• Deadlines should be kept in mind no extension in assignment dates.
• This is an individual assignment. PLAGARISM IS NOT ACCEPTABLE!
• Follow the instructions as it is, otherwise your assignment would not
be accepted at all.
Page 7 of 7