Lab 4 Programming
Lab 4 Programming
COMPUTER PROGRAMMING
DATE 11/5/17
3.
1.
NAME OF INSTRUCTORS
2.
2
VERIFICATION STAMP
EXAMINERS COMMENTS
TOTAL MARKS
3
1.0 LEARNING OUTCOMES
2.0 EQUIPMENTS
1. Personal Computer
2. DEV C++
A 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 module that will be called to do specific task. Each function
has its own name. When that name is encountered in a program, the execution of the
program branches to the body of that function. When the function 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;
User-defined Function
The power function, pow(x,y), The square root function, sqrt(x), The floor function, floor(x)
etc.
User-defined Function
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
1. Open your preferred C++ IDE, and create new console project or file.
3.
4. #include<iostream>
7. void display_text();
8.
9. //--------Main function
11. {
13. display_text();
14. system("PAUSE");
16.
19. {
21. return;
[2 marks]
No. of Line
Function Declaration 7
Function Calling 13
Function Definition 18
[3 marks]
5
Function declaration is a prototype declaration while the function definition is
contains the statements that make up the function.
[2 marks]
Function definition has two parts which is function header and function body.
[2 marks]
7. Add another function, with function name is display_text2 (to write any text
output). Compile and run. Write your code in your report.
#include<iostream>
void display_text1();
void display_text2();
//--------Main function
int main()
display_text1();
display_text2();
system("PAUSE");
//--------Functions areas
void display_text1()
void display_text2()
[3 marks]
1. Open your preferred C++ IDE, and create new console project or file.
3. #include <iostream>
7.
8. int main ()
9. {
16. cout<<endl<<endl;
17.
19. cout << The first result is << answer << \n;
23. cout << The fourth result is << answer << \n;
25. return 0;
27.
23. {
25. return_value=a-b;
27. }
8
The first result is 5
[2 marks]
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 the program 4.2 to get the correct answer. Write down
your code in your report.
#include <iostream>
int main ()
float answer;
cin>> num_1;
cin>> num_2;
9
cout<<endl<<endl;
cout << "The second result is " << subtraction (7,2) <<endl;
cout << "The third result is "<< subtraction (num_1,num_2) << endl;
system("pause");
//--------Functions areas
float return_value;
return_value=a-b;
return (return_value);
[7 marks]
10
Section 3: Functions with Parameters Passing by Reference
1. Open your preferred C++ IDE, and create new console project or file.
3. #include <iostream>
7. int main ()
8. {
15. cout<<endl<<endl;
16. cout << "The contain in answer before pass by reference: "
<< answer << '\n';
18. cout << "The result is " << result << '\n';
19. cout << "The contain in answer after pass by reference: " <<
answer << '\n';
11
21. return 0;
25. {
28. }
The result is 27
[2 marks]
12
Enter first number : 90.98
Enter second number : 80.97
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 the program 4.2 to get the correct answer. Write down
your code in your report.
#include <iostream>
int main ()
float answer;
cin>> num_1;
cin>> num_2;
cout<<endl<<endl;
cout << "The second result is " << subtraction (7,2) <<endl;
cout << "The third result is "<< subtraction (num_1,num_2) << endl;
13
cout << "The fourth result is " << answer <<endl;
system ("pause");
//--------Functions areas
float return_value;
return_value=a-b;
return (return_value);
[7 marks]
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.
25
15
Section 2: System Analysis
no_qualify
16
Section 3: System Design
1.Main function
Start
Enter
salary,year
(salary>35000 Display
&& years>-2) No_Qualify
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 1800-
800-800 for our services details.Thank
you
End
3.No Qualify
Start
19
End
Section 4: Coding
#include <iostream>
void qualify();
void no_qualify();
int main()
float salary,years;
cin>>salary;
cin>>years;
20
if(salary>35000 && years>-2)
qualify();
else
no_qualify();
system("pause");
void qualify()
void no_qualify()
cout<<"that you are not qualified for our credit card "<<endl;
21
Section 5: System Testing
**Show the input you have inserted and also the program output console.
Test 1
Years: 4;
22
Output
Test n
Input Salary:34000;
Years:3;
Output
23
PART C: DISCUSSION & CONCLUSION
1. Discussion
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 but by doing many exercise such as compile the program it
will make students easier to understand.
24