TUTORIAL FINAL EXAM EKT120 SESSION 1 2013/2014 SECTION A Question 1 (a) Write a proper declaration for the problem given. (i) An array to calculate quarterly quantity of sales in a year. (ii) A function prototype to identify the minimum value from the three values sent to it. (b) For each of the following, write a single statement that performs the indicated task. Assumed that variables iValue1 and iValue2 of type int have been declared, and that iValue1 has been initialized to 100,000. (i) Declare the variable piNum1 to be a pointer to an object of type int. (i) Assign the address of variable iValue1 to pointer variable piNum1. (ii) Print the value of the object pointed to by piNum1. (iii) Assign the value of the object pointed to by piNum1 to variable iValue2. (iv) Print the address stored in piNum1. Use the %p conversion specifier. Is the value printed the same as the address of iValue1? 1 Question 2 Consider the program in Figure 2 and answer the questions. # include <stdio .h> int functionA (int); int main () { int p, count ; printf ("How many times to repeat ? "); scanf ("%d", &count ); for (p = 0; p < count ; ++p) { printf ("%d\t%d\n", p, functionA (p)); } return 0; } /* functionA function */ int functionA (int z) { int y, da , db , dc; y = da = db = dc = 2; while (z > y) { y = y + 1; dc = da * db; } return dc; } Figure 2 (a) Trace the output of the given program segment. (b) Sketch a flowchart for the functionA function. (c) Rewrite the while loop into for loop statements in functionA. Question 3 (a) C codes in Figure 3a is to determine the charge for three (3) types of services. Identify the errors and rewrite the correct code segment. float detCharge (int type) { switch case 1: charge = 10.00;break; case 2: charge = 20.00;break; case 3: charge = 30.00; default: charge = 0.00; } int main( ) { float item; printf("Please enter your selected service type: "); scanf(“%i”,&item); printf(“Your selected service price is %f\n”,detPrice(service_item); return 0; } Figure 3a (b) Write in the blank to complete the program segment shown in Figure 3b to calculate the total sales and the average sales of ten (10) items. 44 25 77 103 49 81 34 55 66 99 #define item 10 void getInput (int ______(i)________) { int i; printf("Please enter sales from ten items : "); for (i=0; i<item; i++) { scanf(“%d”, ______(ii)________; } } calTotAvgSales (int ____(iii)_____, float *total, float ____(iv)_____){ int i; *total = 0.0; for (i=0; i<item;i++) *total = *total + Sales[i]; *average = *total/item; } int main( ) { int sales[item]; float totalSales, avgSales; getInput( _______(v)_________ ); calTotAvgSales(____(vi)_____, _____(vii)_____, ____(viii)______); return 0;} Figure 3b (c) Write the output after executing the program segment in Figure 3c. int func (int r, int s) { r -= 2; printf("r = %d\ts = %d.\n", r, s); return r-- * ++s; } int sub (int *p, int *q) { *p += 5; printf("p = %d\tq = %d.\n", *p, *q); return *p++ + --*q; } int main() { int m = 5, n = 7; n = func(m, n); printf("m = %d\tn = %d.\n", m, n); n = sub(&m, &n); printf("m = %d\tn = %d.\n", m, n); return 0; } Figure 3c Question 4 Figure 4 is the declaration of structure and array of structure: struct itemType { string product; string productType; }; struct goodsType { itemType name; int productRating; log int productID; string supplier; float price; }; goodsType goods [50]; goodsType newGoods; Figure 4 Write C statements for the tasks below: (a) to store the following information in newGoods: productName: DutchLady_milk1.0L productType : dairy productID : 0100155 productRating:1 supplier:DL price:4.80 (b) to copy the information of the tenth component of the array goods into newGoods. (c) to update the price of the goods in the array goods by adding 0.45 to its previous value. Question 5 Below is parallel resistance equation. 1 / [1/r1 + 1/r2 + 1/r3] A program reads an input file named “data.txt” that contains the value of r1, r2 and r3 and performed the calculation to determine its corresponding parallel resistance value, then print all the values into an output file named “result.txt. The sample of input and output files are as shown in Figure 5a and Figure 5b respectively. A sample input file: 10 20 30 15 25 35 100 100 100 120 40 50 80 180 450 200 35 110 20 150 440 105 35 55 A sample output file: 0.1830 0.1352 0.030 0.... 0.... 0.... 0.... 0.... Figure 5a Figure 5b Write C statements based on the above problem statements using the following guidelines: (a) Declare two file pointers which points to FILE, namely input and output. (b) Open a file for reading named “data.txt”with internal file name input; likewise open a file for writing named “result.txt”with internal file name output. (c) Perform file verification towards input file. (d) Read a record from the file “data.txt”. The record consists input of type integer, namely r1, r2 and r3. (e) Write a record to the file “result.txt”. The record consists of output of type double, namely resistance. Section B Answer ANY TWO (2) questions in this section. Question 6 Write a program for the National Earthquake Information centre implementing the following decision table to characterize an earthquake based on its Richter scale number. Richter Scale Number (N) Characterization N < 5.0 Little or no damage 5.0 ≤ N ≤ 5.5 Some damage 5.5 ≤ N ≤ 6.5 Serious damage: wall may crack or fall 6.5 ≤ N ≤ 7.5 Disaster: house and buildings may collapse higher Catastrophe: most buildings destroyed (a) Sketch the flowchart for the program. (b) Produce a C program based on flowchart in (a). Question 7 Your friend has just opened a computer appliances shop at Changloon. He asked your help to develop a computer program which can be used at the counter. Your program should be able to input the customer’s order which could be more than one item. Produce a C program to calculate the total price for the order. You must implement array in your program. The program must have at least one function to calculate the total price of the order. Use the price from Table 7 for the calculation. Table 7 itemNo. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. Items Hewlett Packard laptop Longitech Optical Mouse Lenovo laptop Acer laptop Samsung Tablet Apple Tablet Memory card Altek Portable Hardisk 260GB Portable Hardisk 80GB Pendrive 4GB Price RM 3999 RM 31.80 RM 4400 RM 1999 RM 1499 RM 2333 RM 79.00 RM 280 RM 150 RM19.00 Question 8 You are the owner of a hardware store and need to keep an inventory that can tell you what tools you have, how many you have and the cost of each one. Write a C program to create a file named storage.dat and write the data shown in the Table 1 into the file. Also produce a C program to read the data from the file and print to the screen the list of all tools which quantity count falls below 40 and costs more than RM 50.00. struct inventory { long int recNumber; char toolName[40]; int quantity; float cost; }; Table 1: Inventory Details Record Number Tool Name Quantity Cost (RM) 3 Electric_sander 7 37.00 17 Hammer 76 29.00 24 Jig_saw 21 34.00 39 Lawn_mower 3 399.00 56 Power_saw 18 499.00 68 Screw_driver 106 17.00 77 Sledge_hammer 11 89.00 83 Wrench 34 33.00 Use the sample output below as a guide. Enter number of tools 8 Enter the Record Number 3 Enter Tool Name Electric_sander Enter Quantity 7 Enter cost of each tool 37.00 ….. ….. Tools below 40 and costs > RM 50.00 39 Lawn_mower 3 399.00 56 Power_saw 18 499.00 77 Sledge_hammer 11 89.00 -oo0oo-