60-140
Introduction to Algorithms and Programming I
Dr. Christie Ezeife
Lab. Exercises #4 (Lab Date: Week 6 of Classes)
Objectives
are to:
1.
Keep
practising on writing programs with functions as taught in chapter 4 of book.
In particular, to feel comfortable with use of call-by-value and
call-by-reference parameters.
2.
Review
all program logic structures as taught in chapter 5 of text book.
Que. 1. Using top-down approach to problem solving with functions and parameters, write a C program to solve the following problem. TrustCard, a company that issues calling cards uses an algorithm to create card numbers. The algorithm reads two four-digit integer numbers and computes a seven digit card number composed such that the first number of the calling card constitutes the four digits from the first input number, and the last three digits are composed from a number obtained by multiplying the sum of the four digits of the first input data by the number 3 and adding the sum of the four digits of the second input data.
Your program should print the following:
1. The calling card number assigned given any two four digit integers.
2. (i.) Original four digit numbers, (ii) their sums, and (iii) the three digit number obtained by multiplying the sum of first number digits by 3, (iv) the final three digit number that makes the last digit of the calling card (that is, (sum of first number digits x 3) + sum of second number digits) .
For example,
Sample Input:
Type the first digit number : 4737
Type the second digit number : 1234
Sample Output:
The sum of the first four digits 4737 is : 21
The sum of the 2nd four digits 1234 is : 10
The three digit number 21 x 3 is : 063
The three digit number 063 + 10 is : 073
The calling card number is : 4737073
Thank you for visiting TrustCard company !!
Hints on how to solve
i.
Use
the following structure chart.
Use the following structure chart for solving the problem.
Control Module 0000
-All input data should be read in the control module.
-Compute_Sum module should calculate the two sums.
-Findthree_digit should be used to compute the three digits.
-PrintAll should print all output numbers.
Use the integer division and modulus arithmetic to get the digits of the numbers. For example, with Num1 = 4737, and Num2 = 1234, the following equations can be used to get the digits of the number.
Digit4 = Num % 10;
==> Digit4 = 4737 % 10 = 7
Digit3 = (Num / 10) % 10;
==> Digit3 = 4737/10%10 = 473 % 10 = 3
Digit2 = (Num/10) / 10;
==> Digit2 = 4737 / 10 /10 % 10
= 47 % 10 = 7
Digit1 = ((Num/10) / 10)/10;
==> Digit1 = 4737/10/10/10 = 4
Sum1 = Digit1 + Digit2 + Digit3 + Digit4.
Then, after this, the sum of the digits can be obtained the same way as Sum2, and added to the Sum in control module.
A second call to Compute_Sum with the second number can be made to add the sum of the second number to the sum in control module.
The Findthree_digit module can then be called from main to compute the three digit before the Card number is computed in main with the three digit by adding them as the last digits of the first number.
Sum = Sum1 + Sum2.
three_digit = (Sum1 * 3) + Sum2.
Card_num = (Num1 * 1000) + three_digit
ii.
Your
job now is to understand the problem and use the three functions above and the
main driver to write the program to solve the problem by making necessary
function calls, passing the right parameters, placing the right instructions in
the right function definition etc.
You can complete the following program template in order to solve the
problem.
#include <stdio.h>
/* Now define the three function
prototypes with their parameter types */
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
int main(void)
{
/* Now declare the input and output
variables */
-----------------------------------------------------
-----------------------------------------------------
-----------------------------------------------------
/* Next, write the executable
instructions including function calls*/
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
return 0;
} /* end of the
main driver */
/* Next, write the definitions of the three functions Compute_Sum, Findthree_digit and
Print_All*/
write function header for Compute_Sum here
{
/* write instructions in this
function including any local variable declarations */
----------------------------------------------------
----------------------------------------------------
:
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
----------------------------------------------------
} /* end of Compute_Sum function */
write function header for Findthree_digit here
{
/* write instructions in this
function including any local variable declarations */
----------------------------------------------------
----------------------------------------------------
:
----------------------------------------------------
----------------------------------------------------
} /* end of Findthree_digit function */
write function header for Print_All here
{
/* write instructions in this
function including any local variable declarations */
----------------------------------------------------
----------------------------------------------------
:
----------------------------------------------------
----------------------------------------------------
} /* end of Print_All function */
Type, compile and run your program and
show work in a script file.
Que 2. Define the two types of parameters. How do they differ? Under what circumstances would you use each type?
Que 3. Why are cohesion and coupling important to programmers?
Que 4. Name all the program logic structures, giving a simple example of each structure.