FAKULTI TEKNOLOGI KEJURUTERAAN KEJURUTERAAN UNIVERSITI TEKNIKAL MALAYSIA MELAKA
COMPUTER PROGRAMMING
BETR 1343
SEMESTER 2
SESSION 2016/2017
LAB 4: DEVELOPING PROGRAMS USING FUNCTIONS
DATE
11/5/17 SUHANTHAN A/L RAVINTHERAN B071610805
NAME OF GROUP G ROUP MEMBERS & MATRIX NUMBER
3. 1. NAME OF INSTRUCTORS INSTRUCTORS 2.
2
VERIFICATION STAMP
EXAMINER’S COMMENTS
TOTAL MARKS
3
1.0
LEARNING OUTCOMES
1. To apply function functio n technique in the program development in C++ programming. programming. 2. To apply function call call and function with with returning returning value 3. To identify error and troubleshoot troublesho ot a C++ program
2.0
EQUIPMENTS
1. Personal Computer 2. DEV C++
3.0
SYNOPSIS & THEORY
A function is function is a group of statements that is executed when it is called from some point of the program. In C++, a program is made of one or more functions. One and only one of which must be named int main( ).
A function is an independent independent module that will be called to do specific task. t ask. Each function has its own name. name. When that name is encountered in a program, the execution of the program branches to the body of that function. function . When the function functio n is finished, execution returns to the area of the program code from which it was called, and the program continues on to the next line of code.
2
Type of function;
Pre-defined Pre-def ined (or Standard) Function
User-defined Function
Pre-defined (or Standard) Function Example :mathematical functions in (called cmath header file) are: The power function, pow(x,y), The square root function, sqrt(x), The floor function, floor(x) etc.
User-defined Function It has three parts:
Function declaration - is is done done first with with a prototype prototype declaration.
Function definition - contains contains the code to complete complete the task.
Function call (or invocation) invocation) - to call function to perform perform its its task. task.
To use this type of function, the programmer has to call (or invoke) the function. But the function must first be declared and defined before it can be used.
Please refer to lecture slides for more details about the how to write function declaration, function call and function definition.
This lab we will investigate some application C++ programming which is applying various types of function in the program development process, include function with and without parameter, with and without return value.
4.0
PROCEDURE 3
PART A: EXERCISES
Section 1: Functions with No Parameters
1. Open your preferred C++ IDE, and create new console project or file. 2. Type the following program code 4.1:
1. // Student Name: xxxxxxxx Student ID: xxxxxxx 2. // Lab Title: do-while loop Date: xx/xx/xxxx 3. 4. #include 5. using namespace std; 6. //--------Global declaration functions and variables areas 7. void display_text(); 8. 9. //--------Main function 10.
int main()
11.
{
12.
cout<<"Function to display output text and no parameter involve."<
13.
display_text();
14.
system("PAUSE");
15.
}//Ending main loop function
16. 17.
//--------Functions areas
18.
void display_text ()
19.
{
20.
cout<<"Welcome. This text execute form function 'display_text'"<
21.
return;
22.
}//Ending display_text function 4
3. Compile, run your program, and write your output. Please use your own paper.
Function to display output text and no parameter involve. Welcome.This text execute form function ‘display_text’ Press any key to t o continue…
[2 marks]
4. Determine the location location of the following.
No. of Line Function Declaration
7
Function Calling
13
Function Definition
18 [3 marks]
5. Explain the difference between a function declaration and declaration and function definition? definition? 5
Function declaration is a prototype declaration while the function definition is contains the statements that make up the function. [2 marks]
6. In general format, function definition has definition has two parts. Explain. Function definition has two parts which is f unction header and function body.
[2 marks]
7. Add another function, function, with function name name is “display_text2 ” (to write any text output). Compile and run. Write your code in your report.
#include using namespace std; variables areas
//--------Global declaration functions and
void display_text1(); void display_text2(); //--------Main function int main() { cout<<"Function cout<<"Function to display output text and no parameter involve."<
system("PAUSE"); }
//Ending main loop function
//--------Functions areas void display_text1() { cout<<"Welcome. cout<<"Welcome. This text execute form function 'display_text1'"<
}
//Ending display_text display_text function
void display_text2() display_text2() {
cout<<"Welcome. cout<<"Welcome. This text execute form function 'display_text2'"<
Section 2: Functions with Parameters Passing By Value
1. Open your preferred C++ IDE, and create new console project or file. 2. Type the following program code 4.2:
1. // Student Name: xxxxxxxx Student ID: xxxxxxx 2. // Lab Title: do-while loop Date: xx/xx/xxxx 3. #include 4. using namespace std; 5. //--------Global declaration functions and variables areas 6. int subtraction (int,int); 7. 8. int main () 9. { 10.
int num_1, num_2;
11.
int answer;
12.
cout<<”Enter first number : “;
13.
cin>> num_1;
14.
cout<<”Enter second number : “;
7
15.
cin>> num_2;
16.
cout<
17. 18.
answer = subtraction (7,2);
19.
cout << “The first result is “ << answer << ‘\n’;
20.
cout << “The second result is “ << subtraction (7,2) << ‘\n’;
21.
cout << “The third result is “ << subtraction (num_1,num_2) << ‘\n’;
22.
answer = 4 + subtraction (num_1,num_2);
23.
cout << “The fourth result is “ << answer << ‘\n’;
24.
system (“PAUSE”);
25.
return 0;
26.
}//Ending main loop function
27. 28.
//--------Functions areas
29.
int subtraction (int a, int b)
23.
{
24.
int return_value;
25.
return_value=a-b;
26.
return (return_value);
27.
}
3. Compile, run your program. Enter the following inputs;
Enter first number : Enter second number :
90 80
4. Write down your output. Use your own paper. Enter first number :90 Enter second number :80
8
The first result is 5 The second result is 5 The third result is 10 The fourth result is 14 Press any key to continue… [2 marks] 5. Run your program with with the following inputs;
Enter first number : Enter second number :
90.98 80.97
6. Do you have the output answer? answer? Write your observation? observation? Yes .but the first number can be key in and operation is running and the second cannot key in [1 marks]
7. Now, you need to modify modify the program 4.2 to get get the correct answer. answer. Write down your code in your report.
#include using namespace std; //--------Global declaration functions and variables areas float subtraction (float,float);
int main () { float num_1, num_2; float answer; cout<<"Enter first number : "; cin>> num_1; cout<<"Enter second number : "; cin>> num_2; 9
cout<
answer = subtraction (7,2); cout << "The first result is " << answer <
system("pause"); }//Ending main loop function
//--------Functions areas float subtraction (float a, float b) { float return_value; return_value=a-b; return (return_value); (return_value); } [7 marks]
10
Section 3: Functions with Parameters Passing by Reference
1. Open your preferred preferred C++ C++ IDE, and create create new console console project project or file. 2. Type the following program code 4.2:
1. // Student Name: xxxxxxxx Student ID: xxxxxxx 2. // Lab Title: do-while loop Date: xx/xx/xxxx' 3. #include 4. using namespace std; 5. //--------Global declaration functions and variables areas 6. int multiplication(int a, int b, float &result); 7. int main () 8. { 9. int num_1, num_2; 10. float answer=0, result; 11. cout<<"Enter first number : "; 12. cin>> num_1; 13. cout<<"Enter second number : "; 14. cin>> num_2; 15. cout<
21. return 0; 22. }//Ending main loop function 23. //--------Functions areas 24. int multiplication(int a, int b, float &result) 25. { 26. result = a*b; 27. return result; 28. }
3. Compile, run your program. Enter the following inputs;
Enter first number : Enter second number :
9 3
4. Write down your output. Use your own paper. Enter the first number:9 Enter the second number:3
The contain in answer before pass by reference:0 The result is 27 The contain in answer before pass by reference:27 Press any key to continue…_
[2 marks]
5. Run your program with with the following inputs; 12
Enter first number : Enter second number :
90.98 80.97
6. Do you have the output answer? answer? Write your observation? observation? Can run.The program can run but not complete because only can run the first number while number second does not run complete. [1 marks]
7. Now, you need to modify modify the program 4.2 to get get the correct answer. answer. Write down down your code in your report.
#include using namespace std; //--------Global declaration functions and variables areas float subtraction (float,float);
int main () { float num_1, num_2; float answer; cout<<"Enter first number : "; cin>> num_1; cout<<"Enter second number : "; cin>> num_2; cout<
answer = subtraction (7,2); cout << "The first result is " << answer <
cout << "The fourth result is " << answer <
system ("pause"); }//Ending main loop function
//--------Functions areas float subtraction (float a, float b) { float return_value; return_value=a-b; return (return_value); (return_value); } [7 marks]
PART B: PROBLEM SOLVING
Section 1: System Requirement
Write a C++ program based on the following requirements:
i.
Write a program to determine determine whether a person qualifies for a credit card. 14
ii.
To qualify, the person must have worked on his or her current job for at least 2 years and make at least RM 35000 per year.
iii.
Write 2 function after inserting how many years working and total salary per year:
Function Qualify Should explain that the applicant qualifies for the card and the annual interest rate is 12800
Function No_Qualify Should explain that the applicant does not qualify for the card and give general explanation why.
iv.
Copy+paste your program code and submit it with your lab report.
Example of program output:
25
15
Section 2: System Analysis
Input
Salary ,years
Process
Output
if(salary>35000 && years>-2)
qualify
qualify();
Congratulation!! You are qualify for our credit card The annual interest rate for our card is i s 2% per year.Please call 1800-800-800 for our services details.Thank you
else no_qualify();
no_qualify Dear Customer,We are pleased to inform you that you are not qualified for our credit card Thank You.Please Come Again
16
Section 3: System Design
1.Main function
Start
Enter salar ear
(salary>35000 && years>-2)
Display No ualif
Display Qualify
End
17
2.Qualify function
Start
18
Congratulation!!You are qualify for our credit card The annual interest rate for our card is 2% per year.Please call 1800800-800 for our services details.Thank you
End
3.No Qualify
Start
Dear Customer,We Customer,We are pleased to inform you that you are not qualified for our credit card Thank You.Please Come Again
19
End
Section 4: Coding
#include using namespace std; void qualify(); void no_qualify(); int main()
{ float salary,years;
cout<<"This program will determine if you qualify for our credit card"<>salary; cout<<"How many years have you worked at your current jobs"<>years;
20
if(salary>35000 && years>-2)
qualify();
else
no_qualify();
system("pause"); } void qualify() { cout<<"Congratulation!! You are qualify for our credit card "<
to inform you"<
cout<<"that you are not qualified for our credit card "<
21
Section 5: System Testing
**Show the input you have inserted and also als o the program output console.
Test 1 Input
Salary: 36000; Years: 4;
22
Output
Test n Input
Salary:34000; Years:3;
Output
23
PART C: DISCUSSION & CONCLUSION
1. Discussion Based on discussion,we discussion,we could know 3 parts of of function which is no data communication, one way data communication and two way data communication. communication. Besides ,if we had more time we also can improve by doing this program in goodly. In making the improvement the function program ,must have many exercise to make students get easier to understand.
2. Conclusion While doing this program or lab ,we be able to easier to understand and we also can doing the program goodly. However, our first impression we think this program too hard hard but by doing many exercise exercise such as compile the program program it will make students easier to understand.
24