Lab 9 Example 1 #include<iostream> #include<cmath> using namespace std; int main() { cout << pow(3.0, 2.0); } Example 2 #include<iostream> #include<cmath> using namespace std; int main() { double x = 3.0, y = 2.0; cout << pow(x, y); } Example 3 #include<iostream> #include<cmath> using namespace std; int main() { double result, x = 3.0, y = 2.0; result = pow(x, y); cout << result; } Program 1 Write a C++ program that includes a function declaration, a function call and a function definition for a function that takes two arguments, all of type int, and that returns the a char value. Note the name of the function is “great”. The purpose of the function is to print ‘W’ if the first number is bigger or equal and print ‘H’ if the second number is bigger. Solution to Program 1 #include <iostream> using namespace std; char great(int first_par, int second_par); int main( ) { cout << great(10, 9); return 0; } char great(int first_par, int second_par) { if (first_par >= second_par) return 'W'; else return 'H'; } Program 2 Write a C++ program that includes a function declaration, a function call and a function definition for a function that takes three arguments, all of type int, and that returns the sum of its three arguments. Note the name of the function is “sum”. Solution to Program 2 #include<iostream> using namespace std; int sum(int n1, int n2, int n3); int main() { cout<<"The sum is "<<sum(1,2,3); } int sum(int n1, int n2, int n3) { return (n1 + n2 + n3); } Program 3 Write C++ program that includes a function declaration, a function call and a function definition for a function that takes two arguments of type double, and that returns a value of type double that is the average of the two arguments. Note the name of the function is “ave”. Solution to Program 3 #include<iostream> using namespace std; double ave(double n1, double n2); int main() { cout<<"The average is "<<ave(1.0,2.0); } double ave(double n1, double n2) { return ((n1 + n2)/2.0); } Program 4 Write a C++ program that includes a function declaration, a function call and a function definition for a function that takes one argument of type double. The function returns the character value 'P' if its argument is positive and returns 'N' if its argument is zero or negative. Solution to Program 4 #include<iostream> using namespace std; char positive_test(double number); int main() { cout<<positive_test(1.0); } char positive_test(double number) { if (number > 0) return 'P'; else return 'N'; } Program 5 Write a C++ program that includes a function declaration, a function call and a function definition for a function called “even” that takes one argument of type int and returns a bool value. The function returns true if its one argument is an even number; otherwise, it returns false. Solution to Program 5 #include<iostream> using namespace std; bool even(int n); int main() { cout<<even(4); } bool even(int n) { return ((n % 2) == 0); } Program 6 Write a C++ program that will print the outputs of the following arithmetic expressions? Program 7 Write a C++ program that converts the following expressions to a C++ arithmetic expression Program 8 Write a C++ program to compute and output the square root of PI; PI is approximately 3.14159. The const double PI is predefined in cmath. You are encouraged to use this predefined constant. Program 9 Write a C++ program that includes a function declaration, a function and a function definition for a function called “in_order” that takes three arguments of type int. The function returns true if the three arguments are in ascending order; otherwise, it returns false. For example, in_order(1, 2, 3) and in_order(1, 2, 2) both return true, while in_order(1, 3, 2) returns false. Program 10 Write a C++ program that includes a function declaration, a function call and a function definition for a function “is_root_of” that takes two argument of type int and returns a bool value. The function returns true if the first argument is the square root of the second; otherwise, it returns false.
© Copyright 2025