Function Lab Exercise 1 1. Predefined Functions
Function Lab Exercise 1 1. Predefined Functions
LAB EXERCISE 1
1. Predefined Functions
a) <cstring> The following program accepts two full names; checks whether both names are equal, edit
the names, get the length of the first name and concatenate two names together.
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char firsName[30], secName[30];
if (strcmp(firstName, secName) == 0)
cout << “The names are the same” << endl;
else
cout << “The names are different” << endl;
strcpy(firstName, “Marissa”);
strcat(firstName, “Nurdia”);
b) <cctype>The following program accepts a character, checks whether the character is an uppercase or
a lowercase. If it is an uppercase, the lowercase of the character is then displayed and vice versa.
#include <iostream>
#include <cctype>
if (isupper(letter))
{
cout << "The character is an uppercase" << endl;
cout << "The lowercase is "<< (char) tolower(letter) << endl;
}
else
{
cout << "The character is a lowercase" << endl;
cout << "The uppercase is "<< (char)toupper(letter) << endl;
}
return 0;
}
C) <iomanip>The following program displays the integer, string, and floating point values according to the
predetermined output setting.
#include <iostream>
#include <iomanip>
int main()
{
int x = 2345;
cout << "|" << setw(8) <<left<< x << "|" << endl;
cout << "|" << setw(8)<< "HELLO" << "|" << endl;
cout << "|" << setfill('*') << setw(6)<< "HELLO" << "|" << endl;
cout << "|" << setw(6) <<right<< x << "|" << endl;
cout << "|" << setw(8) <<fixed<<showpoint<< setprecision(2) << 86.2 << "|" << endl;
cout << "|"<< 5 << "|" << endl;
return 0;
}
int main()
{
cout << “Welcome!”;
sum(); //function call [empty parameter]
cout << “\nEnd of program”;
return 0;
}
void sum(int, int); //function prototype [with parameter data type – int]
int main()
{
int num1, num2;
cout << “Welcome!”;
cout << “Enter two numbers (separate by space): ”;
cin >> num1 >> num2;
sum(num1, num2); //function call [with parameter value from num1 and num2]
cout << “\nEnd of program”;
return 0;
}
void sum(int n1, int n2) //function definition [parameter received value from function call]
{
int total;
total = n1 + n2;
cout << “\nThe sum is : ” << total << endl;
}
c) FLOAT returning function WITHOUT parameter
#include <iostream>
using namespace std;
return area; //return value from variable area to main function – cout statement
}
float displayAvg3Num(int, int, int); //function prototype [with parameter data type – int]
int main()
{
int no1, no2, no3;
float avg;
return 0;
}
float displayAvg3Num(int n1, int n2, int n3)//function definition [parameter received from main]
{
float average;
average = (n1 + n2 + n3) / 3;
return average; //return value from variable average to main function – avg
}
Exercise 1
The following program calculates and displays the wages of an employee based on the number of hours
worked and the hourly rate of pay.
//To display greeting and calculate wages without using function
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int hoursWorked;
float hourlyRate;
float wage;
char name[25];
return 0;
}
QUESTION :
Modified the above program by applying functions to it. Split and separate the program into two
functions: calWages(int,int) and greeting().
Exercise 2
Referring to your previous repetition exercise (Jumanja National Park Total Payments Program),
modified the program into following functions:-