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

Cp Computer Programming Asst II1

The document outlines the requirements for Assignment II in Computer Programming I (CoSc 1012), including group participation, submission deadlines, and assessment criteria. It specifies that all groups must complete certain questions, while others are assigned based on group parity. Additionally, it includes a list of programming tasks that students must complete, ranging from basic functions to more complex programs involving user input and calculations.

Uploaded by

Kaleab A
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)
4 views

Cp Computer Programming Asst II1

The document outlines the requirements for Assignment II in Computer Programming I (CoSc 1012), including group participation, submission deadlines, and assessment criteria. It specifies that all groups must complete certain questions, while others are assigned based on group parity. Additionally, it includes a list of programming tasks that students must complete, ranging from basic functions to more complex programs involving user input and calculations.

Uploaded by

Kaleab A
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
You are on page 1/ 9

Assignment II.

Computer Programming I (CoSc 1012)

Using

Instruction:
1. From Question 1 to 15 and Question 30 must be done by all groups in all sections
2. Starting from Question 16 even Questions are assigned to even groups and odd Questions are
assigned to odd groups
3. Submission date is June – 8 – 2025
4. All group members must participate in all activities
5. The Assessment: The total mark of the assignment is 30%
6. Each Question must be described clearly using pseudo code on plain paper
7. All codes and implementations will be presented to the instructor by all members of the group
8. If one of the group members miss to answer the questions, the mark will be deducted from the
group
9. If you miss to Submit on time 5 mark will be deducted per day until Submission!!!!

1
1. Not every function we may write has to return a value. Write a function called product which
accepts two integers as arguments (like add and minus) but returns no value. In the function
body the product function should use cout to print the product directly to screen.
2. Write a function called quotient which should take a double argument and an integer
argument. This function should also return a double data type. Now if you provide the
quotient function with the values 5 and 3 say, it should return the value 1.66667 or
thereabouts. Test this by calling the function within a cout statement.
3. Write a calculator program which asks for two numbers and a mathematical operator (repre
sented as a char type. Depending on the operator (+,-, * or /) call the appropriate function and
display the result. Use a switch statement to filter the choices.
4. Write a function named "sum_from_to" that takes two integer arguments, call them "first" and
"last", and returns as its value the sum of all the integers between first and last inclusive. Thus,
for example, cout << sum_from_to(4,7) << endl; // will print 22 because 4+5+6+7 = 22 cout
<< sum_from_to(-3,1) << endl; // will print -5 'cause (-3)+(-2)+( 1)+0+1 = -5 cout <<
sum_from_to(7,4) << endl; // will print 22 because 7+6+5+4 = 22 cout << sum_from_to(9,9)
<< endl; // will print 9
5. Write a function named "enough" that takes one integer argument, call it "goal" and returns as
its value the smallest positive integer n for which 1+2+3+. . . +n is at least equal to goal .
Thus, for example, cout << enough(9) << endl; // will print 4 because 1+2+3+4 9 but
1+2+3<< enough(21) << endl;// will print 6 'cause 1+2+ . . .+6 21 but 1+2+ . . . 5<< enough(-
7) << endl;// will print 1 because 1 -7 and 1 is the smallest // positive integer cout <<
enough(1) << endl; // will print 1 because 1 1 and 1 is the smallest // positive integer
6. Write a function named "digit_name" that takes an integer argument in the range from 1 to 9 ,
inclusive, and prints the English name for that integer on the computer screen. No newline
character should be sent to the screen following the digit name. The function should not return
a value. The cursor should remain on the same line as the name that has been printed. If the
argument is not in the required range, then the function should print "digit error" without the
quotation marks but followed by the newline character.
7. Write a function named "sort3" that takes three floating point arguments, call them "x" , "y" ,
and "z" , and modifies their values, if necessary, in such a way as to make true the following
inequalities: x y z . The function should return no value.

2
8. Write a function named "reverse" that takes as its arguments the following: (1) an array of floating
point values; (2) an integer that tells how many floating point values are in the array. The function
must reverse the order of the values in the array.
9. The function Process (int a, int b, int c, int& s, double& g) computes the sum and average of 3
integer numbers. Write a C++ program that calls the function Process
10. Write a program that computes the annual after-tax cost of a new house for the first year of
ownership. The cost is computed as the annual mortgage cost minus the tax savings. The input
should be the price of the house and the down payment. The annual mortgage cost can be
estimated as 3% of the initial loan balance credited toward paying off the loan principal plus
8% of the initial loan balance in interest. The initial loan balance is the price minus the down
payment. Assume a 35% marginal tax rate and assume that interest payments are tax
deductible. So, the tax savings is 35% of the interest pay ment. Your program should use at
least two function definitions. Your pro gram should allow the user to repeat this calculation
as often as the user wishes.
11. write three functions with same name and different with parameter (int, double, and float).
The function take a value then apply the multiplication operation by number 2.
12. Write a program which draws a square shape with asterisk (*) shown below using nested for
loops. Take size from the user using function

13. Write C++ User-defined Function Types with the following requirement as:
A. Function with no argument and no return value
B. Function with no argument but return value
C. Function with argument but no return value
D. Function with argument and return value
14. Write a temperature conversion program that gives the user the option of converting a value of
temperature to Celsius and Fahrenheit scales of temperature.

3
15. Automatic Teller Machine Write a program that takes balance of a user’s account as input. It
should then ask the user how much amount he wants to withdraw from his account. The
program should take this amount as input and deduct from the balance. Similarly it should ask
the user how much amount he wants to deposit in his account. It should take this amount as
input and add to the balance. The program shall display the new balance after amount has
been withdrawn and deposited.

Note: Your program should have a


check on balance and amount being
withdrawn. Amounts greater than
balance cannot be withdrawn i.e.
balance cannot be negative.

16. Write a program which takes two numbers as input from user and determines which is the
larger of the two numbers. The Program should also tell which of the entered numbers is even
or odd.

17. You need to design a calculator that performs four basic arithmetic operations. You will write
a program that takes two numbers as input from the user. It then displays a list of arithmetic
operations. The user selects one operation and the program displays the result of the
corresponding operation. The program should also display which of the two input numbers is
greater and which is smaller. Use do-while loop. Interaction with the program might look like
the following:

4
18. An aircraft system is designed which checks speeds and monitors various functions of the
aircraft during the flight. Warnings and errors are generated if any fault develops during the
flight. The system is coded in a manner that if 100 is entered as input it indicates “normal
speed range”, if 101 is entered it gives “over-speed warning” indicating that aircraft is over
speeding. If 88 is entered, it indicates “Low Oil Pressure” and if 187 is entered it indicates
“Engine Failure”. Use switch statement to implement this logic.
19. Write a program that takes marks of 10 students as input. It calculates the class average and
displays it on the screen. Interaction with the program might look like this. Use while loop.

20. Write a program that calculates and prints the sum of the even integers from 2 to
user_defined_limit and product of odd integers from 1 to user_defined_limit. Use a do-while
loop that asks the user whether the program should be terminated or not. Also maintain a
count as to how many times the user ran the do-while loop.
21. Write the following program and execute it. What output does the program display? Expand
the shorthand form of expression sum += i-- to show what it actually represents. State the total
number of iterations made by the “for loop”?

22. The factorial of an integer n, written n!, is simply the product of all the integers from 1 to n.
For example 5! = 1*2*3*4*5 = 120. Write a program to calculate the factorial of integers
from 1 to n where n is taken from the user. Your output should look like the sample output
given below. Use setw() operator to format your output. What is the maximum value of

5
factorial that you can calculate for int data type? Is there any difference between the
maximum value of factorial for unsigned and signed int?

23. Write a program which asks the user whether he wants to draw a pattern as shown in the
screenshot below. If user enters “Y” or “y” the program should draw the pattern. If “Q” or “q”
is opted the program should quit. However, if any other alphanumeric input is given to
program the program shall display that the particular input is not recognized. Use nested for
loops in your program. Interaction with the program might look like this:

24. Write a program which takes PIN number of the user as input and then verifies his pin. If pin
is verified the program shall display “pin verified” and “Welcome”; otherwise the program
shall give user another chance. After 4 wrong attempts, the program shall display “Limit
expired” and then exit. Use for loops to implement the logic. Note: 5 random PIN numbers
can be assumed and fed into the program with which input pin is matched.

6
25. Write a program which takes two integers from the user. Implement a function void
swapInts(int, int) that interchanges the two int values passed to it by main() function. Display
the interchanged numbers inside the function. Also display the values of the two integers in
main() function. What do you observe from the output? What happens to the output if you
change the function prototype as follows: void swapInts(int &, int &);
26. Write a program that takes a set of test scores of a student. It then uses two functions calcula-
teSum() and calculateAverage() to calculate the sum and average of the data set. Your
program must use the following functions:
1. A value-returning function, calculateSum(), to determine the sum of the test scores. Use a
loop to read and sum the test scores. The function does not output the sum of the test scores.
This task must be done in the main function.
2. A void function, calculateAverage(), to determine the average of the test scores. The
function does not output the average test score. This task must be done in the main function.
Hint: Use pass by reference for average variable.
27. This exercise designs a program that uses recursion to convert a non-negative integer in de-
cimal format to equivalent binary number. Let x be an integer. We call the remainder of x
after division by 2 the right most bit of x. Thus, the right most bit of 33 is 1 because 33% of 2
is 1 and right most bit of 28 is 0 because 28% is 0.
Suppose we want to find the binary representation of 35. First we divide 35 by 2. The quotient
is 17 and the remainder, right most bit of 35 is 1. Next we divide 17 by 2. The quotient is 8
and remainder, that is right most bit of 17 is 1. Next we divide 8 by 2. The quotient is 4 and
the re- mainder that is the right most bit of 8 is 0. We continue this process until the quotient
is 0.
Thus, to convert an integer “num” in base 10 into the equivalent binary number we first
convert quotient “num/2” into an equivalent binary number, and then append the rightmost bit
of “num” to the binary representation of “num/2”.

Recursive Algorithm :
decToBin is equal to num if num = 0
decToBin is equal to decToBin(num/base, base) followed by
cout << remainder if num >0

7
Write a recursive function void decToBin () having two input parameters num and base and
hence calculate the binary number.
28. Write a function that displays a message indicating the number of times it gets called: For
instance “The function has been called three times”. Write a program that calls this function
10 times from main(). You will need to use a global variable to store the count. A global
variable is declared outside the main() function. Note: All variables declared inside a function
are local variables. Their value is lost once the function ends.
29. Write an iterative function void printPattern(num) that takes as a parameter a non- negative
integer and generates the following pattern of stars to generate the following pat- tern. Then
try and convert it into a recursive function that takes as a parameter a non- negative integer
and generates the following pattern of stars. E.g. if the non-negative integer is 5, then
following pattern is generated:
*****
****
***
**
*
*
**
***
****
*****
30. If P is the population of the first day of the year, B is the birth rate and D is the death rate, the
estimated population at the end of the year is given by formula:

The population growth rate is given by the formula:

Write a program that prompts the user to enter the starting population, birth and death rates
and n, the number of years. The program should then calculate and print the estimated
population after n years. Your program must contain the following functions:

8
GrowthRate: This function takes as its parameters the birth and death rates and returns the
population growth rate.
Estimated Population: This function takes as its parameters the current population,
population growth rate and n, the number of years. It returns the estimated population after n
years. Your program should not accept negative birth rate, negative death rate or population
less than 2.

You might also like