Gulis Laboratory 6 PDF
Gulis Laboratory 6 PDF
Gulis Laboratory 6 PDF
: 6
Name: John Kenneth S. Gulis Section: EE21S1
Date Performed: November 19, 2021
Date Submitted: November 19, 2021
Instructor: Engr. Julie Ann Susa
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.
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++
ALGORITHM
NARRATIVE
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
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
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;
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;
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
#include <iostream>
using namespace std;
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.
#include <iostream>
#include <string>
using namespace std;
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.
#include <iostream>
using namespace std;
float d, C, A;
float pie ()
{
return (3.1416);
}
int main ()
{
float 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.