keyMidterm2 - CompE 102 Computer Programming Midterm 2...

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: CompE 102 Computer Programming Midterm 2 Spring 2007-8 KEY ATILIM UNIVERSITY DEPARTMENT OF COMPUTER ENGINEERING Inst.s: G.Kalem, Ö.Kılıç, D.Mishra, M.Rehan Duration: 90 min. Name, Surname: Student no: Signature: WARNINGS • It is forbidden to bring electronic data storage equipments (e.g. mobile phone, MP3 player, flash disk etc.) to exams. • Students who either cheat, attempt to cheat or provide a help to other(s) in cheating, get 0 (zero) grade from the entered examination. Also, based on the regulations, a disciplinary action will be taken. Q1 (25 pts) Q2(25 pts) Q3 (25 pts) Q4(25 pts) TOTAL Q1 (25 pts). Write a function that receives a two digit positive integer. The function will continue to multiply the number’s digits until a single digit number is reached. The function returns the single digit result and the number of multiplications needed to reach to this single digit number. For example, if 39 is the input, 3x9=27 (1. multiplication), 2x7=14 (2. multiplication), 1x4=4 (3. multiplication) (Since 4 is a single digit, we stop here.) It will return the last value 4 and 3, which is the number of multiplications. void f(int n, int *mult, int *result) { int a, b; *mult=0; while(n>9) { a=n%10; b=n/10; n=a*b; (*mult)++; } *result=n; } 1 CompE 102 Computer Programming Midterm 2 Spring 2007-8 Q2 a) (15 pts) A function computes and returns the average of the odd numbers between 250 and 500. i) What is the prototype of this function? double f(void); ii) What is the function-call of this function? (Assume that all necessary variables are declared) x=f(); iii) What is the decleration of this function? double f(void) { int count=0, sum=0, i; double avg=0.0; for(i=250;i<=500; i++) { if(i%2= = 1) { sum+=i; count++; } } avg=sum/(double)count; } b) (10 pts) Make necessary corrections in the following code and re-write it into the box. #include <stdio.h> int factorial(int n, int fact); int main(void) { int *result; factorial(x, &result); printf("Factorial of %d is %d",4, &result); return(fact); } int factorial(int *n, int fact) { int i; * fact = 1; for( i = 1; i<=*n; i++) *fact = fact * i; } void factorial(int n, int *fact); int main(void) { int result,num; printf("Enter a number"); scanf("%d",&num); factorial(num, &result); printf("Factorial of %d is %d",num, result); return(0); } void factorial(int n, int *fact) { int i; * fact = 1; for( i = 1; i<=n; i++) *fact = *fact * i; } 2 CompE 102 Computer Programming Midterm 2 Spring 2007-8 Q3 (25 pts). What would be the output of the following code segment? Fill the grid provided below. #include <stdio.h> void first(int *x, int y); void second(int x, int *y); int a=2, b=3; int main(void) { int a,b; a=5; b=8; printf("%3d%3d\n",a,b); first(&a,b); printf("%3d%3d\n",a,b); getchar(); getchar(); return(0); } void first(int *x, int y) { int b; printf("%3d%3d\n",*x,y); b=6; printf("%3d%3d\n",a,b); *x=9; y=2; second(y,&b); } void second(int x, int *y) { int a; printf("%3d%3d\n",x,*y); a=4; printf("%3d%3d\n",a,b); } 5 5 2 2 4 9 8 8 6 6 3 8 3 CompE 102 Computer Programming Midterm 2 Spring 2007-8 Q4 (25 pts). Write a complete program to input a character from the user. According to this character do the following: - if the character is ‘G’ then your main function will call the green() function. This function takes no arguments but prints to the screen that “This is green”. - if the character is ‘Y’ then your main function will ask user to input an integer, and call the yellow() function. This function takes this argument as an input and returns the cube of that number. Your main function will print the result. #include <stdio.h> void green(void); int yellow(int); int main(void) { char c; int x, res; printf(“Enter a character:”); scanf(“%c”, &c); if(c==‘G’) green(); else if(c==‘Y’) { printf(“Enter a number:”); scanf(“%d”, &x); res=yellow(x); printf(“Result is %d”, res); } return 0; } void green(void) { printf(“This is green”); } int yellow(int x) { return(x*x*x); } 4 ...
View Full Document

This note was uploaded on 12/03/2009 for the course INDUSTRIAL IE 307 taught by Professor Arzuakyüz during the Spring '09 term at Atılım Üniversitesi.

Ask a homework question - tutors are online