The purpose of this problem is to write some small functions and practice passing things around amoung functions.
1) The main function shall ask the user to enter three numbers and read the three numbers.
2) Write a function named findSum that takes three numbers as arguments and returns their sum. The main function shall call this function.
3) Write a function named findAverage that takes the sum and the number of numbers and returns the average. The main function shall call this function.
4) Write a function named findSmallest that takes the three numbers and returns the smallest value. The main function shall call this function.
5) The main function shall print the results in the following format, with two decimal positions and the decimal points aligned:
Results:
First number 17.23
Second number 3.98
Third number 22.32
Total 43.53
Average 14.51
Smallest 3.98
Test the program twice with the following two sets of numbers:
37.144 2.4144 19
4.23 5.78 6.21
#include <iostream>
#include <string>
using namespace std;
float findSum(float number1,float number2,float number3){
return number1+number2+number3;
}
float findAverage(float sum,float n){
return sum/n;
}
float findSmallest(float number1,float number2,float number3){
if (number1 < number2 && number1 < number3)
{
return number1;
}
if (number2 < number1 && number2 < number3)
{
return number2;
}
return number3;
}
int main() {
float number1;
float number2;
float number3;
cout<<"Enter number 1: ";
cin>>number1;
cout<<"Enter number 2: ";
cin>>number2;
cout<<"Enter number 3: ";
cin>>number3;
float total=findSum(number1,number2,number3);
float average=findAverage(total,3);
float smallest=findSmallest(number1,number2,number3);
cout<<"\nFirst number " <<number1<<"\n";
cout<<"Second number " <<number2<<"\n";
cout<<"Third number " <<number3<<"\n";
cout<<"Total " <<total<<"\n";
cout<<"Average " <<average<<"\n";
cout<<"Smallest " <<smallest<<"\n";
system("pause");
return 0;
}
Comments
Leave a comment