You are on page 1of 16
alls ayalllayall lllaealall INTERNATIONAL ISLAMIC UNIVERSITY MALAYSIA Final Examination SEMESTER I, 2013/2014 SESSION KULLIYYAH OF ENGINEERING Program: Engineering Level of Study :UGI ‘Time 22:30 PM — 5:30PM Date 2 07 JANUARY 2014 Duration 3 Hours Course Code: BCE 1322 Section(s) 2116 Course Title: Programming for Engineers ‘This question paper consists of Sixteen (16) printed pages (including cover page) with Four (4) sections, INSTRUCTION(S) TO CANDIDATES DO NOT OPEN UNTIL YOU ARE ASKED TO DO SO “This examination amounts to 40% of the total assessment, Answer the questions from all Four (4) sections. Write down your answers on these examination sheets. ‘Write your name, matric number and section on every sheet ‘The total mark of this examination is 80. ‘Any form of cheating or attempt to cheat is a serious offence which may lead to dismissal. CE 1372 Porn fx Eger fel Exsusatn anny 204 Matrie No 7] & | % [10 tT]_2 fs 74 [anaver 1. Which one of these is not true about C structure? a. A structure is a collection of data values, called data members '. A structure groups different variables under a single name for & ce. Anareay cannot be a data member in a structure d. Data members in a structure can be of different types handling 2. Which one of these is not true about pointers? (Assume ptr is a pointer variable) a. double *ptr; (Declaring a pointer variable) b. ptr = pz (where p is another pointer) c. print£("$a", ptr) ; Gisplaying the value that ptr is poit d. ptr = val; (where val is variable of type double) ing to) 3, Assuming an array and pointer declaration as below: int (2) [3] = (12,4,6), (1/5,3}}, *sptr=&s [0] (0]7 How to access value 5 in the array using a pointer reference? a *(sptr + 4) b, *sptr(4) ce (*sptr + 5) d. ‘sptr + 5 4, A two dimensional array of type double of size 4 rows by 6 columns is declared as a. double arr(3] [5]; b, double arx{4](6]7 c. double arr[6] [4]; d. double arr(4) (6); 5. Which of the following operations is valid on the file "NOTES . TXT"? (Assume that you have the following code segment beforehand) FILE *£p; £p = fopen("NOTES.TXI", "w"); a, printf (fp, "Welcome\n") ; fscanf("8d", gdata); fprinté(fp, data); fprint£(£p, "sd", data); aos Page 2 of 16 CE 122 Pop Eger al ton 1° Janay 204 Name Matrie No ; Seation No 6, ‘The keyword used to skip the remaining code segment in a loop and proceed with the next iteration of the same loop is, a switch b. continue ¢ break d. skip 7. ‘There are two ways to pass arguments to a function. Which is correct? a. Pass by reference and argument b. Pass by pointers and argument c. Pass by address and value d. Pass by value and reference 8. Which statement is not true about array and function? a. Array is passed to a function by reference ’b. When an array is passed to a function, the original values of the array can be changed inside the calling function (ifno const: keyword is specified in the array definition of the formal parameter of the calling function) ©, By default, an array is passed by value to a function, therefore the original value of the array cannot be changed inside the calling function . When an array is passed to a function where a const. keyword is specified i the array definition of the formal parameter of the calling function, the original values of the array cannot be changed! inside the calling function. 9, When rand () fumetion is called, the value returned by the function will be a random integer between 0 and RAND_MAX (normally 32767), (inclusive). Which of the following will generate random number from -25 to 25 (inclusive) and assign the value to y? a, y=rand()$51-25; b. y=rand()%51-26; c ysrand()$50-25; d. y=rand () 825-25; 10, What is the command to create the following matrix in Matlab? x= L 5 8 9 4 3 aX = (17578, 97473] vb. X= (1, 5 8,7 % 4 37] c X= (17 5 8,7 9, 4 3s] d x= [15 8, 94 3) Page 3 of 16 CE 132 Pogmsing fe ayes Pinel Benton 07 amy 2014 Name Matrie No Section No:_ Part B Program Output [10 Marks] rai ene ee Sukae Write the output of the following code: 1. [2marks] Hinclude struct course { int coursenos char coursename [25]; Ve int main () cl struct course of] = { (102, "Java"}, (103, "PHE"), {104, "DotNet"}}; printf ("8d ", c{1].courseno); printe("$s\n", (*(c#2)) .coursename) 7 return 0; ) 2, (1 mark] finclude #include int main() ‘ char str1[20] =~ "Hello", str2(20) World"; printg("$s\n", strepy(str2, strcat(strl, str2))); return 0; ) 3. [2 marks} Hinelude int main () 4 char *names(] = ( "Mercury", "Venus", "Earth", "Mars"); int iz char *t; t = names(2)7 names [2] = names (3}7 names [3] = ty for(i=0; 4 printé ( xeturn 0; pee) "$5", names (i]}7 Page 4 of 16 Name. Seotion No: BCE 1822 Fropanning Eres Pal Exaniatn] OJ 2916 Matric No | 4, [2 marks} Hinclude int sumdig(int n); int main() ( int a, b; a = sumdig (123); b = sumdig (123); print£("$d, %d\n", a, b); return 0; int sumdig(int n) int s, dy LE (n1=0) ‘ ) d= 8107 n= n/10; 3 dtoumdig(n) else return 0; return sj } 5, [1 mark] #include int main() { int a = $00, b = 100, ¢ U£(1a >= 400) e= printf("b = $d c = td\n", by ©)? return 0; ) 6. [2 marks] #include int main() { int a(5] = (5, 1, 15, 20, 25}7 int i, j, m i m b = 300; 200; tall; all]+t; alitels print£("d, $d, 8d, 8d", i, 5, m alii? return 0; Page 5 of 16 E122 Fogel for Eis | raination| OP Jenny 2014 watieno ¢[ —T Section No: Section 2: Error Finding and Correction (20 Marks) Section 2: Part A [5 marks] Find a total of Five (5) errors in the following code segments, For each error, you have to dicate the line of error and provide the correction. 1, include 2, Hinclude 3, tinelude 4, struct Resource ; 5. 6. char material [30]; 7 double Longitude; 8 double latitude; 9. double quantity; 10. char units (20]; 11. } 12, int main (void } 13. ¢ 14, struct Resource; 15. metal.longitude = 57.37 16. metal.latitude = 32.1; 17, metal.quantity = 3e+10; 18, strepy (metal.material, ‘Ivon‘’); 19. strepy (metal.units, "cubic meters"); 20. int£ ("the metal information is:\n" "$1f\n$4.11£ degrees Longitude\n" "g4.11£ degrees latitude\nte %s\n\n", metal.material, metal. longitude, metal,latitude, metal.quantity, metal.units); 21, system("pause"); xeturn 0; 22.3 Answer Now | bine No. Correetion Page 6 of 16 CE 122 Foun fx Esc Fal Bwana: 204 Name: Mattie No [7 T Section No: Section 2: Part B [5 marks] Find a total of Five (5) errors in the following code segments. For each error, you have to indicate the line of error and provide the correction. 1. #include 2. 3. structure simple at 5. int ival; 6. double dval; eee 8. void funl(struct simple s); a. int main (void) dO. 11. 12. struct simple sl = (10, 1.5}; 13. struct funl(sl); 14. printf ("$d $0.21f\n", si.ival , sl.dval 45. system("pause") ; 16. return 0; 17.) 18. void funl(struct simple s); 19.0 { 20. s.ival = s.ival + 2; Set s. (dval+t); 22. return; 23. 4+ Answer’ | Tine No, Correction No. ; 1 2 3 4 5 Page 7 of 16 ECE1N22 Popa ic gers | al anion [OP dena 2014 Name Matric No Section No: Section 2: Part C [5 marks] Find a total of Five (5) errors in the following code segments. For each error, you have to indicate the line of error and provide the correction. 1, include 2, #include 3. 4, struct rect 5. 6. double x, y, width; Toe 8. 9. int main (void) 10. ¢ 11. FILE *rectangle; 12. input = fopen(rectangle.txt, "r"); 13. scanf(rectangle, "$l $1f tlf $1£",arl.x, erl.y, srl.width, grl.height); 14. 15. print£("Rectangle is at location: 8f , $f \n", rl.x, rl.y)i 16. printf("The width is: tf \n The height is: $f \n",rl.width, rl.height) + n. 18, fclose (rectangle); 19. system("paase")? 20. return 0; 21.) Answer} Line No. Correction 1 2 3 4 5 Page 8 of 16 CE 192 Pos fc Ege | Fal eatin |O7 ey 204 Name Matric No + Section No: Section 2: Part D [5 marks] Given the structure defini n and declarations struct Force { char name [40]; int point_number; double xforce; double yforce; Me int main() ( struct Force wing, fuselage; return 0; } Find errors if any in the following statement. If there is error, provide the correction in the Answer column below. si Statement Answer 1 wing. xforce=15.3; 2 | wing.xforce.yforce=28.5; 3 fuselage .name=!v! ; 4 | strepy(wing, "Dc 10"); 5 | fuselage .point_number=85; Page 9 of 16 E172 oan Eger |e Esmutin | O7* Sanay 294 Name: Matric No :| I Section No: Section 3: Coding (20 Marks) Write a complete C program to solve for the following problem. You are required to perform matrix manipulation using the following equation: © = A(2*A4B) ; where A and B are 3-by-3 matrices which values are defined in files ‘A. txt’ and vB.txt! respectively. ‘A. txt’ looks like this: plneuter esate 4 5 6 7 8 9 Similarly, ‘B. txt” looks like this: 4 5 6 7 8 9 10 11 12 Specifically, you need to: 1. For ‘A. txt’, read the data from the file and store them in matri matrix B A. Do the same for 2. Perform the calculation as written above. Particularly, write functions that perform matrix addition and multiplication respectively. 3. Print out matrices A, B and C on the screen, Hint: ‘You can break down the equation into several parts and store them in temporary matrices, Page 10 of 16 [CE 1372 Progeanng Eines al Brazn| O7 omy 204 Name Matrie No jon No: Page 11 of 16 ECE 1322 psn fc Epes el Exaintion OT Faay/29 1 Name Matrie No Section No Page 12 of 16 ECE 192 Prange Sagi | Fla Cxainnton}O7 rey 291 Name Matric No Seotion No: Section 4: MATLAB (20 marks) Part A [10 Marks] What is the output of the following MATLAB seript? (Hint: Every question is not independent question. Therefore, the variables on a particular question depend on the variables which have been defined or modified in the previous questions.) A= 5*6/6*5 Be Lids c= Bes b= BHC Bs B.*C F(-1, 0, 07 1, 1, OF 1, ~1, OF 0, O 23 F(2)=5 G={1, 0, 0; 10, 1, 10; 10, 10, 217 H=G(2:3, 2) Tones (2, 3) 0. gent Output: Page 13 of 16 CE 122 Fong fi Eger Fel Csnatin 7" Jenny 214 Name: Matrie No Section No: Part B [5 Marks] Draw the graph that will be executed by the following Matlab instructions. >> x = [0:22:10]; >> y= (0, by 2, 3, 4, S17 >> plot (x,y) >> title (‘Ahmad’/s Matlab Graph’) >> xlabel ( ‘Time, msec’) >> ylabel (‘Distance, km’) >> grid on Graph: Page 14 of 16 ECE (32 Preys Eager Fn Exannatn 07 Sema 2085 Name: Matric No Seation No: Part C [5 Marks] Write the MATLAB commands to solve the following linear equations. Bx + 2y- 2 ~x + By + 22 x- yr 2 Matlab Commands: >> >> >> >> >> >> >> Page 15 of 16 Name Section No: Extra blank space E12 Progamming fr Eater |anlHsaieaton 7° Janay 201 Matric No Page 16 of 16

You might also like