Please, I have one question with two problems that I did\'t know how to write th
ID: 3625421 • Letter: P
Question
Please, I have one question with two problems that I did't know how to write the code for them.Hope if you can help me.
The question is:
Q1: Write the function max ( ) that reads n positive integers from the input and computes and returns the maximum number among the input ones. Write a program to test max ( ).
Using an approach similar to that of Exercise 1, write and test a function that finds the two largest values among n input positive numbers and returns to the calling function these two numbers. [Note: You must input each number only once].
Thanks.
:)
Explanation / Answer
please rate - thanks
#include <iostream>
using namespace std;
int max();
void max2(int&,int&);
int main()
{int big,big2;
cout<<"The biggest number is "<<max()<<endl;
max2(big,big2);
cout<<"The 2 biggest numbers are: "<<big<<" and "<<big2<<endl;
system("pause");
return 0;
}
int max()
{int n,big,m,i;
cout<<"How many numbers do you have? ";
cin>>n;
cout<<"Enter number 1: ";
cin>>big;
for(i=2;i<=n;i++)
{cout<<"Enter number "<<i<<": ";
cin>>m;
if(m>big)
big=m;
}
return big;
}
void max2(int &b1,int &b2)
{int n,i,m;
b1=-999;
b2=-999;
cout<<"How many numbers do you have? ";
cin>>n;
for(i=1;i<=n;i++)
{cout<<"Enter number: "<<i<<": ";
cin>>m;
if(m>b1)
{b2=b1;
b1=m;
}
else if(m>b2)
b2=m;
}
}