60-140-1 Lecture for Thursday, Sept. 18, 2014. *** Dear 60-140-1 class, I am posting this lecture I would have given tomorrow, Thursday, Sept. 18, 2014 so you can read and continue with learning the course material with minimal disruption even on this even of a morning half day WUFA strike. Please, read and continue to work on your assignment 1 using this method of problem solving although your assignment 1 requires you to provide only the algorithm and the C program with its compilation and execution. Assignment 1 is not asking you to provide all 6 problem solving steps. Dr. C.I. Ezeife. Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem. B. Starting Ch. 3 Objectives: Basic Data types and instructions for an algorithm and a C program. 1. What are variables, constants & data types? 2. What are algorithmic and program instructions? 3. What the syntax for Read (scanf) and Print (printf) and assignment instructions? Example: Solve the Example 2.1 on Slide 33 also given below. Example 2.1: Management wants to see the patterns in absenteeism across its two departments, dept1 and dept2 for one week. It is interested in knowing the total absenteeism in each department in the one week it collected data. You are required to identify the input and output data of this problem and attempt to define an algorithm and a program. Step 1: Requirements Analysis (R): This involves defining the problems clearly. With example 2.1, the problem definition is clear with the work week consisting of 5 days.
Step 2: Components Analysis (C): Define the problem components of input data set, output data set and their types with relationships. Input Data Set: absent_day1_dept1, absent_day1_dept2, absent_day2_dept1, absent_day2_dept2, absent_day3_dept1, absent_day3_dept2, absent_day4_dept1, absent_day4_dept2, absent_day5_dept1, (integer) Output Data Set: total_absent_dept1, total_absent_dept2; (integer) Relationships: total_absent_dept1 = absent_day1_dept1 + absent_day2_dept1 + absent_day3_dept1 + absent_day4_dept1 + absent_day5_dept1; total_absent_dept2 = absent_day1_dept2 + absent_day2_dept2 + absent_day3_dept2 + absent_day4_dept2 + Step 3: Breaking down into modules possibly (M): For example 2.1, we are not breaking down into modules yet. Step 4: Algorithm Design(A): Writing the algorithmic solution of the problem using all the data gathered in steps 1 to 3 above. The format of the algorithm to follow is given on slide 54 of lecture notes and page 72 of text. The algorithm of Example 2.1 is given below: MainModule Begin
/* Declare Input and Output variables */ Input data: absent_day1_dept1, absent_day1_dept2 absent_day2_dept1, absent_day2_dept2 absent_day3_dept1, absent_day3_dept2 absent_day4_dept1, absent_day4_dept2 absent_day5_dept1, (integers) absent_day5_dept2 /*Note that this is the list of input data as in step 2 */ */ Output data: total_absent_dept1, total_absent_dept2 (integers) /* Note that this is list of output data as in step 2 /* Next, define instructions by first reading */ /* data values into input variables. */ a)read (absent_day1_dept1, absent_day2_dept1, absent_day3_dept1, absent_day4_dept1, absent_day5_dept1); b) read (absent_day1_dept2, absent_day2_dept2, absent_day3_dept2, absent_day4_dept2, absent_day5_dept2); /* Now, use input data values to obtain values for */ /* output variables. */ c) total_absent_dept1= absent_day1_dept1+absent_day2_dept1+ absent_day3_dept1+absent_day4_dept1+absent_day5_dep t1; d) total_absent_dept2= absent_day1_dept2+absent_day2_dept2+
absent_day3_dept2+absent_day4_dept2+absent_day5_dep t2; /* Next, print the output data values for the user. */ e) Print (total_absent_dept1, total_absent_dept2); End Step 5: Coding(C): Translate the algorithm of step 4 above into a C program. Use the format of a C program given on slide 55 of course note and on page 73 of course book. The C program for Example 2.1 is given below: #include <stdio.h> int main (void) { /* Input and output data are declared first in a program*/ int int absent_day1_dept1, absent_day1_dept2, absent_day2_dept1, absent_day2_dept2, absent_day3_dept1, absent_day3_dept2, absent_day4_dept1, absent_day4_dept2, absent_day5_dept1, total_absent_dept1, total_absent_dept2; /*Now present the sequence of instructions*/ scanf ( %d %d %d %d %d, &absent_day1_dept1, &absent_day2_dept1, &absent_day3_dept1, &absent_day4_dept1, &absent_day5_dept1); scanf ( %d %d %d %d %d, &absent_day1_dept2, &absent_day2_dept2, &absent_day3_dept2, &absent_day4_dept2,
} &absent_day5_dept2); total_absent_dept1 = absent_day1_dept1 + absent_day2_dept1 + absent_day3_dept1 + absent_day4_dept1 + absent_day5_dept1; total_absent_dept2 = absent_day1_dept2 + absent_day2_dept2 + absent_day3_dept2 + absent_day4_dept2 + printf ( Number of people absent in dept1 is %d and Number return 0; of people absent in dept2 is %d\n, total_absent_dept1, total_absent_dept2); Step 6: Testing and Verification (T): In this step, you can test the correctness of your program by: (i) tracing the program, which means executing it by hand with given data to see what results the program produces the way the CPU does. Tracing helps to identify logical errors so you can correct them. (ii) Also, you can compile run the program with the CPU and so it can produce results. This method is good if there are no logical errors in the program or it may be producing incorrect results. Tracing the above program by executing the insructions after the variables are initialized will have the following outcomes: scanf ( %d %d %d %d %d, &absent_day1_dept1, &absent_day2_dept1, &absent_day3_dept1, &absent_day4_dept1, &absent_day5_dept1); After first scanf above, the values of the variables are as given below: absent_day1_dept1 = 2,
absent_day2_dept1= 0, absent_day3_dept1 = 3, absent_day4_dept1 = 1, absent_day5_dept1 = 4. scanf ( %d %d %d %d %d, &absent_day1_dept2, &absent_day2_dept2, &absent_day3_dept2, &absent_day4_dept2, &absent_day5_dept2); After second scanf above, the values of the variables are as given below: absent_day1_dept2 = 0, absent_day2_dept2= 0, absent_day3_dept2 = 1, absent_day4_dept2 = 1, absent_day5_dept2 = 2. The effect of the 2 next instructions are given below: &absent_day5_dept2); total_absent_dept1 = absent_day1_dept1 + absent_day2_dept1 + absent_day3_dept1 + absent_day4_dept1 + absent_day5_dept1; = 2 + 0 + 3 + 1 + 4 = 10; total_absent_dept2 = absent_day1_dept2 + absent_day2_dept2 + absent_day3_dept2 + absent_day4_dept2 + = 0 + 0 + 1 + 1 + 2 = 4;
The last instruction prints the total for the two departments which arfe 10 and 4 and are the desired results. printf ( Number of people absent in dept1 is %d and Number of people absent in dept2 is %d\n, total_absent_dept1, total_absent_dept2); END OF CHAPTER 2. (GOOD BYE TO CHAPTER 2). ** Ch. 3 Objectives: Basic Data types and instructions for an algorithm and a C program. 1.What are variables, constants & data types? 2.What are algorithmic and program instructions? 3.What the syntax for Read (scanf) and Print (printf) and assignment instructions? Variables, Constants and data types. Primitive (basic) data types for representing real life data we use in solving our problems are: 1. integer called int in programming language C. 2. real called float in C. 3. character called char in C 4. string seen as a string of characters in C and declared as char variable[number of specified characters]. 5. logical (or Boolean) declared in C as data of type into with 0 to represent FALSE and!0 (that is not zero) representing TRUE. Read course lecture slides 40 to 45 for format of declaring variables of type int, float
or char. General format for declaring a variable in C is: datatype variablenames separated by commas and ending with a semicolon. It is: datatype variablename; datatype variablename, variablename; Eg. int num1, num2; int sum; int num1, num2, num3; float newnum, newnum1, diff; A constant variable is one that cannot change its value during the one program it is declared in. It is used to protect input data (such as a conversion value, eg. pi = 3.141) we do not want to destroy. The format for declaring a constant input data is: const datatype variablename = value; Eg. const float pi = 3.141; Format of an algorithm is on slide 54. Format of a C program is on slide 55. Read slides 56 to 60 for the format for writing a scanf and a printf General format of a scanf is: scanf ( format specifiers, &variable1, &variable2,, &variable); Eg. scanf( %f %f, &newnum, &nenum1);
The format specifiers for the different primitive data types are: Data type format specifier int %d float %f char %c string %s logical %d You have enough with your lab exercise 1 this week to start working on your assignment 1 and see you in class next Tuesday.