60-140

Introduction to Algorithms and Programming I

Dr. Christie Ezeife

Lab  Exercises  #2 (Lab Date: Week 4 of Classes)

 

Objectives are to:

1. Practise on solving problems and writing simple algorithmic and C program solutions for the problems by going through the problem solving steps as taught in chapter 2 of text book.

2. Practise on the use of arithmetic, logical, relational, bitwise, increment and decrement operators in C’s expressions and assignment instructions as taught in chapter 3 of book.

 

Que. 1.  Solve the following problem by going through the problem solving steps.

 

Write a program to accept two 4-digit integer numbers and generates one new integer number where each corresponding digit of the output number is the sum of the two corresponding digits of the two input numbers divided by 2.  For example, if the input numbers are 3155 and 4998, the output number to be printed by your program is 3576.  Hint: Note that to solve, the digits of each integer number have to first be extracted using the integer modulus and division arithmetic and later, the digits of the resulting integer number have to be put together.

 

Hints on how to solve

 Follow the following steps in solving the problem:

i.                     Understand the problem by stating the problem definition clearly and learning what is required to be done, what are available as input data to work with.You can draw a picture to make the requirements clear if needed.

Problem Definition: Given two 4 digit numbers, print an output number where the corresponding digits of output number are obtained as the sum of the corresponding digits of the input numbers divided by 2. For example, with the input numbers 3155 and 4998, the output number is 3576.

 

ii.                 Identify all input, output data and their type as well as relationships between input and output data in equations:

Input data are:


---------------------------                    -----------------------------

---------------------------                    --------------------------------

----------------------------                   --------------------------------

-----------------------------                 -------------------------------- 

 

 Output data are:

 ------------------------------                ---------------------------------

 

Equations that link output data variables on the left hand side with input data variables on the right hand side in the correct logical order they will work are:

 

----------------------   =      -------------------------------------------------------------

---------------------    =      -------------------------------------------------------------

---------------------    =      -------------------------------------------------------------

---------------------    =      -------------------------------------------------------------

---------------------    =      -------------------------------------------------------------

---------------------    =      -------------------------------------------------------------

 

iii.                  No need to break the problem solution into smaller modules now.

iv.                 Write the algorithmic solution using the template for an algorithm similar to the formal algorithmic structure in Figure 3.1 of text book. Use the following template now.

 

main module

{

   Input data variables are:  

      [fill in from step ii above ]

  

   Output data variable are:
                  [fill in from step ii above]

 

/*  Executable instructions consist of the equations that link

output variables and input variables in the correct order.  Write

them next.   Remember that input data have to be read first before

 being used to compute the output data.  Finally, the output data

have to be printed.              */

--------------------------------------------------------------------

--------------------------------------------------------------------

--------------------------------------------------------------------

      --------------------------------------------------------------------

      --------------------------------------------------------------------

      --------------------------------------------------------------------

      --------------------------------------------------------------------

 

 

 

 }

 

v.                   Now, translate the algorithm you have written in step iv above into a C program using the following template that is similar to the general structure of a C program presented in Figure 3.2 of text book.

 

#include      <stdio.h>

int main(void)

{

/* Now declare the input and output variables using int or float         */

/* Remember to end every instruction with a semi-colon.*/

 

--------------------------------------------------------------------

--------------------------------------------------------------------

 

/* Next, translate each executable instruction in iv to a C instruction. */

 

--------------------------------------------------------------------

--------------------------------------------------------------------

--------------------------------------------------------------------

      --------------------------------------------------------------------

      return   0;
                 }

           

vi.                 Now, type the program in v, compile and run with a set of test input data. You may use the following set of test data:

Number1 = 3155 and  Number2= 4998

Number1 = 1234 and  Number2= 5679

Show the results of your program in a script file.

 

Que. 2.  Solve the version of problem of Exercise 2.4, question 3 of course book which reads the temperature in degrees Fahrenheit (oF) and computes and prints the temperature in degrees Celsius, by going through the problem solving steps.

 

Que. 3.  From Section 3.4 of course book, do question 3 (but with A = 32 and B = 17; also for A = -5 and B = -20). Also do questions  5, 6, and 7.

 

Que. 4. Discuss the problem solving steps.

 

Que. 5.  Start to prepare for Quiz #1.