Sie sind auf Seite 1von 4

FUNCTION

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];

cout << “Enter full name: ” << endl;


cin.getline(firstName, 30);
cout << “Enter another full name: ” << endl;
cin.getline(secName, 30);

if (strcmp(firstName, secName) == 0)
cout << “The names are the same” << endl;
else
cout << “The names are different” << endl;

strcpy(firstName, “Marissa”);

cout << “First name is now ” << firstName << endl;


cout << “The length of the first name is ” << strlen(firstName) << endl;

strcat(firstName, “Nurdia”);

cout << “First name is now ” << firstName << endl;


return 0;
}

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>

using namespace std;


int main()
{
char letter;
cout << "Enter a character: ";
cin >> letter;

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>

using namespace std;

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;
}

2. User Defined Functions


a) VOID returning function WITHOUT parameter
#include <iostream>
using namespace std;

void sum(); //function prototype declaration [empty parameter]

int main()
{
cout << “Welcome!”;
sum(); //function call [empty parameter]
cout << “\nEnd of program”;
return 0;
}

void sum() //function definition [empty parameter]


{
int num1, num2, total;
cout << “Enter two numbers (separate by space): ”;
cin >> num1 >> num2;
total = num1 + num2;
cout << “\nThe sum is : ” << total << endl;
}

b) VOID returning function WITH parameter


#include <iostream>
using namespace std;

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;

float calcAreaCircle(); //function prototype [empty parameter]


int main()
{
cout << “\nThe area of the circle is ” << calcAreaCircle();//function call [empty parameter value]
return 0;
}

float calcAreaCircle() //function definition [empty parameter]


{
float radius, area;

cout << “\nEnter radius of circle: ”;


cin >> radius;
area = 3.142 * radius * radius;

return area; //return value from variable area to main function – cout statement
}

d) value returning function WITH parameter


#include <iostream>

using namespace std;

float displayAvg3Num(int, int, int); //function prototype [with parameter data type – int]
int main()
{
int no1, no2, no3;
float avg;

cout << “Enter three numbers”;


cin >> no1 >> no2 >> no3;
avg = displayAvg3Num(no1, no2, no3); //function call [with parameter value no1, no2 and no3]
cout << “\nThe average is: ” << 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];

cout << "Please enter name :";


cin.getline(name, 25);
cout << "\nHello " << name << ". Welcome to this program." << endl;
cout << "\nEnter the number of hours worked :";
cin >> hoursWorked;
cout << "\nEnter the hourly pay rate : RM";
cin >> hourlyRate;
wage = hoursWorked * hourlyRate;
cout<<setprecision(2)<<fixed;
cout << "\nThe wage is :RM" << wage << endl;

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:-

 Function calculatePayment(int,int) receives the information of trail and porter as its


parameters. This function is required to calculate payment that is being charged to the visitor and
return the payment to where it is invoked. Payment is calculated based on the following selections:

 Function displayOutput(char,int,int) receives the information of visitor_name, trail, porter and


payment as its parameters. This function will display the information of visitor as shown in the
sample output.

Das könnte Ihnen auch gefallen