INTERNATIONAL ISLAMIC UNIVERSITY MALAYSIA

MID SEMESTER EXAMINATION SEMESTER I, 2007/2008 SESSION
KULLIYYAH OF ENGINEERING
Programme Time Duration : ENGINEERING : 8.30 pm – 10.00 pm : 1 Hr 30 Mins Section(s) : 7 Date Level of Study : UG 1

: 20/08/2007

Course Code : ECE 1113 Course Title

: Computing Systems and Programming

Name : ______________________________________________________ Matric #: _____________________ Section: _____________________

Question Q1 Q2 Q3 Q4 Total

Points /10 /10 /10 /15 /45

Any form of cheating or attempt to cheat is a serious offence which may lead to dismissal.

Computing Systems and Programming

Matric #:_______________

_____________________________________________________________
Section A [10 marks] [Answer all the questions. Write your answer in the space provided] Q.1) [10 marks] Select the best answer for the following questions. Every right answer will be rewarded with 1 mark. 1. Which of the following assignments produces a value of zero? (a) result = 9%3 – 1; (b) result = 8%3 – 1; (c) result = 2 – 6%2; (d) result = 2 – 8%3; 2. High level language is (a) good real-time programming. (b) the second generation of computer languages. (c) called natural languages. (d) written in English-like words. 3. The difference between the source program and the object program is (a) the source program possibly contains some bugs, and the object program does not contain any bugs. (b) the source program is the original code, and the object program is a modified code. (c) The source program is specified in a high level language, and the object program is specified in machine language. (d) the source program can be executed, and the object program cannot be executed. 4. A hand example means (a) doing arithmetic on your hands. (b) working out the details of the problem solution using a simple set of data. (c) outlining a solution to a problem. (d) expanding the outline of a solution and testing the algorithm step by step with a calculator. 5. Consider the following statement :
int i=100, j=0;

Which of the following statements are true?
(a) i<3 (b) !(j<1) (c) (i >0)||(j>50) (d) (j <i)&&(i<=10)

6. If a1 is true and a2 is false, then which of the following expression are true? (a) a1 && a2

2

ECE 1113

Computing Systems and Programming (b) a1 || a2 (c) !(a1 || a2 ) (d) !a1 && a2

Matric #:_______________

_____________________________________________________________

7. The expression (!((3-4%3) < 5 && (6/4 > 3))) is (a) true (b) false (c) invalid (d) none of the above 8. Which of the following is a valid function definition statement?
(a) (b) (c) (d) function cube(double x) double cube(double x) double cube(x) cube(double x)

9. In a function call, the actual parameters are separated by (a) commas (b) semicolons (c) colons (d) spaces 10. The definition of the statements in which an identifier is known (or can be used) (a) global (b) local (c) static (d) scope ANSWER:
1. 2. 3. 4. 5. 6. 7. 8. 9. D D C B C B A B A

10. D

3

ECE 1113

Computing Systems and Programming Section B [10 marks]

Matric #:_______________

_____________________________________________________________
Q.2) Program Errors [5 marks] Find five errors and write the correct statements in the space provid.
(a) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42.

(5 marks)
#include <stdio.h> #include <math.h> /* This program will read “dataFile.txt” containing data 5 3 8 9 2 5 0 8 3 7 4 and print the results of 3^8, 9^2, 5^0, 8^3, 7^4. */ int main() { int noOfData,x,y; int count,result; File *theData; theData =open(“dataFile.txt”, “r”); if (theData == NULL) printf(“Error opening input file.\n”); else { fscanf(“%d”, &noOfData); for (count = 1; count <= noOfData; count++); { fscanf(theData, “%d %d”, &x, &y); if {y == 0} result = 1; else result = pow(x,y); printf(“%d ^ %d is %d”, x, y, result); } fclose(theData); } return 0; }

4

ECE 1113

Computing Systems and Programming

Matric #:_______________ (5 marks)

_____________________________________________________________
b) What will be the output after the following program has been executed? #include <stdio.h> int main(void) { int no; int count; no = 1; do { printf("%d\n",no++); count = no; while (count < 7) { printf("%d",++count); if (count == 7) printf("\n"); } }while(no < 6); return 0; } ANSWER: a)[5 marks] Find and fix error Line Number ______17_______ ______19_______ ______26_______ ______27_______ ______31_______ Correction Statement ___________FILE_______________________ ____________fopen______________________ ____________(theData,”_________________ _____________no ;___________________ _____________if ( y == )____________________

b)[5 marks] Program Output Answer (0.5 points for each line) 1 34567 2 4567 3 567 4 67 5 7

5

ECE 1113

Computing Systems and Programming Section C [10 marks]

Matric #:_______________

_____________________________________________________________

Q.3 a) Conversion [5 marks] Convert the following nested if/else statements to a switch statement: if (rank==1 || rank==2) printf(“Lower division \n"); else { if (rank==3 || rank==4) printf(“Upper division \n"); else { if (rank==5) printf(“Graduate student \n"); else printf(“Invalid rank \n"); } }

ANSWER: switch(rank) (0.5) { (0.25) case 1 : case 2 (0.5) printf(“Lower division \n"); (0.25) break; (0.25) case 3 : case 4 (0.5) printf(“Upper division \n"); (0.25) break; (0.25) case 5 : (0.5) printf(“Graduate student \n"); (0.25) break; (0.25) default : (0.5) printf(“Invalid rank \n"); (0.25) break; (0.25) } (0.25)

6

ECE 1113

Computing Systems and Programming
b) Guided [5 marks]

Matric #:_______________

_____________________________________________________________
Write a loop that will calculate the sum of every third integer, beginning with i = 2 (i.e. calculate the sum 2+5+8+11+...) for all values of i that less than 100. a) declare and initialize sum as integer. b) Declare and initialize third_int as integer. c) Write while statement and it condition. d) Sum all the third_int. e) Increment the third_int so that every third integer will be sum up. [ 5 marks] ANSWER: #include <stdio.h> void main() { int sum=0; int i=2;

/* 1 */ /* 1 */

while (i<100) /* 1 */ { sum += i; /* 1 */ i += 3; /* 1 */ } }

7

ECE 1113

Computing Systems and Programming Section D [15 marks] Q.4 [15 marks]

Matric #:_______________

_____________________________________________________________

Write a complete C program for each of the problems presented below. Use whatever control structures are most appropriate for each problem. Calculate (a) and (b) in different programmer defined functions. (a) Calculate the weighted average of a list of n numbers, using the formula x _ avg = f 1 x1 + f 2 x 2 + ..... + f n x n where f's are fractional weighting factors, i.e. 0 ≤ f i < 1 , and f1 + f 2 + f 3 + ... + f n = 1 Ask the user to enter the n numbers in main function (maximum n numbers is 10).Get the input x from a data file "x_val.txt", the input is as follows 27.5 13.4 53.8 29.2 74.5 87.9 39.9 47.7 8.1 63.2 The number of loop that will be done is depend on the n numbers choose by the user. The input f i is from user. Write a statement to check either the input of f i is in the range of 0 ≤ f i < 1 , if not in the range asks the user to re-enter the value for f i . Print the result to the screen. (b) Calculate the geometric average of a list of n numbers, using the formula

x _ avg = ( x1 x 2 x3 ...x n )1 / n Ask the user to enter the n numbers in main function. The value of x is taken from user. The x_number entered are according to n numbers chose by the user. Print the result to the screen. Give a user a choice either to continue or exit the program.
ANSWER: Ans: #include <stdio.h> #include <math.h>

/*

0.25

*/

8

ECE 1113

Computing Systems and Programming void find_weig_ave(int n); void find_geo_ave(int num); void main() { int n,num,pick; do/* { /* /* 0.25 0.25 */ */

Matric #:_______________

_____________________________________________________________

0.5 */ /* calculate the weighted average */ printf("Dear user please enter how many numbers that you want to calculate” “(max is 10)\n"); scanf("%d",&n); /* 0.25 */ find_weig_ave(n); /* 1 */

printf("Dear user please enter how many numbers that you want to calculate\n"); scanf("%d",&num); /* 0.25 */ find_geo_ave(num); /* 1 */ /* 0.25 */

printf("0 to exit, 1 to continue\n"); scanf("%d",&pick); }while(pick!= 0);/* 0.25 */ } void find_weig_ave(int n) /* { double fi,x_avg=0.0 /* int i; FILE *input; /* 1 input=fopen("x_val.txt","r"); 0.5 0.25 */ /*

*/ */,x;

1

*/

for (i=0;i<n; i++) /* 0.5 */ { fscanf(input,"%lf",&x); /* 0.5 */ printf("Dear user please enter the value of fi\n"); scanf("%lf",&fi); /* 0.25 */ if (fi>0 && fi<1) /* 1 */ { x_avg+=fi * x; /* 1 */ printf("average = %.3lf\n",x_avg); /* } else { printf("please enter fi between 0<fi<1\n"); scanf("%lf",&fi); x_avg+=fi * x; printf("average = %.3lf\n",x_avg); } } }

0.25*/

/*] 1 */ /*] */ /*] */ /*] */

9

ECE 1113

Computing Systems and Programming

Matric #:_______________

_____________________________________________________________
void find_geo_ave(int num) /* 0.5 */ { double x_avg,x,mul=1.0; /* 0.25 */ int i; for (i=1; i<=num; i++) /* 0.5 */ { printf("Dear user please enter the value of x\n"); scanf("%lf",&x); mul *= x; /* 1 */ } x_avg=pow(mul,1.0/num); /* 1 (need to have 1.0/num if not it won;t give a correct value) */ printf("geometric average is %.3lf \n",x_avg); /* 0.25 */ }

10

ECE 1113

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer: Get 4 months of Scribd and The New York Times for just $1.87 per week!

Master Your Semester with a Special Offer from Scribd & The New York Times