0% found this document useful (0 votes)
2 views36 pages

2 Answer LabExercise Function

The document provides a comprehensive overview of user-defined functions in C++, including their structure, definitions, prototypes, and calls. It contains multiple programming exercises with sample code for functions that calculate IQ levels, areas of shapes, BMI, and pricing based on user input. Each section includes function definitions and main programs demonstrating how to utilize these functions effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views36 pages

2 Answer LabExercise Function

The document provides a comprehensive overview of user-defined functions in C++, including their structure, definitions, prototypes, and calls. It contains multiple programming exercises with sample code for functions that calculate IQ levels, areas of shapes, BMI, and pricing based on user input. Each section includes function definitions and main programs demonstrating how to utilize these functions effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Chapter 2 - Function

Lab Exercise
User-defined Functions Structure

Five important terms about user-defined functions:


1) Function Definition
a) Function Name
b) Formal Parameter Lists
c) Return value
2) Function Prototype/Declaration
3) Function Call
Question 1: January 2018

ii. Write a main program that will input the IQ scores and call the above function.
Answer Question 1: January 2018
#include <iostream>
using namespace std;
void FindIQLevel(int); //FUNCTION DECLARE

void FindIQLevel(int score) //FUNCTION DEFINITION


{
if(score<20)
cout<<"Slow";
else if(score>=20 && score<=140)
cout<<"Normal";
else if(score>=141 && score<=210)
cout<<"Intelligent";
else
cout<<"Genius";
}
int main()
{
int score;

cout<<"Enter IQ Scores: ";


cin>>score;

FindIQLevel(score); //function call

return 0;
}
Question 2

Write the function definition for each of the following C++ functions:
a) Function GetInput( ) that input and return the radius of a circle.

b) Function AreaCircle( ) that receives the radius of a circle. It will


calculate and return the area of circle using the formula given
below:
Area = πr2

a) Write a main program that will call both functions and display the
area of circle in the main function.
Answer Question 2
#include <iostream>
using namespace std;
#include <math.h>
float GetInput( ); // function prototype
double AreaCircle(float); // function prototype

float GetInput( ) // function definition


{
float radius;

cout<<"Enter the radius of a circle: ";


cin>>radius;

return radius;
}
double AreaCircle(float radius) // function definition
{
double area;
const double PAI=3.142;

area = PAI*pow(radius,2);

return area;
}
int main()
{
float radius,area;

radius = GetInput(); // function call


area = AreaCircle(radius); // function call

cout<<"Area of Circle = "<<area<<endl;

return 0;
}
Question 3: Feb 2023
Answer Question 3: Feb 2023
#include <iostream>
using namespace std;
float getMarks(); //function prototype
void displayResult(float); //function prototype

float getMarks()
{
int marks;
float total,average;

for(int i=1; i<=5; i++)


{
cout<<"Enter marks: ";
cin>>marks;

total = total + marks;


}
average = total/5;

return average;
}
void displayResult(float average)
{
if(average>80)
cout<<"Excellent";
else if(average>50)
cout<<"Good";
else
cout<<"Failed";
}
int main()
{
float average;

average = getMarks(); //function call


cout<<“\nThe average is "<<average;
displayResult(average);//function call

return 0;
}
Question 4 : January 2024
Answer - Question 4
a) float rectArea(float length, float height)
{
float areaRect;

areaRect = length * height;

return areaRect;
}

b) float triArea(float height, float base)


{
float areaTri;

areaTri = 0.5 * height * base;

return areaTri;
}
Answer - Question 4
c) int main()
{
float height, length, base, areaRect, areaTri;

cout<<"Enter height: ";


cin>>height;
cout<<"Enter length: ";
cin>>length;
cout<<"Enter base: ";
cin>>base;

areaRect = rectArea(length,height);
areaTri = triArea(height,base);

cout<<"Area of Rectangle : "<<areaRect<<endl;


cout<<"Area of Triangle : "<<areaTri<<endl;

return 0;
}
Question 5 : July 2024
Answer - Question 5

i. float calculateBMI(float weight, float height)


{
float bmi;

bmi = weight/pow(height,2);

cout<<"WEIGHT (KG): "<<weight<<endl;


cout<<"HEIGHT (M): "<<height<<endl;
cout<<"BMI VALUE: "<<bmi<<endl;

return bmi;
}
Question 5 : July 2024 (cont…)
Answer - Question 5
ii. void WeightStatus(float bmi)
{
if(bmi<18.5)
cout<<"Underweight"<<endl;
else if(bmi>=18.5 && bmi<=24.9)
cout<<"Normal"<<endl;
else if(bmi>=25 && bmi<=29.9)
cout<<"Overweight"<<endl;
else
cout<<"Obese"<<endl;
}
Answer - Question 5
iii. int main ()
{
float weight, height, bmi;

cout<<"Insert weight (kg): “;


cin>>weight;
cout<<"Insert height (metre): “;
cin>>height;

bmi = calculateBMI(weight,height);
WeightStatus(bmi);

return 0;
}
Question 6 : July 2024
Answer - Question 6

double calcPrice(char activity, int age)


{
double price;
if (activity == 'A’)
{
if (age< 12)
price= 10;
else if (age < 50)
price= 20;
else
price = 15;
}
else
{
if (age< 12)
price= 20;
else if (age < 50)
price= 30;
else
price = 20;
}
return price;
}
Answer - Question 6
Answer - Question 6
void displayDetailsPayment (string name, int age, char activity, double price)
{
cout<<"\n\n-------Details Payment-------"<<endl;
cout<<"Name: "<<name<<endl;
cout<<"Age: "<<age<<endl;
cout<<"Activity: ";

if (activity== 'A’)
cout<<'Water Park"<<endl;
else
cout<<"Night Safari"<<endl;

cout<<"Price: RM"<<price<<endl;
}

//Function calls
float price = calcPrice (activity, age);

displayDetailsPayment (name, age, activity, price);


Answer - Question 6
int main()
{
string name;
int age;
char activity;
double price;

cout<<"Enter your name: ";


getline(cin,name);
cout<<"Enter your age: ";
cin>>age;
cout<<"Choose your activities [A-WaterPark or B-Night Safari]: ";
cin>>activity;

//Function calls
price = calcPrice (activity, age);
displayDetailsPayment (name, age, activity, price);
return 0;
}
Question 7 : Feb 2025
Answer: Question 7 (Feb 2025)

int quadratic(int a, int b, int c)


{
int y;
int x = 2;

y = (a*x*x)+(b*x)+c;

return y;
}
Answer: Question 7 (Feb 2025)

void quadraticShape(int a)
{
if (a>0)
cout <<“\nGraph Opens Upward";
else
cout << “\nGraph Opens Downward";
}
Answer: Question 7 (Feb 2025)

int main()
{
int y, a, b, c;

cout << "Enter the value of a: " ;


cin >> a;
cout << "Enter the value of b: " ;
cin >> b;
cout << "Enter the value of c: " ;
cin >> c;

y = quadratic (a,b,c);
cout << "\nThe value of y is " <<y;
quadraticShape (a);

return 0;
}
Answer: Question 8 (Feb 2025)
Answer: Question 8 (Feb 2025)
double calcTotalPrice(string bookCategory, int quantity)
{
double total= 0;

if (bookCategory = "Novel")
total= 35 *quantity;
else if (bookCategory == "Comic")
total= 20 *quantity;
else if (bookCategory == "Magazine")
total= 15 *quantity;
else if (bookCategory == "Encyclopedia")
total = 50 *quantity;
else if (bookCategory == "Children")
total= 25 *quantity;
return total;
}

void displayOrderDetails(string name, string bookCategory, int quantity, double total)


{
cout<<" \n\n--------Order Details--------" << endl;
cout<<"Name: " <<name << endl;
cout<<"Category: "<< bookCategory << endl;
cout<<"Quantity: " <<quantity << endl;
cout<<"Total Price: RM" << total << endl;
}
Question 9
Question 9 (continue)
Question 10
Question 10

You might also like