Structured Programming Assignment I

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

Structured Programming Assignment.

Lecturer
John Maina

Jomo Kenyatta University of Agriculture and Technology


Stage I Sem II (Aug – Dec 2011) Structured Programming Assign I
Instructions:
i) Assignment to be done in groups of four (Maximum)
ii) All questions must be done and submitted in both softcopy and hardcopy. Ensure
that the names of all group members appear both in the hardcopy and the folder.
iii) Misreading the question will not be a valid excuse. Students should note that the
restrictions placed on the questions are due to the fact that specific aspects of the
course are being tested. Any deviation from the instructions will therefore lead to
disqualification of the answer in total.

1) Write a program that accepts somebody’s names (any) in any case and the outputs
them using the proper case (i.e. each name starts with a capital letter and the other
letters are in lower case. E.g. if somebody enters geORgE cliNTon maCdoNalD
oTIeNo, the output should be: - The proper way to write your name is George
Clinton Macdonald Otieno. (10 marks)
2) Using two nested do…while loops, write a program that prints out the following
output. NB: The program should not have more than two loops. (8 marks)
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
3) Write a program that accepts a set of integers (the user decides how many) and then
stores them in an array. The main function then passes these values one by one to a
user-defined function called get_even which returns 1 if the integer is even and 0 if it
is odd. The main function should then specify which numbers were even, which ones
were odd and their respective totals. It should also specify how many numbers were
odd and how many were even. For example, if the user enters 25 34 56 17 14 20, the
output should be: -
25 is an odd number
34 is an even number
56 is an even number
17 is an odd number
14 is an even number
20 is an even number

There is a total of 2 odd numbers and their sum is 42.


There is a total of 4 even numbers and their sum is 124.

1
Structured Programming Assignment. Lecturer
John Maina
NB: All data input and output should be done in main. Don’t use % any where in the
main function. (12 marks)
4) Write a program that accepts an integer and outputs it’s square, square root and cube
root using user-defined functions. The program should not use the predefined
mathematical functions e.g. sqrt and pow anywhere.
NB: 1) All the data input and output should be done in the main function and
computations in the user defined functions. 2) The program should only give specific
answers when those answers are whole numbers otherwise it should specifically say
the answer is not an integer. (12 marks)
Examples.
If the user enters 9 the output should be: -
9 squared is 81
The square root of 9 is 3
The cube root of 9 is not an integer.
If the user enters 27 the output should be: -
27 squared is 729
The square root of 27 is not an integer
The cube root of 27 is 3.
If the user enters 64 the output should be: -
64 squared is 4096
The square root of 64 is 8
The cube root of 64 is 4.
5) Write a program that accepts a number and the power it should be raised to and the
computes and outputs the results. Input and output should be done in the main
function and computation in a user-defined function called get_power. Don’t use any
predefined mathematical functions e.g. pow etc. anywhere in your program.
(8 marks)
6) Write a program that accepts the amount of money deposited in a bank account, the
annual interest rate and the target amount (The amount of money the account holder
wants to have in the account after a period of time) and then calculates the number of
years it will take for the money to accumulate to the targeted amount. NB: 1) The
interest being earned is Compound Interest. 2) Don’t use the formula for calculating
compound interest.
For example if the money deposited is 10000 and the target amount is 20000 and the
account earns an interest rate (compound) of 10% pa, then the output should be: -
It will take 8 years for your money to reach your target.
By the end of this period, the amount in your account will be 21435.89 (10 marks)
7) Write a program that stores and evaluates the total cost for items bought from a
supermarket. The cashier should enter the following: - Product code, Price and
Quantity. The total price should be evaluated as follows: -
Total cost = Price * Quantity
If the total cost per item is more than 20,000 there is a discount of 14% on that item
and a discount of 10% on an item whose total cost is between 10,000 and 20,000. No
discount is given on items whose total cost is less than 10,000

2
Structured Programming Assignment. Lecturer
John Maina
NB: The cashier should decide how many Items he/she wants to work with. If he/she
chooses 3, for example, the output should take the format shown below.
Item Code Price Quantity Total Cost Discount Net
109 6000 4 24000 3360 20640
201 900 8 7200 0 7200
127 600 20 12000 1200 10800
The total amount payable is 38640
NB: The output must take the format given above. (10 marks)
8) Write a program that accepts a set of numbers (The user decides how many) and
stores them in array. The program should the sort all the numbers in the array in
ascending order. It should output the numbers in the array before sorting and then
again after sorting. (10 marks)

* * * * * All the best – John Maina * * * * *

You might also like