0% found this document useful (0 votes)
22 views3 pages

C++ Functions Practice

The document outlines two programming assignments for CMP 120, focusing on user-defined functions in C++. The first problem requires writing a function to determine the maximum of three integers, while the second problem involves calculating a series to compute a value Z based on user input. Both assignments emphasize user interaction for input and output, and require testing in a programming environment.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views3 pages

C++ Functions Practice

The document outlines two programming assignments for CMP 120, focusing on user-defined functions in C++. The first problem requires writing a function to determine the maximum of three integers, while the second problem involves calculating a series to compute a value Z based on user input. Both assignments emphasize user interaction for input and output, and require testing in a programming environment.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

CMP 120 Assignment 5

Due : Oct. 24, 2022


Problem 1:
Write and test a user-defined function with prototype
int max3(int x, int y, int z);
The function max3 computes and returns the largest of x, y and z. Write the main()
program to test the working of your function with some sample values. All user inputs
and printouts should be done in main().

Sample session: (user input in Red color)


Enter the first integer: 2
Enter the second integer: 12
Enter the third integer: 10
Largest = 12

Code:
#include<iostream>
using namespace std;
int max3(int x, int y, int z);
int main()
{
int x,y,z,a;
cout<<"Enter the first integer: ";
cin>>x;
cout<<"Enter the second integer: ";
cin>>y;
cout<<"Enter the third integer: ";
cin>>z;
a= max3(x,y,z);
cout<<"Largest = "<<a<<endl;

int max3(int x, int y, int z)


{
int a=0;
if ((x>y)&&(x>z))
{
a=x;
}
else if ((y>x)&&(y>z))
{
a=y;
}
else if ((z>y)&&(z>x))
{
a=z;
}
return a;
}

Output:

Problem 2:

Write a program that first asks the user to input a positive integer N. It then computes
and prints the double number Z using the following series :

Z = 1 - (1/2!) + (1/3!) - (1/4!) + …. +- (1/N!)

Your main() program should use a looping structure, and should call a user-defined
function int factorial (int x); to return the factorial of a positive integer number x to
be used in the computation of Z. Also note that all divisions in Z are real divisions.
All user inputs and printouts should be done in main().

Sample session: (user input in Red color)


Enter the value for N: 6
Z = 0.631944

Code:
#include <iostream>
using namespace std;
int factorial(int x)
{
int res = 1;
for (int i=2; i<=x; i++)
res *= i;
return res;
}
double sum(int x)
{
double sum = 0;
for (int i = 1; i <= x; i++)
if(i%2==0)
{
sum += -1.0/factorial(i);
}
else
{
sum += 1.0/factorial(i);
}
return sum;
}
int main()
{
int n;
cout<<"Enter the value for N: ";
cin>>n;
cout <<"Z = "<< sum(n)<<endl;
return 0;
}
Output:

Write and test your programs under Visual Studio or Online


Compiler, and submit your programs and sample outputs on the link
provided for this assignment.

You might also like