Introduction to software design 2012-1 Final 2012.06.13 16:00-18:00 Student ID: Name: - 1 -
0. 표지에이름과학번을적으시오. (6) 1. 변수 x, y 가 integer type 이라가정하고다음빈칸에 x 와 y 의계산결과값을적으시오. (5) x = (3 + 7) * 6; x = 60 x = (12 + 6) / 2 * 3; x = 27 x = 3 * (8 / 4) + 2; x = 8 y = x = (2 + 3)/4; x = 1 y = 1 y = 3 + 2 * (x = 7 / 2); x = 3 y = 9 2. 각각의문장을 condition 으로표현하시오. (4) x 는 5 보다크다. x > 5 x 는 5 가아니다. x!= 5 x 는 20 보다크거나같다. x >= 20 x는 1과 9를포함하는두수사이의값이다. x는 1과 9를포함하는두수사이의값이아니다. x >= 1 && x <= 9 x < 1 x > 9-2 -
3. 다음 While-loop 를 for-loop 으로바꿔표현하시오. (10) // sweetie1.c -- a counting loop #include <stdio.h> int main(void) const int NUMBER = 22; int count = 1; while (count <= NUMBER) printf("be my Valentine!\n"); count++; for-loop code // sweetie1.c -- a counting loop #include <stdio.h> int main(void) const int NUMBER = 22; int count; for (( count = 1 ; count <= NUMBER ; count++ ) printf("be my Valentine!\n"); - 3 -
4. 다음과같은결과를출력하는프로그램의빈칸을채우시오. (10) 출력결과 Pick an integer from 1 to 100. I will try to guess it. Respond with a y if my guess is right and with an n if it is wrong. Uh...is your number 1? n Well, then, is it 2? no Well, then, is it 3? no sir Well, then, is it 4? forget it Sorry, I understand only y or n. n Well, then, is it 5? y I knew I could do it! #include <stdio.h> int main(void) int guess = 1; int response; printf("pick an integer from 1 to 100. I will try to guess "); printf("it.\nrespond with a y if my guess is right and with"); printf("\nan n if it is wrong.\n"); printf("uh...is your number %d?\n", guess); while ((response = getchar())!= 'y') if ( response == 'n' ) else printf("well, then, is it %d?\n", ++guess); printf("sorry, I understand only y or n.\n"); while ( getchar()!= '\n' ) continue; printf("i knew I could do it!\n"); - 4 -
5. 다음프로그램들의출력결과를나타내시오. (20) (1) #include <stdio.h> int main(void) int n, m; for (n = 0; n <= 12; n++) if (n % 3 == 0) putchar('#'); else putchar('*'); putchar('\n'); for (n = 6; n > 0; n--) for (m = 0; m <= n; m++) printf("*"); printf("\n"); 출력결과 # * * # * * # * * # * * # * * * * * * * * * * * * * * * * * * * * * * * * * * * - 5 -
(2) #include<stdio.h> int main(void) int i,j,n; int set[100][100]; n = 6; for(i = 0; i <= n; i++) for(j = 0; j <= n; j++) set[i][j] = 0; set[0][0] = 1; for(i = 1; i <= n; i++) for(j = 1; j <= i; j++) set[i][j] = set[i - 1][j] + set[i - 1][j - 1]; printf(" Index"); for(i = 1; i <= n; i++) printf("%3d", i); printf("\n"); for(i = 1; i <= n; i++) printf("%4d ", i); for(j = 1; j <= n; j++) printf("%3d",set[i][j]); printf("\n"); - 6 -
출력결과 Index 1 2 3 4 5 6 1 1 0 0 0 0 0 2 1 1 0 0 0 0 3 1 2 1 0 0 0 4 1 3 3 1 0 0 5 1 4 6 4 1 0 6 1 5 10 10 5 1-7 -
6. 다음과같은결과를출력하는프로그램의빈칸을채우시오. (10) Enter an uppercase letter: D A A B A A B C B A A B C D C B A Enter an uppercase letter: E A A B A A B C B A A B C D C B A A B C D E D C B A Enter an uppercase letter: G A A B A A B C B A A B C D C B A A B C D E D C B A A B C D E F E D C B A A B C D E F G F E D C B A - 8 -
#include <stdio.h> void pyramid(char letter); int main(void) char letter; printf("enter an uppercase letter: "); scanf("%c", &letter); pyramid(letter); void pyramid(char letter) int i, j; for(i = 'A'; i <= letter; i++) for(j = 0 ; j <= letter i - 1 ; j++) printf(" "); for(j = 0 ; j <= i - A ; j++) printf("%c", j+ A ); for(j = i - A - 1 ; j >= 0; j--) printf("%c", j+ A ); printf("\n"); - 9 -
7. 다음빈칸을채우시오. (10) /* order.c -- precedence in pointer operations */ #include <stdio.h> int data[2] = 200, 300; int moredata[2] = 150, 50; int main(void) int * p1, * p2, * p3; p1 = p2 = data; p3 = moredata; printf(" *p1 = %d, *p2 = %d, *p3 = %d\n", *p1, *p2, *p3); printf("*++p1 = %d, *p2++ = %d, (*p3)++ = %d\n", *++p1, *p2++, (*p3)++); printf(" *p1 = %d, *p2 = %d, *p3 = %d\n", *p1, *p2, *p3); 출력결과 *p1 = 200, *p2 = 200, *p3 = 150 *++p1 = 300, *p2++ = 200, (*p3)++ = 150 *p1 = 300, *p2 = 300, *p3 = 151-10 -
8. 다음빈칸을채우시오. (25) (1) 다음은 삽입정렬 을수행하는프로그램이다. 아래와같이새로운수를하나씩입력하면이미정렬된배열에알맞게삽입하고출력하는것을반복한다. 이삽입정렬을수행하는프로그램의빈칸을채우시오. 입출력예제 2 2 4 2 4 6 2 4 6 5 2 4 5 6 10 2 4 5 6 10 1 1 2 4 5 6 10 3 1 2 3 4 5 6 10 7 1 2 3 4 5 6 7 10 13 1 2 3 4 5 6 7 10 13 9 1 2 3 4 5 6 7 9 10 13 11 1 2 3 4 5 6 7 9 10 11 13-1 - 11 -
#include <stdio.h> #define MAXSIZE 10000 int main(void) int i, loc, currentsize = 0; int inputnum; int num[maxsize]; while (scanf("%d", &inputnum) == 1) if (inputnum == -1) break; else if(currentsize == MAXSIZE) printf("error: Memory is full."); break; else loc = currentsize++ ; while ( loc > 0 && num[loc-1] > inputnum ) num[loc] = num[loc-1]; loc--; num[loc] = inputnum ; for (i = 0; i < currentsize; i++) printf("\n"); printf("%d ", num[i]); - 12 -
(2) 아래는 8-(1) 의코드의일부분이다. 여기서 if 문을제거해도같은동작을수행 하도록 while 조건부분을수정하라. while (scanf("%d", &inputnum) == 1) if (inputnum == -1) break;... while( scanf("%d", &inputnum) == 1 && inputnum!= -1 )... - 13 -
9. 다음과같은선언이주어졌을때, 각 statement 가올바르면 O, 그렇지않으면 X 로표시하시오. (10) (1) float rootbeer[10], things[10][5], *pf, value = 2.2; int i = 3; rootbeer[2] = value; things[4][4] = rootbeer[3]; things[5] = rootbeer; pf = value; pf = rootbeer; O O X X O (2) double rates[5] = 88.99, 100.12, 59.45, 183.11, 340.5; const double locked[4] = 0.08, 0.075, 0.0725, 0.07; const double * pd = rates; pd[2] = 222.22; *pd = 29.89; pd++; pd = &rates[3]; pd = locked; X X O O O - 14 -
10. 다음의가운데정렬을하는프로그램의빈칸을채우시오. (10) ( 단, strlen() 함수를사용한일반적인답이아닌경우오답처리 ) 출력결과 **************************************** G I G A T H I N K, I N C. 1 0 1 M e g a b u c k P l a z a M e g a p o l i s, C A 9 4 9 0 4 **************************************** Hint: size_t strlen( const char *str ) 함수는인자로 String 을받아그 String 의길이를리턴하는함수이다. 예 : strlen( abcdefg ) == 7 strlen( 12345 67890 ) == 11-15 -
#include <stdio.h> #include <string.h> #define NAME "GIGATHINK, INC." #define ADDRESS "101 Megabuck Plaza" #define PLACE "Megapolis, CA 94904" #define WIDTH 40 #define SPACE ' ' void show_n_char(char ch, int num); int main(void) int spaces; show_n_char('*', WIDTH); putchar('\n'); show_n_char(space, 12); printf("%s\n", NAME); spaces = (WIDTH - strlen(address)) / 2; show_n_char(space, spaces); printf("%s\n", ADDRESS); show_n_char(space, ( WIDTH strlen(place) ) / 2 ); printf("%s\n", PLACE); show_n_char('*', WIDTH); putchar('\n'); void show_n_char(char ch, int num) int count; for (count = 1; count <= num; count++) putchar(ch); - 16 -
11. 다음 if-elseif 문을 switch-case 문으로바꿔표현하시오. (15) if-elseif 문 #include<stdio.h> int main(void) char code; scanf("%c",&code); if(code == 'A' code == 'a') printf("your Score : 90-100\n"); else if(code == 'B' code == 'b') printf("your Score : 80-89\n"); else if(code == 'C' code == 'c') printf("your Score : 70-79\n"); else if(code == 'D' code == 'd') printf("your Score : 60-69\n"); else if(code == 'F' code == 'f') printf("your Score : - 60\n"); else printf("not known..\n"); - 17 -
Switch-case 문 int main(void) char code; scanf("%c",&code); switch(code) case 'A': case 'a': printf("your Score : 90-100\n"); break; case 'B': case 'b': printf("your Score : 80-89\n"); break; case 'C': case 'c': printf("your Score : 70-79\n"); break; case 'D': case 'd': printf("your Score : 60-69\n"); break; case 'F': case 'f': printf("your Score : - 60\n"); break; default: printf("not known..\n"); break; - 18 -
12. 입력이 abcdefg 일때, 다음과같은출력이나오도록빈칸을채우시오. ( 입력문자열의길이가 1000을넘지않는다.) (15) 출력결과 gabcdef fgabcde efgabcd defgabc cdefgab bcdefga abcdefg #include<stdio.h> #include<string.h> int main(void) int i,j; char current_word[1000], next_word[1000]; scanf("%s",currnet_word); for(i = 0; i < strlen(current_word); i++) next_word[0] = current_word[strlen(current_word) - 1]; for(j = 1; j < strlen(current_word); j++) next_word[j] = current_word[j - 1]; next_word[j] = '\0'; printf("%s\n",next_word); strcpy(current_word, next_word); - 19 -