mid_2_Sample - KEY ATILIM UNIVERSITY Department of Computer...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: KEY ATILIM UNIVERSITY Department of Computer Engineering COMPE 102 Computer Programming, Midterm II Spring, May 8, 2007 Instructors: N. Çağıltay, G. Kalem, Ö. Kılıç, D. Mishra, M. Rehan Duration: 90 min. Name Surname Id Signature Q1 15 pts. Q2 25 pts. Q3 30 pts. Q4 30 pts. Total 100pts. Q1. (15 pts) What is the output of the program below? Write down the output in the grid provided. #include <stdio.h> void func1(int *p, int k); int main(void) { int x=1, y=2; func1 (&y,x); printf("%4d%4d\n", x,y); func1 (&x,y); printf("%4d%4d\n", x,y); return (0); } void func1(int *p,int k) { k=2+k; *p=k+3; printf("%4d%4d\n",*p,k); } Answer here: 6 1 11 11 3 6 8 6 1 Q2. (25 pts) For the function given below, what would be the value assigned to result and what is the output of the function, if the following function call is executed? Function call in main: result = 3* find(3, 2, 4); Function definition: int find(int q, int b, int c) { int i, j, k, p; p = 0; for (i = 1; i <= q; i = i + 1) { for (j = b; j >= 2; j = j - 1) p = p + j; printf("%4d%4d\n",p,j); for (k = 3; k <= c; k = k + 1) p = p + k; printf("%4d%4d\n",p,k); } return (p); } Answer here: 1 1 2 2 2 9 1 8 0 7 1 5 1 5 1 5 The value assigned to result: 81 2 Q3. a) (10 pts) Write a function that receives two integer numbers. Function returns character value ‘e’, if the multiplication of the integer numbers is even; it returns character value ‘o’, if multiplication of integer numbers is odd, under function name to the calling function. Answer: char f (int x, int y) { if (x*y%2==0) return ‘e’; else return ‘o’; } b) (10 pts) Re-write the following program segment and instead of for-statements, use the equivalent while-statements. for(i=1;i<6;++i) { for(j=6;j>i;--j) printf(“*”); printf(“\n”); for(k=1;k<=i;k=k+2) printf(“+”); printf(“\n”); } i=1; while(i<6) { j=6; while(j>i) { printf(“*”); --j; } printf(“\n”); k=1; while(k<=i) { printf(“+”); k=k+2; } printf(“\n”); i++; } 3 c)(10 pts) Write a function that receives real numbers a,b,c, x and returns the values of y and z to the calling function, according to the following formula. y=ax2+bx+c z=y+(a+b)x2 +x Answer: void f1(double a, double b, double c, double x, double double *z) { *y=a*x*x+b*x+c; *z=*y+(a+b)*x*x+*x; *y, } 4 Q4. (30 pts)Write a complete C program to do the following: main function will: - read two integer values a and b (assume a<b) - displays the number of values which are multiples of 10, in range [a,b] and their summation Function f1: - receives two integer values x and y (assume x<y) - finds number of values which are multiples of 10 in range [x,y] and returns it to the calling function through a parameter - finds sum of numbers which are multiples of 10 in range[x,y] and returns sum to the calling function through a parameter Sample Run: (Italics respresents user input) Enter two integers: 5 53 5 numbers are multiples of 10 between 5 and 53 Sum is 150 hint: (10+20+30+40+50) Answer: #include <stdio.h> void f(int x, int y, int *num, int *sum); int main(void) { int a,b,total,c; printf(“Enter two integers”); scanf(“%d%d”,&a,&b); f(a,b,&c,&total); printf(“%d numbers are multiples of10 between%d and %d”,c,a,b); printf(“\nSum is %d”,total); return 0; } void f(int x, int y, int *num, int *sum) { int i,count=0,s=0; for(i=x; i<=y ; ++i) { if(i%10==0) { count++; s+=i; } } *sum=s; *num=count; } 5 ...
View Full Document

Ask a homework question - tutors are online