Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6 WEEK 1 - INTRODUCTION TO VOID FUNCTIONS (PROCEDURES)

Similar presentations


Presentation on theme: "Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6 WEEK 1 - INTRODUCTION TO VOID FUNCTIONS (PROCEDURES)"— Presentation transcript:

1 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6 WEEK 1 - INTRODUCTION TO VOID FUNCTIONS (PROCEDURES)

2 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 2 학습목표 1. 모듈성 (modularity ) 의 중요성 학습 2. 함수 ( 프로시져 ) 개발과 호출 방법 학습 3. 함수원형 (prototype) 의 역할 4. 매개변수 (parameter) 가 없는 함수 5. 함수에 매개변수 전달 방법 6. 실 매개변수와 형식매개변수 이해 7. 값에 의한 전달 vs. 참조에 의한 전달 8. 소형 프로그램 개발 ( 프로시져 )

3 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 3 LAB ASSIGNMENTS( 실습과제 ) Lesson 6A Lab 6.1: 매개변수가 없는 함수 Lab 6.2: 값에 의한 전달 Lesson 6B Lab 6.3: 참조에 의한 전달 Lab 6.4: Student-Generated Code Assignments

4 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 4 Lab 6.1 매개변수가 없는 함수 http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab 에서 download “ proverb.cpp ” // This program prints the proverb // "Now is the time for all good men to come to the aid of their party." // in a function (procedure) called by the main function Exercise 1: proverb.cpp 프로그램의 시작 부분에 있는 주석의 격언을 출력하도록 프로그램을 완성하라. main 함수에서 proverb 함수를 호출하게 하고, 호출된 proverb 함수에서 격언을 출력하게 하라. http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab

5 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 5 // This program prints the proverb // "Now is the time for all good men to come to the aid of their party." // in a function (procedure) called by the main function. // PLACE YOUR NAME HERE. #include using namespace std; void proverb(); // prototype for the proverb function int main() { // Fill in the code to call the proverb function. return 0; } /*********************************************************** * proverb * * This function prints a proverb. * ***********************************************************/ void proverb() { // Fill in the body of the function that prints to the screen // the proverb given in the comments at the beginning of the program. } proverb.cpp

6 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 6 Lab 6.2 값에 의한 전달 실습 http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab 에서 download “newproverb.cpp” Exercise 1: 다음과 같이 동작하도록 프로그램을 완성하라. asks the student to complete the program so that it allows the user to choose, by inputting a number, what the last word of the proverb will be. An input of 1 should cause the proverb to end with the word “party” and any other number to have it end with the word “country.” http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab

7 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 7 // This program will allow the user to input from the keyboard whether // the last word to the following proverb should be party or country. // "Now is the time for all good men to come to the aid of their ________. // Inputting a 1 will print "party". Any other number will print "country". // PLACE YOUR NAME HERE. #include using namespace std; // Fill in the prototype of the proverb function. int main () { int wordCode; cout << "Given the phrase:" << endl; cout << "Now is the time for all good men to come to the aid of their ___\n"; cout << "Input a 1 if you want the sentence to be finished with party.\n"; cout << "Input any other number for the word country.\n"; cout << "Please input your choice now.\n"; cin >> wordCode; cout << endl; proverb(wordCode); return 0; } newproverb.cpp

8 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 8 /********************************************************************* * proverb * * This function takes number from the call. If the number is a 1 * * it prints "Now is the time for all good men to come to the aid of* * their party." Otherwise it prints "Now is the time for all good * * men to come to the aid of their country." * *********************************************************************/ void proverb (int number) { // Fill in the body of the function to accomplish what is described above. }

9 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 9 Exercise 2: 위 프로그램을 수정하여 입력 값이 1 일 경우는 끝에 “party” 를 2 일 경우는 “country” 그리고 다른 값일 경우는 오류 메시지와 함께 다시 입력을 시도하게 하라. ( 자료 검증을 위한 루프문 사용 ) Given the phrase: Now is the time for all good men to come to the aid of their ___ Input a 1 if you want the sentence to be finished with party. Input a 2 if you want the sentence to be finished with country. Please input your choice now. 4 I am sorry but that is an incorrect choice; Please input a 1 or 2. 2 Now is the time for all good men to come to the aid of their country.

10 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 10 Exercise 3: 위 프로그램을 수정하여 사용자가 격언의 마지막 단어를 입력하게 하라. (proverb 함수 매개변수 자료형의 수정 요구 !) Given the phrase: Now is the time for all good men to come to the aid of their ___ Please input the word you would like to have finish the proverb. family Now is the time for all good men to come to the aid of their family.

11 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 11 Lab 6.3 참조에 의한 전달 실습 http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab 에서 download “paycheck.cpp ” http://ce.kyungil.ac.kr/~kykim/OOP2009/Lab // This program takes two numbers (pay rate and hours) and multiplies them // to get gross pay. It then calculates net pay by subtracting 15%. Exercise 1: paycheck.cpp 를 완성하라. 사용자로부터 시간당 수당 금액과 근무 시간을 입력 받아 함수 paycheck 함수에서 grosspay 와 netpay 를 계산한 다음 이들 값을 참조에 의해 전달된 main 함수에서 출력하게 하라.

12 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 12 // PLACE YOUR NAME HERE. #include using namespace std; // Function prototypes void description(); void paycheck(double, int, double&, ________ ); int main() { double payRate, grossPay, netPay; int hours; cout << setprecision(2) << fixed; cout << "Welcome to the Payroll Program." << endl; description(); cout << "Please input the pay per hour: "; cin >> payRate; cout << endl << "Please input the number of hours worked: "; cin >> hours; cout << endl; paycheck(payRate, hours, grossPay, ______ ); // Write statements to print out both gross pay and net pay, with labels. cout << "We hope you enjoyed this program." << endl; return 0; } paycheck.cpp

13 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 13 /******************************************************* * description * * This function prints a program description * *******************************************************/ void description() { cout << "************************************************" << endl << endl; cout << "This program takes two numbers (pay rate and hours)" << endl; cout << "and outputs net pay. "<< endl; cout << "************************************************" << endl << endl; } /******************************************************* * paycheck * * This function computes gross and net pay, placing * * the results in reference parameters. * *******************************************************/ void paycheck(double rate, int time, double& gross, ______________) { // Write an assignment statement to compute gross pay. // Write an assignment statement to compute net pay. }

14 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 14 Exercise 2: 다음과 같은 자료에 대해 출력 결과가 나오게 프로그램을 실행하여 보자. Welcome to the Payroll Program. ************************************************ This program takes two numbers (pay rate and hours) and outputs net pay. ************************************************ Please input the pay per hour: 9.50 Please input the number of hours worked: 40 The gross pay is $380 The net pay is $323 We hope you enjoyed this program.

15 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 15 Exercise 3: 프로그램의 paycheck 함수 내에서 gross 와 net 변수는 값에 의한 전달인가 ? 참조에 의한 전달인가 ? 값에 의한 전달을 사용하면 결과는 ? Exercise 4: gross 값과 net 값이 main 함수 대신에 paycheck 함수에서 출력되도록 수정하여 보자. 참조에 의한 전달을 사용할 필요가 있나 ? Exercise 2 에서 사용한 자료로 실행하여 보자. 같은 결과를 보여주는가 ?

16 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 16 All of the options for this lab require students to use functions and to pass parameters correctly. 선택 1: 두 수를 입력 받아 서로 값을 바꾸는 프로그램을 작성하라. 참조에 의한 전달을 사용하라. 첫 번째 수를 입력하시오 80 두 번째 수를 입력하시오. 70 입력한 수는 80 과 70 입니다. Swap 후 첫 번째 수는 80 입니다. Swap 후 두 번째 수는 70 입니다. Lab 6.4 독립적인 소형 프로그램 개발

17 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 17 Option 2: This is a simple miles per hour problem requiring miles and hours to be passed to a function which will compute milesPerHour. The problem statement specifically requires that milesPerHour be passed as a parameter. Thus, even if a student understands value returning functions (not introduced until next week’s labs), one should not be used here because the idea is to know which parameters should be passed by value and which by reference. For a correct solution miles and hours should be passed by value and milesPerHour by reference. Option 3: This program has main call a convert function to convert the hours and minutes passed to it into hours. The problem statement does not specify whether there are to be two or three parameters. If three are used, hours and minutes should be passed by value and convertedHours, which holds the result, should be passed by reference. If only two parameters are passed, with the idea that hours will be updated to hold the resulting total hours, then minutes must be passed by value and hours must be passed by reference. Option 4: This option combines Options 2 and 3. First a convert function converts minutes and hours into hours so that this result can be passed to a mph function, along with miles traveled, to obtain the milesPerHour.

18 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 18 Thanks!!!!!!!!


Download ppt "Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 실 습실 습 6 WEEK 1 - INTRODUCTION TO VOID FUNCTIONS (PROCEDURES)"

Similar presentations


Ads by Google