Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Gulis Laboratory 6 PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Course: CPE 102A - Computer Programming Experiment No.

: 6
Name: John Kenneth S. Gulis Section: EE21S1
Date Performed: November 19, 2021
Date Submitted: November 19, 2021
Instructor: Engr. Julie Ann Susa

LABORATORY ACTIVITY NO. 6


FUNCTIONS
1. Objective(s):

The activity aims to perform a certain task a small block using analytical tools such as algorithm, flowchart and
program to evaluate the output based on the requirement.

2. Intended Learning Outcomes (ILOs):


The students shall be able to:

1. Understand the use of functions in a program and how to call function


2. Design a modularized program using function
3. Construct a program using user-defined function

3. Discussion:
A function is a block of code that has a name and it has a property that it is reusable i.e. it can be executed from as
many different points in program. Function groups a number of program statements into a unit and gives it a name. The
name of the function is unique and global.
Structure of a Function
There are two main parts of the function.
1) The function header - It has three main parts
a. The name of the function i.e. sum
b. The parameters of the function enclosed in parenthesis
c. Return value type i.e. int
2) The function body.
Whatever is written with in { } in the above example is the body of the function.

Function Prototypes
The prototype of a function provides the basic information about a function which tells the compiler that the function is
used correctly or not. It contains the same information as the function header contains. The prototype of the function
must have a semicolon at the end of the prototype.
NOTE: The only difference between the header and the prototype is the SEMICOLON (;)
Syntax:
#include<iostream>
using namespace std;
void MyPrint()
{
cout << "Printing from a function.\n";
}
int main ()
{
MyPrint();
return 0;
}
Parameters and return
Functions can accept parameters and can return a result. Where the functions are declared in your program does not
matter, as long as a functions name is known to the compiler before it is called. In other words: when there are two
functions, i.e. functions A and B, and B must call A, than A has to be declared in front of B.
Global and Local variables
• A local variable is a variable that is declared inside a function.
• A local variable can only be used in the function where it is declared.
• A global variable is a variable that is declared outside ALL functions.
• A global variable can be used in all functions.
Syntax:
#include<iostream>
using namespace std;
int A, B; Global Variables
int Add ()
{
return A + B;
}
int main ()
{
int answer; Local Variables
A = 2;
B = 2;
answer = Add ();
cout << A << " + " << B << " = " << answer;
return 0;
}

4. Resources:

DevC++

5. Procedure and Output


EXERCISE No. 1
A young boy wants to create a program in block of code that will compute for the quadratic
equation then print the result. The main function composes of the input variables. The formula is
given below:
-B ± √ B ^2 – 4AC
--------------------------------
2A
If the user inputs zero (0), the program will display “Invalid Entry” and return to main menu to input
another valid number.

1. Write the corresponding algorithm


a. Narrative
b. Pseudocode
2. Create the equivalent flowchart based on the algorithm of the given problem
3. Construct the program using Global and Local Variables and record your screen display
result.
4. Create the equivalent program using the following structure.
a. Function Header
b. Function Prototype
c. Parameters and return

ALGORITHM

NARRATIVE

Step 1: Start the program


Step 2: Make three global variables: a, b, and c.
Step 3: Make two local variables for the function and two more local variables for the discriminant.
Step 4: To answer the quadratic equation, you'll need to build a function.
Step 5: Request that the user enters any three integers.
Step 6: Call the function that would compute the quadratic equation.
Step 7: Solve the quadratic equation.
Step 8: Display the result of the quadratic equation.
Step 9: End the program
PSEUDOCODE

Step 1: Start
Step 2: Enter any three integers and save into a, b and c
Step 3:
if (a == 0 || b == 0 || c == 0)
{
cout << "Invalid coefficient" << endl;
goto start;
}
Quadratic_Equation (a, b, c); // Call function
Step 4:
double discriminant ()
{
return (sqrt (b*b-4*a*c));
}
void Quadratic_Equation (double a, double b, double c) {
double root1, root2;
root1 = (-b + discriminant())/(2*a);
root2 = (-b - discriminant())/(2*a);
cout << endl << "First Root is: " << root1;
cout << endl << "\nSecond Root is: " << root2;
}
int main ()
{
start:
cout << "\nEnter cofficient a: ";
cin >> a;
cout << "\nEnter cofficient b: ";
cin >> b;
cout << "\nEnter cofficient c: ";
cin >> c;
return 0;
}
Step 5: End the program
FLOWCHART

Start

Enter any three integers


and save into a, b and c

True cout << "Invalid


a == 0 ||
b == 0 || coefficient" << endl;
c == 0 goto start;

False

double discriminant ()
{
return (sqrt (b*b-4*a*c));
}
void Quadratic_Equation (double a, double b, double c) {
double root1, root2;
root1 = (-b + discriminant())/(2*a);
root2 = (-b - discriminant())/(2*a);
cout << endl << "First Root is: " << root1;
cout << endl << "\nSecond Root is: " << root2;

End
Record your screen display:
QUESTIONS

1. If the first input is higher than the second input but lower than the third input, what will be the output results? Write on the
space provided the input values and the results.

Input: a = 4, b = 2, c = 6
Output: First root: -nan, Second root: -nan

2. If the third input is first than the second input but lower than the first input, what will be the output results? Write on the space
provided the input values and the results.

Input: a = 5, b = 2, c = 3
Output: First root: -nan, Second root: -nan

3. If the second input is higher than the first input but lower than the third input, what will be the output results? Write on the
space provided the input values and the results.

Input: a = 1, b = 9, c = 14
Output: First root: -2, Second root: -7

4. If the first input is higher than the second input and equal to the third input, what will be the output results? Write on the
space provided the input values and the results.

Input: a = 4, b = 2, c = 4
Output: First root: -nan, Second root: -nan

5. If the first input is lower than the second input and equal to the third input, what will be the output results? Write on the space
provided the input values and the results.

Input: a = 2, b = 8, c = 2
Output: First root: -0.267949, Second root: -3.73205

EXERCISE No. 2
Juan wanted to create a program that would use block of code (function) to group the student
grades. In each block, it consists of each prelim, midterm and final grade that will required
inputting a student name, course, student grade in quiz, seatwork, laboratory exercises,
assignment and exam. The formula below shows the following computation:

Prelim Grade = Quiz * 20% + Seatwork * 10% + Lab. Exercise * 20% + Prelim Exam * 50%
Midterm Grade = Quiz * 20% + Seatwork * 10% + Lab. Exercise * 20% + Midterm Exam * 50%
Total Midterm Grade = 1/3 Prelim Grade + 2/3 Midterm Grade
Final Grade = Quiz * 20% + Seatwork * 10% + Lab. Exercise * 20% + Final Exam * 50%
Total Final Grade = 1/3 Midterm Grade + 2/3 Final Grade
1. Write the corresponding algorithm
c. Narrative
d. Pseudocode
2. Create the equivalent flowchart based on the algorithm of the given problem
3. Construct the program using and record your screen display result.

ALGORITHM

NARRATIVE PSEUDOCODE
Step 1: Start the program Step 1: Start
Step 2: Make global variables for the prelim, midterm, and Step 2: Enter the student's name and course, then save the names

final. and courses as global variables.

Step 3: For each global variable, create local variables for Step 3: Call function prelim ();
cout << "Enter Prelim Quiz Grade: " << endl;
the quiz, seatwork, laboratory exercise, and exam.
cin >> tp_quiz;
Step 4: Make a global variable for the name of the student cout << "Enter Prelim Seatwork Grade: " << endl;
and the course. cin >> tp_seatwork;
Step 5: Inquire about the user's name, course, and prelim, cout << "Enter Prelim Lab. Exercise Grade: " << endl;

midterm, and final grades. cin >> tp_lab;


cout << "Enter Prelim Exam Grade: " << endl;
Step 6: Solve the prelim, midterm, and final grades
cin >> tp_exam;
Step 7: Print student name, course, prelim, midterm, and final p_quiz = tp_quiz * 0.2;
grades (quiz, seatwork, laboratory exercise, and exam) p_seatwork = tp_seatwork * 0.1;
Step 8: End the program p_lab = tp_lab * 0.2;
p_exam = tp_exam * 0.5;
p_grade = p_quiz + p_seatwork + p_lab + p_exam;
PSEUDOCODE PSEUDOCODE
Step 4: Call function midterm (); Step 7:
cout << "Enter Midterm Quiz Grade: " << endl; cout << "Prelim:" << endl;

cin >> tm_quiz; cout << " Quiz Grade: " << tp_quiz << " Point Grade: "<< p_quiz << endl;

cout << "Enter Midterm Seatwork Grade: " << endl; cout << " Seatwork Grade: " << tp_seatwork << " Point Grade: " << p_seatwork
<< endl;
cin >> tm_seatwork;
cout << " Lab. Exercise Grade: " << tp_lab << " Point Grade: " << p_lab <<
cout << "Enter Midterm Lab. Exercise Grade: " << endl;
endl;
cin >> tm_lab;
cout << " Exam Grade: " << tp_exam << " Point Grade: " << p_exam << endl;
cout << "Enter Midterm Exam Grade: " << endl;
cout << " Total Prelim Grade: " << p_grade << endl;
cin >> tm_exam;
m_quiz = tm_quiz * 0.2; cout << "Midterm:" << endl;
m_seatwork = tm_seatwork * 0.1; cout << " Quiz Grade: " << tm_quiz << " Point Grade: " << m_quiz << endl;
m_lab = tm_lab * 0.2; cout << " Seatwork Grade: " << tm_seatwork << " Point Grade: " <<
m_exam = tm_exam * 0.5; m_seatwork << endl;

m_total = m_quiz + m_seatwork + m_lab + m_exam; cout << " Lab. Exercise Grade: " << tm_lab << " Point Grade: " << m_lab <<
endl;
m_grade = (p_grade / 3) + ((2 * m_total) / 3);
cout << " Exam Grade: " << tm_exam << " Point Grade: " << m_exam << endl;
cout << " Total Midterm Grade: " << m_grade << endl;
Step 5: Call function Final ();
cout << "Enter Final Quiz Grade: " << endl;
cout << "Final:" << endl;
cin >> tf_quiz; cout << " Quiz Grade: " << tf_quiz << " Point Grade: " << f_quiz << endl;
cout << "Enter Final Seatwork Grade: " << endl; cout << " Seatwork Grade: " << tf_seatwork << " Point Grade: " << f_seatwork
cin >> tf_seatwork; << endl;
cout << "Enter Final Lab. Exercise Grade: " << endl; cout << " Lab. Exercise Grade: " << tf_lab << " Point Grade: " << f_lab << endl;

cin >> tf_lab; cout << " Exam Grade: " << tf_exam << " Point Grade: " << f_exam << endl;

cout << "Enter the Final Exam Grade: " << endl; cout << " Total Final Grade: " << f_grade << endl;

cin >> tf_exam;


Step 8: Call the Function Remarks ();
f_quiz = tf_quiz * 0.2; if (f_grade < 75)
f_seatwork = tf_seatwork * 0.1; cout<<"Failed!";
f_lab = tf_lab * 0.2;
else
cout<<"Passed!";
f_exam = tf_exam * 0.5;
f_total = f_quiz + f_seatwork + f_lab + f_exam; Step 9:
return 0;
f_grade = (m_grade / 3) + ((2 * f_total) / 3);
}

Step 6: Step 10: End


cout << "Name: " << name;
cout << endl;
cout << "Course: " << course;
cout << endl;
m_total = m_quiz + m_seatwork m_grade = (p_grade / 3) + ((2 *
+ m_lab + m_exam m_total) / 3)

Call function final ()

m_quiz = tm_quiz * 0.2


m_seatwork = tm_seatwork * 0.1 Enter final quiz grade
m_lab = tm_lab * 0.2 and save into tf_quiz.
m_exam = tm_exam * 0.5 Enter final seatwork
grade and save into
tf_seatwork.
Enter final laboratory
exercise grade and
Enter midterm quiz grade save into tf_lab.
and save into tm_quiz. Enter final exam
Enter midterm seatwork grade and save into
grade and save into tf_exam.
tm_seatwork.
Enter midterm laboratory
exercise grade and save
into tm_lab.
Enter midterm exam
grade and save into f_quiz = tf_quiz * 0.2
tm_exam. f_seatwork = tf_seatwork * 0.1
f_lab = tf_lab * 0.2
f_exam = tf_exam * 0.5;
Call function midterm ()

p_grade = p_quiz + p_seatwork End


+ p_lab + p_exam

f_total = f_quiz + f_seatwork +


f_lab + f_exam
p_quiz = tp_quiz * 0.2
p_seatwork = tp_seatwork * 0.1
p_lab = tp_lab * 0.2
p_exam = tp_exam * 0.5
f_grade = (m_grade / 3) + ((2 *
f_total) / 3)
Passed
Enter prelim quiz grade
and save into tp_quiz.
Enter prelim seatwork
grade and save into Print name
tp_seatwork. Print course
Enter prelim laboratory
exercise grade and save Else
Print tp_quiz, p_quiz
into tp_lab. Print tp_seatwork, p_seatwork
Enter prelim exam grade Print tp_lab, p_lab
and save into tp_exam. Print tp_exam, p_exam
Print p_grade False
Call function prelim ()
Print tm_quiz, m_quiz
Enter the Print tm_seatwork, f_grade
student's name m_seatwork < 75
and course, then Print tm_lab, m_lab
save the names Print tm_exam, m_exam
and courses Print m_grade
True
Print tf_quiz, f_quiz
Call function Print tf_seatwork, f_seatwork
stud_info () Print tf_lab, f_lab Failed
Start Print tf_exam, f_exam
Print f_grade
Record your screen display:
QUESTIONS

1. What is/are the variable/s needed to produce local variable/s?


- The user input that would be supplied to the variables in the function that was called in the main function would be the
variables required to construct the local variables. Student name, course and grades from quiz, seatwork, laboratory exercises,
exam grades, and total grades (Prelim, Midterm and Final) are the variables needed to execute the program.

2. Using Parameter and return structure, what inputs stored in output?


- The use of a parameter and return structure would make using the program to conduct constant computations much easier.
Use a return value when you only need to return one, and output parameters when you need to return more than one. The data
saved in output are the initial data that the user entered.

3. What is/are the variable/s needed to produce global variable/s?


- The global variables are created outside of any functions where they are not constrained to becoming local variables unless
the third way of calling functions is utilized to allow local variables to be used across many functions.

4. What did you observe in function header and prototypes?


- The prototypes are simply how one function notifies another function that they have to collaborate on anything or perform
something within the code of the header function. The function header is the primary function in which variables and other
functions work around it.

5. What is the important of return in function?


- In functions, the return statement is vital because it allows them to send any value calculated in their local variables to other
functions that may require it. Your function exit and returns a value to its caller when you use the return statement. In general,
the purpose of functions is to accept inputs and return something. The return statement is also utilized when a function is ready
to return a value to its caller.
SUPPLEMENTAL ACTIVITIES

1. Write a parameter and return program that will require the user to enter a temperature in Celsius and convert it to degree
Fahrenheit and degree Kelvin.

Formula:
Fahrenheit = (9/5) * Celsius + 32
Kelvin = Celsius + 273

Write your answer:

// SUPPLEMENTAL ACTIVITIES BY JOHN KENNETH GULIS

#include <iostream>
using namespace std;

float Celsius_to_Fahrenheit(float Celsius)

return (1.8 * Celsius) + 32;

float Celsius_to_Kelvin(float Celsius)

return (Celsius + 273);

int main()

float Celsius;
cout << "Input your preferred value for Celsius: " ; cin >> Celsius;
cout << endl<< "Temperature in Fahrenheit = " << Celsius_to_Fahrenheit(Celsius) << endl;
cout << endl<< "Temperature in Kelvin = " << Celsius_to_Kelvin(Celsius) << endl;
return 0;

}
2. Refer to No. 1, transform the program using function header and function prototype.

Write your answer:

// SUPPLEMENTAL ACTIVITIES BY JOHN KENNETH GULIS

#include <iostream>
#include <string>
using namespace std;

float Celsius, Fahrenheit, Kelvin;


void Conversion ();
int main ()
{
Conversion ();
return 0;
}
void Conversion ()
{
float Celsius, Fahrenheit, Kelvin;

cout<<"Enter the temperature in Celsius: ";

cin >> Celsius;

Fahrenheit = (1.8*Celsius) + 32;

cout << endl << "Temperature in Fahrenheit: " << Fahrenheit;

Kelvin = (Celsius + 273);

cout << endl << endl << "Temperature in Kelvin: " << Kelvin;
}
3. Write a global and local variable program that would require the user to input a value of the Radius of a circle and then show
the output in the diameter, circumference of the circle and area of the circle.

Write your answer:

// SUPPLEMENTAL ACTIVITIES BY JOHN KENNETH GULIS

#include <iostream>
using namespace std;

float d, C, A;
float pie ()

{
return (3.1416);
}

void Solution (float r)


{
d = 2 * r;
C = 2 * pie () * r;
A = pie () * (r * r);

cout << "Radius: " << r << endl;


cout << "Diameter: " << d << endl;
cout << "Circumference: " << C << endl;
cout << "Area: " << A << endl;

int main ()
{
float r;

cout << "Input the value of radius: " ; cin >> r;

Solution (r);

return 0;
}

6. Conclusion:

Functions change the state of things, perform activities, and/or return a value. Functions, I've discovered, are best
employed to run a section of code that would otherwise be repeated and to divide the program into well-organized
sections. Finally, employing functions may either make coding easier or more difficult than it has to be. It all relies on how
well you understand how each function works and how you can utilize that knowledge to make the programs simpler or
more complicated as needed.

You might also like