Microsoft PowerPoint - Chapter_05.pptx

Similar documents
Chapter_06

C 언어 프로그래밊 과제 풀이

OCW_C언어 기초

int main(void) int a; int b; a=3; b=a+5; printf("a : %d \n", a); printf("b : %d \n", b); a b 3 a a+5 b &a(12ff60) &b(12ff54) 3 a 8 b printf(" a : %x \

Microsoft PowerPoint - chap04-연산자.pptx

중간고사

프로그래밍개론및실습 2015 년 2 학기프로그래밍개론및실습과목으로본내용은강의교재인생능출판사, 두근두근 C 언어수업, 천인국지음을발췌수정하였음

untitled

untitled

비트와바이트 비트와바이트 비트 (Bit) : 2진수값하나 (0 또는 1) 를저장할수있는최소메모리공간 1비트 2비트 3비트... n비트 2^1 = 2개 2^2 = 4개 2^3 = 8개... 2^n 개 1 바이트는 8 비트 2 2

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

Microsoft PowerPoint - chap02-C프로그램시작하기.pptx

untitled

Microsoft PowerPoint - chap05-제어문.pptx

Microsoft PowerPoint - chap03-변수와데이터형.pptx

< E20C6DFBFFEBEEE20C0DBBCBAC0BB20C0A7C7D12043BEF0BEEE20492E707074>

Microsoft PowerPoint - Chapter_08.pptx

Infinity(∞) Strategy

1.2 자료형 (data type) 프로그램에서다루는값의형태로변수나함수를정의할때주로사용하며, 컴퓨터는선언된 자료형만큼의메모리를확보하여프로그래머에게제공한다 정수 (integer) 1) int(4 bytes) 연산범위 : (-2 31 ) ~ (2 31 /2)-

Microsoft PowerPoint - [2009] 02.pptx

금오공대 컴퓨터공학전공 강의자료

: 1 int arr[9]; int n, i; printf(" : "); scanf("%d", &n); : : for(i=1; i<10; i++) arr[i-1] = n * i; for(i=0; i<9; i++) if(i%2 == 1) print

Microsoft PowerPoint - CP_02_Convert_To_C_02_Step.pptx

Microsoft PowerPoint - chap10-함수의활용.pptx

Microsoft PowerPoint - Chapter_04.pptx

Microsoft PowerPoint - chap13-입출력라이브러리.pptx

KNK_C02_form_IO_kor

PowerPoint Presentation

11장 포인터

윈도우즈프로그래밍(1)

PowerPoint Presentation

Microsoft PowerPoint - ch07 - 포인터 pm0415

歯9장.PDF

PowerPoint 프레젠테이션

Microsoft PowerPoint - chap-06.pptx

C++-¿Ïº®Çؼ³10Àå

Microsoft PowerPoint - ch01.ppt

PowerPoint 프레젠테이션

Microsoft PowerPoint - chap11-포인터의활용.pptx

Microsoft PowerPoint - 제11장 포인터

[ 마이크로프로세서 1] 2 주차 3 차시. 포인터와구조체 2 주차 3 차시포인터와구조체 학습목표 1. C 언어에서가장어려운포인터와구조체를설명할수있다. 2. Call By Value 와 Call By Reference 를구분할수있다. 학습내용 1 : 함수 (Functi

쉽게 풀어쓴 C 프로그래밍

Microsoft PowerPoint - 5장 조건문 pptx

3. 1 포인터란 3. 2 포인터변수의선언과사용 3. 3 다차원포인터변수의선언과사용 3. 4 주소의가감산 3. 5 함수포인터

OCW_C언어 기초

C 프로그램의 기본

슬라이드 1

PowerPoint 프레젠테이션

Microsoft PowerPoint - chap-11.pptx

Microsoft PowerPoint - 제11장 포인터(강의)

제 14 장포인터활용 유준범 (JUNBEOM YOO) Ver 본강의자료는생능출판사의 PPT 강의자료 를기반으로제작되었습니다.


Microsoft PowerPoint - 3ÀÏ°_º¯¼ö¿Í »ó¼ö.ppt

컴파일러

C 프로그래밊 개요

Microsoft PowerPoint - chap12-고급기능.pptx

슬라이드 1

쉽게 풀어쓴 C 프로그래밍

제 11 장포인터 유준범 (JUNBEOM YOO) Ver 본강의자료는생능출판사의 PPT 강의자료 를기반으로제작되었습니다.

chap8.PDF

歯7장.PDF

Java ...

쉽게 풀어쓴 C 프로그래밍

PowerPoint 프레젠테이션

chap7.PDF

슬라이드 1

Microsoft PowerPoint - Chapter_02.pptx

<4D F736F F F696E74202D20C1A633C0E52043C7C1B7CEB1D7B7A5B1B8BCBABFE4BCD2>

C 프로그래밊 개요

ch15

<4D F736F F F696E74202D20C1A63134C0E520C6F7C0CEC5CD5FC8B0BFEB>

쉽게 풀어쓴 C 프로그래밍

Microsoft PowerPoint - 2주차-1차시 (강의자료) ch01 - C Programming 기초 (part 2)

Line (A) å j a k= i k #define max(a, b) (((a) >= (b))? (a) : (b)) long MaxSubseqSum0(int A[], unsigned Left, unsigned Right) { int Center, i; long Max

ABC 2장

1 장 C 언어복습 표준입출력배열포인터배열과포인터함수 const와포인터구조체컴파일러사용방법 C++ 프로그래밍입문

Microsoft PowerPoint - chap-07.pptx

본 강의에 들어가기 전

Microsoft PowerPoint - 2장 변수와 자료형 pptx


11장 포인터

Microsoft PowerPoint - Lesson6.pptx

Microsoft PowerPoint - gnu-w10-c-chap11

Microsoft PowerPoint - chap-03.pptx

쉽게 풀어쓴 C 프로그래밍


Microsoft PowerPoint - Chapter_09.pptx

프로그램의실행화면 주석 (comment) 두수의합 : 300 /* 두개의숫자의합을계산하는프로그램 */ 주석은코드를설명하는글입니다. 주석 3 가지방법의주석 주석의예 /* 한줄로된주석 */ /* 저자 : 홍길동날짜 : 2013.

2015 개정교육과정에따른정보과평가기준개발연구 연구책임자 공동연구자 연구협력관

PowerPoint 프레젠테이션

학습목표 함수프로시저, 서브프로시저의의미를안다. 매개변수전달방식을학습한다. 함수를이용한프로그래밍한다. 2

Microsoft PowerPoint - Lesson2.pptx

Microsoft PowerPoint - c2.ppt

<4D F736F F F696E74202D20B8AEB4AABDBA20BFC0B7F920C3B3B8AEC7CFB1E22E BC8A3C8AF20B8F0B5E55D>

Microsoft PowerPoint - 10장 문자열 pptx

Microsoft PowerPoint - Java7.pptx

C++ Programming

Microsoft PowerPoint - Chapter_07.pptx

목차 포인터의개요 배열과포인터 포인터의구조 실무응용예제 C 2

13 주차문자열의표현과입출력

Transcription:

프로그래밍 1 1 Chapter 5. Functions and Control Flow April, 2016 Dept. of software Dankook University http://embedded.dankook.ac.kr/~baeksj

이장의강의목표 2 printf 함수와 scanf 함수의이해함수의이해대표적제어문인 if와 for 문을이해한다. 코드블록 (code block) 의의미와필요성을이해한다. format specifier 와 escape sequence를이해한다

printf() 의확장된기능 (1/7) 3 문자 형식지정자 (format specifier) printf(...%*, ); 정수 (%i) %c %d %f 실수 부호없는정수 문자열 %u %x %o %s %e %g %p %n 부호없는정수 8 진수 부호없는정수 16 진수 실수의과학계산용표기 %e or %f 포인터주소 문자열길이

printf() 의확장된기능 (2/7) 4 형식지정자의예 int num; printf("%c %d %f\n", 'A', 123, 456.789); printf("%d %u\n", 2, 2); printf("%d %x %o\n", 26, 26, 26); printf("%s\n", "Hello world"); printf("%f %e %g\n", 123.45, 123.45, 123.45); printf("%s %p\n", "Hi", "Hi"); printf("1234\n%n", &num); printf("num = %d\n", num); return 0; Q: 정상실행이되는가?

printf() 의확장된기능 (3/7) 5 형식지정자의확장 형식지정자앞에출력양식변환수정자가사용될수있음 printf(...%[ ][m][.][n][h l]*, ); // 형식지정자의확장예 1 int var_int; short var_short; long var_long; float var_float; double var_double; var_int = 4; var_short = 2; var_long = 4; printf("%d %hd %ld\n", var_int, var_short, var_long); printf("%hu %u\n\n", 65536 + 1, 65536 + 1); var_float = 123.456; var_double = 123.456; printf("%f %lf\n\n", var_float, var_double); // 형식지정자의확장예 2 printf("%d\n", 1234); printf("%d\n", 1234); printf("% 10d\n\n", 1234); printf("%f\n", 234.5678); printf("%10.2f\n", 234.5678); printf("%10.6f\n", 234.5678); printf("% 10.2f\n\n", 234.5678); Q: printf( %d n, 1234) 에서 %2d 로변경하면?

printf() 의확장된기능 (4/7) 6 The first alphabet is A 를출력하고싶다. 어떻게할까? char ch; ch = 'A'; printf("the first alphabet is %c", ch); O.K. 대소문자, 숫자출력가능. 그럼 는어떻게출력할수있을까? 또한 Enter, Back Space, Tab 등의특수문자는어떻게프로그램에서수행시킬수있을까? Escape Sequence ( 역슬래쉬문자상수 )

printf() 의확장된기능 (5/7) Escape sequence ( 역슬래쉬문자상수 ) 7 b backspace f form feed n new line r carriage return t horizontal tab v vertical tab 0 null character a bell double quotation single quotation backslash? question mark N 8진수상수 xn 16진수상수 #include <string.h> // strlen() 을위해포함 printf("1234\n567\n"); printf("1234\b567\n"); printf("1234\f567\n"); printf("1234\r567\n"); printf("1234\t567\n"); printf("1234\v567\n"); printf("1234\a567\n"); printf("\"why\?\"\n"); printf("length = %d\n", strlen("abc\0d")); // \0 테스트를위한인위적인코드

printf() 의확장된기능 (6/7) 8 역슬래쉬문자들은 ASCII 문자코드에대응 0~32 : 제어문자 (bell 은 7, BS 는 8, HT 은 9, CR 은 13) 33~47 : 특수문자 (! 는 33, 는 34, / 는 47) 48~57 : 숫자 ( 0 은 48, 1 은 49, 9 는 57) 65~90 : 알파벳대문자 ( A 는 65, B 는 66, Z 는 90) 97~122 : 알파벳소문자 ( a 는 97, b 는 98, z 는 122) 그외 : 특수문자 ( : 은 58, [ 는 91, 는 123 ) printf("1234\t567\n"); printf("1234%c567\n", '\t'); printf("%d\n", '\t'); printf("1234%c567\n", 9); return 0;

printf() 의확장된기능 (7/7) 9 ASCII 코드사용예 int i; i = 49; printf("%d %c\n", i, i); printf("%c %d\n", 'A', 'A'); printf("%c %d\n", 65, 65); printf("%c %d\n", 0x41, 0x41); printf("%c %d\n", '\x41', '\x41'); printf("1234%c567\n", '\t'); printf("1234%c567\n", 9); printf("1234%c567\n", '9'); A == 0x41 ASCII 코드에서 9 번은 Horizontal Tab 에매핑. ASCII 코드에서 57 번은숫자 9 에매핑 if ('\b' == '7') printf("same\n"); else printf("different\n"); for (i = 0; i <= 127; i++) // 모든 ASCII 문자출력 printf("%d th ASCII code = %c\n", i, i);

scanf() 의확장된기능 10 함수의형식 scanf (...%*, ); 정수 : %d, %o, %x 실수 : %f, %lf (double), %Lf (long double) int num; float f; double d; printf("enter an Integer: "); scanf("%d", &num); printf("enter a floating point number: "); scanf("%f", &f); printf("enter a floating point number again: "); scanf("%lf", &d); printf("%d ", num); printf("%f ", f); printf("%lf", d); return 0;

함수 (1/7) 11 함수의정의 작업을수행하기위한문장 (statement) 들의그룹모든 C 프로그램은하나이상의함수가존재 : main() 모든프로그램은추가함수정의가가능

함수 (2/7) 12 함수가필요한예 -1 int x, y; x = 4; y = 2; printf("==========================\n"); printf("%d + %d = %d\n", x, y, x + y); printf("==========================\n"); printf("%d %d = %d\n", x, y, x y); printf("==========================\n"); printf("%d * %d = %d\n", x, y, x * y); printf("==========================\n"); printf("%d * %d = %d\n", x, y, x / y); return 0;

함수 (3/7) 13 함수가필요한예 -2 int x = 2; printf("%d x %d = %d\n", x, 1, x * 1); printf("%d x %d = %d\n", x, 2, x * 2); printf("%d x %d = %d\n", x, 3, x * 3); printf("%d x %d = %d\n", x, 4, x * 4); printf("%d x %d = %d\n", x, 5, x * 5); printf("%d x %d = %d\n", x, 6, x * 6); printf("%d x %d = %d\n", x, 7, x * 7); printf("%d x %d = %d\n", x, 8, x * 8); printf("%d x %d = %d\n", x, 9, x * 9);

함수 (4/7) 14 함수형식 return_type function_name(parameter_lists) statements; statements; void func1(void); printf("i "); func1(); printf("c.\n"); void func1(void) printf("like "); return;

함수 (5/7) 15 함수제어흐름 (control flow) main() func1() 함수내부에서함수를호출하면? 응용이두개이상수행되는경우흐름도는? (multitask, multithread)

함수 (6/7) 16 함수가필요한예 1 (update) Q: PrintLine() 함수를 main() 함수뒤에작성하려면? void PrintLine() printf("==========================\n"); int x, y; x = 4; y = 2; PrintLine(); printf("%d + %d = %d\n", x, y, x + y); PrintLine(); printf("%d %d = %d\n", x, y, x y); PrintLine(); printf("%d * %d = %d\n", x, y, x * y); PrintLine(); printf("%d * %d = %d\n", x, y, x / y); return 0;

함수 (7/7) 17 함수가필요한예 2 (update) void Multi_Table(int x); Multi_Table(2); Multi_Table(5); void Multi_Table(int x) printf("%d x %d = %d\n", x, 1, x * 1); printf("%d x %d = %d\n", x, 2, x * 2); printf("%d x %d = %d\n", x, 3, x * 3); printf("%d x %d = %d\n", x, 4, x * 4); printf("%d x %d = %d\n", x, 5, x * 5);...

함수실습 18 인자와리턴값이있는경우 int Add(int num1, int num2) return num1 + num2; int main(void) int result; Q: 함수로가능한가? result = Add(3, 4); printf("result1: %d \n", result); result = Add(5, 8); printf("result2: %d \n", result); return 0;

if 조건문 (1/3) 19 인생은선택의연속임!! if 조건문형식 if (expression) statement; expression 이 0 이아니면참, 0 이면거짓 int main(void) int num; printf("enter a number: "); scanf("%d", &num); 관계연산자 (>, ==, <,!=, ) 산술연산자보다우선순위가낮음 if (num < 0) printf("number is negative\n"); if (num > 1) printf("number is non negative\n"); return 0;

if 조건문 (2/3) 20 예제프로그램 다음프로그램의기능은? int main(void) int answer; printf("what is 10 + 14? "); scanf("%d", &answer); if (answer == 10 + 14) printf("right!"); if (answer!= 10 + 14) printf("wrong!"); return 0;

if 조건문 (3/3) 21 연습문제 : 정수를입력받아홀수인지짝수인지알려주는프로그램작성 int main(void) int num; printf("enter a number: "); scanf("%d", &num); if ((num % 2) == 0) printf("number is even\n"); if ((num % 2) == 1) printf("number is odd\n"); return 0;

else 의사용 (1/2) 22 형식 if (expression) statement; else statement; else 를사용해다시작성하면 int main(void) int num; printf("enter a number: "); scanf("%d", &num); if ((num % 2) == 0) printf("number is even\n"); else printf("number is odd\n"); return 0;

else 의사용 (2/2) 23 Example of Exception Handling ( 예외처리예제 ) int main(void) int num1, num2; if (num2 == 0) printf("cannot divide by zero\n"); else printf("answer is %d\n", num1/num2); printf("enter the first number: "); scanf("%d", &num1); printf("enter the second number: "); scanf("%d", &num2); printf("answer is %d\n", num1/num2); return 0; What s wrong? 실습시간에반드시해보세요. 재미있는결과가.. 소수점까지계산하려면?

코드블록생성 (1/2) 24 연관된문장들을 block 으로만들수있음 논리적인하나의단위가됨 사용, code block은독립된스택사용 float num; int choice; printf("1:feet to meters, 2: meters to feet\n"); printf("enter choice: "); scanf("%d", &choice); if (choice == 1) printf("enter number of feet: "); scanf("%f", &num); printf("meters: %f\n", num / 3.28); else printf("enter number of meters: "); scanf("%f", &num); printf("feet: %f\n", num * 3.28); 이프로그램의문제는? 항상예외처리고려 code block: logically single unit!!

코드블록생성 (2/2) 25 예제수정 int choice; printf("1:feet to meters, 2: meters to feet\n"); printf("enter choice: "); scanf("%d", &choice); if (choice == 1) float num; printf("enter number of feet: "); scanf("%f", &num); printf("meters: %f\n", num / 3.28); else float num; printf("enter number of meters: "); scanf("%f", &num); printf("feet: %f\n", num * 3.28);

연습문제 26 if 문 두정수를입력받아두수의차를출력 두수의차는함수로작성 ( 차를리턴값으로받음 ) 두수의차는항상큰값에서작은값을뺀값임 예 ) 12 와 7 입력시값은 5, 4 와 16 입력시값은 12 Enter 2 integer : 4 16 Differnce : 12

for 반복문사용 (1/6) 27 for 반복문형식 for (initialization; conditional test; operation) statement; or code block 1 에서 10 까지의합은? int i; int total = 0; for (i=1; i<=10; i=i+1) total = total + i; printf("total = %d\n", total); initialization conditional test operation(s) at each iteration

for 반복문사용 (2/6) 28 for 반복문의다양한예 // for 반복문의예 // for 반복문의예계속 int i, total; total = 0; for (i=1; i<10; i=i+1) total = total + i; printf("total = %d\n", total); total = 0; for (i=1; i<10; i=i+3) total = total + i; printf("total = %d\n", total); total = 0; for (i=10; i>5; i=i 1) total = total + i; printf("total = %d\n", total); total = 0; for (i=10; i<10; i=i+1) total = total + i; printf("total = %d\n", total); total = 0; for (i=1; i<10; i=i+1) if ((i % 2) == 0) total = total + i; printf("total = %d\n", total); total = 0; for (i=1; i<10; i=i+1); total = total + i; printf("total = %d\n", total); return 0; 좀더좋은다른방법없을까?

for 반복문사용 (3/6) 29 연습문제 1 1) 0 부터 100 까지정수를출력하시오. 2) 0 부터 100 까지정수중에짝수만출력하시오. int i; for (i = 1; i <= 100; i = i + 1) printf("%d ", i); return 0;

for 반복문사용 (4/6) 30 연습문제 2 0 이상의정수값을입력받아정수값까지합을구하시오. 조건 : 최대정수값은 50 input integer number : 10 total = 55

for 반복문사용 (5/6) 31 연습문제 3 5 이하의정수중에소수 (prime number) 를찾는프로그램을작성하시오. 소수를찾는부분은함수로작성하시오. enter any integer: 15 15 in not prime number enter any integer: 13 13 is prime number enter any integer:...

for 반복문사용 (6/6) 32 initialization, condition test, operation 은생략가능. 무한루프를돌거나, for 루프를한번도수행하지않게할수있음 콤마연산자사용가능 for 반복문은매우중요!!. 실제실습을많이해보시길 6 자리 lotto 복권번호를예상해주는프로그램작성 #include <stdlib.h> /* int rand(void) 함수를위해포함 */ main() int i, num; for (i=1; i<=6; i++) num = rand(); num = num % 46; printf("%d th number = %d\n", i, num); 위프로그램은 3 가지정도의문제가있다. 수정해보세요. 과제

연습문제 33 수알아맞추기 컴퓨터에서임의의숫자생성 (0~500 사이 ) 사용자가숫자를맞추기위해서 10번까지숫자입력 입력한값이정답과같으면 Right 출력 입력한값이정답이아니면정답보다큰지작은지알려줌 I select a number from 0 to 500. Guess what the number is? 1 th Try.. Guess the number: 100 Sorry.. The number I've selected is larger than 100. 2 th Try.. Guess the number: 400 Sorry.. The number I've selected is larger than 400. 3 th Try.. Guess the number: 450 Sorry.. The number that I've selected is smaller than 450. 4 th Try.. Guess the number: 432 Sorry.. The number I've selected is larger than 432.

이장의결론 34 printf 함수 서식문자, Escape sequence 문자 scanf 함수 함수 printf 서식문자와유사 함수의정의및필요성, 사용자정의함수 대표적제어문인 if와 for 문을이용한프로그램작성코드블록 (code block) 의의미이해 for 반복문이해