To get credit for the lab, submit the source file, 5 algorithms and a screen shot of the running program.
C++ PROGRAM
#include<iostream>
using namespace std;
// implement larger() function with two integer parameters
// return integer value
int larger(int x,int y)
{
if(x>y) // check x>y then
return x; // return x value
else
return y; // else, return y
value
}
// implement larger3() function with three integer
parameters
// return integer value
int larger3(int x,int y,int z)
{
int big=larger(x,y); // calling larger() function and
receive biggest value in 'big'
if(big>z) // check big>z then,
return big; // return big
value
else
return z; // else, return z
value
}
// implement larger4() function with four integer
parameters
// return integer value
int larger4(int x,int y,int z, int n)
{
int big1=larger(x,y); // calling larger() function and
receive biggest value in 'big1'
int big2=larger(z,n); // calling larger() function and
receive biggest value in 'big2'
if(big1>big2) // check big1>big2 then,
return big1; // return big1
value
else
return big2; // return big2
value
}
// implement largest() function with two parameters
// first parameter is integer array and second paramter integer
argument
// return integer value
int largest(int a[],int size)
{
int big=larger(a[0],a[1]); // calling larger()
function and receive biggest value in 'big'
for(int i=0;i<size;i++) // create for loop until
size
{
if(a[i]>big) // check array
element a>big
big=a[i]; // then, assign biggest
value into 'big'
}
return big; // finally return value
}
int main()
{
int x,y,z,n; // declare 4 integer variable
int a[100],size; // declare integer array a and
integer variable size
cout<<"Enter Positive integer x,y,z and n
values: ";
cin>>x>>y>>z>>n; // Read 4
integer variables
cout<<"Enter Array Size: ";
cin>>size; // Read size of an array
cout<<"\nEnter Positive "<<size<<"
Elements in Integer array :";
// input array elements
for(int i=0;i<size;i++) // create for loop until
size of an array
cin>>a[i]; // read array
elements
// Display original values
cout<<"\n\nX = "<<x<<", Y =
"<<y<<", Z = "<<z<<", N =
"<<n<<endl;
cout<<"Original Array Elements\n";
for(int i=0;i<size;i++)
cout<<a[i]<<" ";
int b1=larger(x,y); // callng larger() function and
receive biggest number into b1
int b2=larger3(x,y,z); // calling larger3() function
and receive biggest number into b2
int b3=larger4(x,y,z,n); // calling larger4() function
and receive biggest number into b3
int b4=largest(a,size); // calling largest() function
and recieve biggest number into b4
// dipslay all biggest values
cout<<"\n\nLarger Value:
"<<b1<<endl;
cout<<"Larger 3 Value:
"<<b2<<endl;
cout<<"Larger 4 Value:
"<<b3<<endl;
cout<<"Array Largest Value:
"<<b4<<endl;
return 0;
}
OUTPUT
Enter Positive integer x,y,z and n values: 25 689 5 66
Enter Array Size: 5
Enter Positive 5 Elements in Integer array :58 11 489 6 78
X = 25, Y = 689, Z = 5, N = 66
Original Array Elements
58 11 489 6 78
Larger Value: 689
Larger 3 Value: 689
Larger 4 Value: 689
Array Largest Value: 489
ALGORITHMS
Algorithm for larger() function with two integer arguments are x and y
Step 1: Start
Step 2: Read two Integer values x and y
Step 3: if x>y
Display x is the Biggest Number
Else
Display y is the Biggest Number
Step 4: Stop
Algorithm for larger3() function with three integer arugments are x,y and z
Step 1: Start
Step 2: Read three Integer values x,y and z
Step 3: calling larger() function and store into integer variable
big i.e.,
big=larger(x,y)
Step 4: if big>z
Display big is the Biggest
number
Else
Display z is the Biggest
number
Step 5: Stop
Algorithm for larger4() function with four Integer arguments area x,y,z and n
Step 1: Start
Step 2: Read 4 Integer values x,y,z and n
Step 3: calling larger() function and store into Integer variables
big1 and big2
i.e.,
big1=larger(x,y)
big2=larger(z,n)
Step 4: if big1 > big2
Display big1 is the Biggest
number
Else
Display big2 is the Biggest
number
Step 5: Stop
Algorithm for largest() function with two
arguments
First argument is Integer Array a[] and Second Argument is integer
variable size
Step 1: Start
Step 2: Read Size of and Array size
Step 3: Read Integer Array Elements a
Step 4: calling larger() function like
big=larger(a[0],a[1]) first and second elements in the
array
Step 5: Set a for loop
Step 6: if a[i]>big then,
big=a[i]
Step 7: Display Biggest number is big
Step 8: Stop
Algorithm for main() function
Step 1: Start
Step 2: Read 4 Integer variables x,y,z,n and size and Integer Array
a
Step 3: Read Array size
Step 4: Set a for loop
Step 5: Read Array elements a
Step 6: calling larger() function and receive return value into
b1
b1=larger(x,y)
Step 7: calling larger3() function and receive return value into
b2
b2=larger3(x,y,z)
Step 8: calling larger4() function and receive return value into
b3
b3=larger4(x,y,z,n)
Step 9: calling largest() function and receive return value into
b4
b4=largest(a,size)
Step 10: Display b1,b2,b3,b4
Step 11: Stop
Get Answers For Free
Most questions answered within 1 hours.