keyMidterm1 - CompE 102 Computer Programming Midterm 1...

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 1 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: Q1 (25 pts) Student no: Q2(25 pts) Q3 (25 pts) Signature: Q4(25 pts) TOTAL Q1. Write a complete C program which reads 100 3-digit positive integer numbers one at a time and outputs how many of them satisfy the following condition. klm= k3 + l3 +m3 , where k, l, m are integers. satisfies the condition Ex: 153= 13 + 53 + 33 Sample Run: Enter an integer: 101 Enter an integer: 153 …. …. 2 of the input numbers satisfy the condition #include <stdio.h> #include <math.h> int main() { int i, n, k, l, m, counter=0; for(i=0;i<100;i++) { printf(“Enter an integer:”); scanf(“%d”,&n); m=n%10; l=(n/10)%10; k=n/100; if(n==(pow(k,3)+pow(l,3)+pow(m,3))) counter++; } printf(“%d of the input satisfy the condition”,counter); return 0; } 1 CompE 102 Computer Programming Midterm 1 Spring 2007-8 Q2. a) (15 pts) Re-write the following code segment by using switch and for statements: scanf(“%d”,&x); if (x==-1) { printf(“Too small\n”); printf(“Try again”) } else if(x==0) printf(“Down”); else printf (“Up”); i=1;s=0; t=-1; while(i<=x) { t=t*i; s=t+s++; i=i+2; } scanf(“%d”,&x); switch(x) { case -1: printf(“Too small\n”); printf(“Try again”) break; case 0: printf(“Down”); break; default: printf (“Up”); } s=0; t=-1; for(i=1;i<=x;i=i+2) { t=t*i; s=t+s++; } b) (10 pts) Evaluate each of the following expressions if a = 5, b = 10, c = 15, and flag = 1. 1. 2. 3. 4. c = = a + b || !flag a != 7 && flag || c >= 6 !(b <= 12) && a % 2 = = 0 !(a > 5 || c < a + b) Answer: 1. T 2. T 3. F 4. T 2 CompE 102 Computer Programming Midterm 1 Spring 2007-8 Q3. What would be the output of the following code segment? Fill the grid provided below. int i,j=1,k=4; for (i=1; i<8; i++) { if (k < 100) k = k*2; else k = k/3; j = ++k + i++; printf("%3d%3d\n",j,k); switch (j%5) { case 0 : printf("REMAINDER = 0\n"); k += 2; printf("%3d%3d\n",j,k); break; case 1 : printf("REMAINDER = 1\n"); k /= 2; printf("%3d%3d\n",j,k); break; case 2 : printf("REMAINDER = 2\n"); k *= 2; printf("%3d%3d\n",j,k); break; case 3 : printf("REMAINDER = 3\n"); k -= 2; printf("%3d%3d\n",j,k); break; case 4 : printf("REMAINDER = 4\n"); k -= 2; printf("%3d%3d\n",j,k); } } 10 9 REMAINDER 10 11 26 23 REMAINDER 26 11 28 23 REMAINDER 28 21 50 43 REMAINDER 50 45 =0 =1 =3 =0 3 CompE 102 Computer Programming Midterm 1 Spring 2007-8 Q4. Write a complete C program to read 2 non-negative integer numbers. If a negative number is entered, force user to input a positive one. If the first number is larger one, exchange these numbers. Then, find and print the average of all integer numbers between these two numbers #include <stdio.h> int main() { int n1, n2, sum=0, counter=0, temp, i; double avg; printf(“Enter 2 positive integer:”); scanf(“%d %d”,&n1, &n2); while(n1<0||n2<0) scanf(“%d %d”,&n1, &n2); if(n1>n2) { temp=n1; n1=n2; n2=temp; } for(i=n1;i<=n2;i++) { sum+=i; counter++; } avg=sum/(counter*1.0); printf(“%f is the average”, avg); return 0; } 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