{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

midtermSample

midtermSample - KEY ATILIM UNIVERSITY Department of...

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

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 Spring, 2007 Instructors: N. Çağıltay, M. Eryılmaz, G. Kalem, Ö. Kılıç Midterm I Date: Apr. 4, 2007 Duration: 90 min. Name Surname Id Q1 20 pts. Q2 10 pts. Signature Q3 15 pts. Q4 30 pts. Q5 25 pts. Total 100pts. Q1 (20 pts.) What would be the output of the following code segment? double z=2.0, t=3.615, w=2.5335; int x=0,y=3; if(x=5) x=9/2+y++; else z=--y+11/2; printf(“x=%d\n y=%3.1f z=%3d\n”, x, z, y); if(--y!=3) printf(“%5.2f %6.1f %1.2f\n”, z, t, w); else printf(“%5.2f %6.2f %3.1f\n”, w, z, t); x I I I = y 2 t t t switch(y) { case 2: printf(“It is 2\n”); break; case 3: printf(“It is 3\n”); case 4: printf(“It is 4\n”); default: printf(“It is none\n”); } 7 =2.0 z= 4 .53 2.00 3 is 3 is 4 is none . 6 1 Q2 (10 pts.) There are some mistakes in the following C program, please correct the code and ReWrite the program. #include <stdio.h> int main(void) { char gpa; double id; int ch; printf("Enter your GPA: "); scanf("%lf", gpa); printf("Enter your ID number: "); scanf("%3d",&id); printf("Enter a character (E,M,O): "); scanf(" %c",&ch); if(‘ch’==E); if(gpa>=2.80); printf("You are eligible for the course.); " else scanf("You are not eligible for the course.); " else printf("You are not eligible for the course."); return(0); } #include <stdio.h> int main(void) { double gpa; int id; char ch; printf("Enter your GPA: "); scanf("%lf",&gpa); printf("Enter your ID number: "); scanf("%3d",&id); printf("Enter a character (E,M,O): "); scanf(" %c",&ch); if(ch=='E'); if(gpa>=2.80); printf("You are eligible for the course."); else printf("You are not eligible for the course."); else printf("You are not eligible for the course."); return(0); } 2 Q3 (15 pts.) ReWrite the following C program using if statements. #include <stdio.h> int main(void) { char animal; printf(" D/d for DOG \n C/c for CAT \n R/r for RABBIT "); printf("\n Enter one of the characters above:"); scanf("%c",&animal); switch(animal) { case 'D': case 'd': printf(" Your choice is DOG."); break; case 'C': case 'c': printf(" Your choice is CAT."); break; case 'R': case 'r': printf(" Your choice is RABBIT."); break; default: printf(" You entered wrong character. Try again!"); } return(0); } #include <stdio.h> int main(void) { char animal; printf(" D/d for DOG \n C/c for CAT \n R/r for RABBIT "); printf("\n Enter one of the characters above:"); scanf("%c",&animal); if(animal=='D' || animal=='d') printf(" Your choice is DOG."); else if(animal=='C' || animal=='c') printf(" Your choice is CAT."); else if(animal=='R' || animal=='r') printf(" Your choice is RABBIT."); else printf(" You entered wrong character. Try again!"); return(0); } 3 Q4 (30 pts.) Write a complete C program which finds the average of the square roots of the numbers between 200 and 700 that are divisible by 3 or 5. #include <stdio.h> #include <math.h> int main() { double avg, total=0.0; int i=200,counter=0; while(i<=700) { if(i%3==0||i%5==0) { total+=sqrt(i); counter++; } i++; } avg=total/counter; printf("%6.2f",avg); return 0; } 4 Q5 (25 pts.) A family wants to go to Antalya for holiday, but they couldn’t decide which hotel to stay in and which class to choose. With respect to their choice, they will pay an entrance payment for hotels. For Hotel A the entrance payment is 50 YTL and for Hotel B it is 100 YTL. For each day they stay in the hotel, they will pay the amounts shown in the table below. First Class Second Class Third Class 30 20 10 Hotel A 90 70 50 Hotel B For example; - if the family choose Hotel B and Second Class and stay 5 days, they will pay 100 + (5 x 70) = 450 YTL - if the family choose Hotel A and First Class and stay 8 days, they will pay 50 + (8 x 30) = 290 YTL - if the family choose Hotel B and Third Class and stay 10 days, they will pay 100 + (10 x 50) = 600 YTL Write a complete C program using nested-if statements that calculates the total amounts of the family pay with respect to their choices. Sample run 2: Sample run 1: Enter Hotel: B Enter Class number: 2 Enter number of days: 5 You will pay 450 YTL. Enter Hotel: A Enter Class number: 1 Enter number of days: 8 You will pay 290 YTL. #include <stdio.h> int main(void) { char h; int c, day, amount; printf("Enter Hotel: "); scanf("%c",&h); printf("Enter Class number: "); scanf("%d",&c); printf("Enter number of days: "); scanf("%d",&day); if(h=='A') { if(c==1) amount=50+(day*30); else if(c==2) amount=50+(day*20); else if(c==3) amount=50+(day*10); } else if(h=='B') { if(c==1) amount=100+(day*90); else if(c==2) amount=100+(day*70); else if(c==3) amount=100+(day*50); } printf("You will pay %d YTL.", amount); return(0); } 5 ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online