2 Answer LabExercise Function
2 Answer LabExercise Function
Lab Exercise
User-defined Functions Structure
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
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.
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
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;
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;
return average;
}
void displayResult(float average)
{
if(average>80)
cout<<"Excellent";
else if(average>50)
cout<<"Good";
else
cout<<"Failed";
}
int main()
{
float average;
return 0;
}
Question 4 : January 2024
Answer - Question 4
a) float rectArea(float length, float height)
{
float areaRect;
return areaRect;
}
return areaTri;
}
Answer - Question 4
c) int main()
{
float height, length, base, areaRect, areaTri;
areaRect = rectArea(length,height);
areaTri = triArea(height,base);
return 0;
}
Question 5 : July 2024
Answer - Question 5
bmi = weight/pow(height,2);
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;
bmi = calculateBMI(weight,height);
WeightStatus(bmi);
return 0;
}
Question 6 : July 2024
Answer - Question 6
if (activity== 'A’)
cout<<'Water Park"<<endl;
else
cout<<"Night Safari"<<endl;
cout<<"Price: RM"<<price<<endl;
}
//Function calls
float price = calcPrice (activity, age);
//Function calls
price = calcPrice (activity, age);
displayDetailsPayment (name, age, activity, price);
return 0;
}
Question 7 : Feb 2025
Answer: Question 7 (Feb 2025)
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;
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;
}