Average of 4 floating-point numbers.


I need to implement a C++ program that asks the user for four floating-point numbers. The program should then calculate the average using two different functions, one value returning and one void. The program should output the average of the four numbers. For this program I need to use float instead of int for the types of variables. Can anyone help me with this? Below is a proto-type code that I am able to use to do this program.


#include <iostream>
using namespace std;

int sum(int,int);
void sum(int,int,int&);
void print(int,int,int); // <- this is the function prototype for print


int main(){

float a,b,c,d,sum,avg;

std::cout<<"Inpute your numbers:"<<endl;
std::cin>>a>>b>>c>>d;

sum= a+b+c+d;

avg= sum/4

std::cout<<"Sum is:"<<sum<<endl;
std::cout<<"Average is:"<<avg<<endl;

sum=::sum(a,b);
print(a,b,sum);
return 0;
}

int sum(int a1,int b2){
return a1+b2;
}

void sum(int n1,int n2,int& answer){
answer=n1 + n2;
}

void print(int v1,int v2,int sum){
cout<<v1<<"+"<<v2<<"="<<sum<<endl;
}
Last edited on
1.)prompt the user for four doubles or floats
2.)Declare four doubles or floats
3.)Get them with cin>>a>>b>>c>>d;
4.)write function which calculates average of a,b,c,d. from a,b,c,d, passed to it and prints it.(void function)
5.) write a second function which calculates average similarly( from a,b,c,d passed to it) and returns it to main() where it it is then printed out in main()
Thank you for info. I am a beginner at this so I only understand some of what you are saying.
Ok I was able to do the float. Can you help me with the other functions that you mentioned?
Topic archived. No new replies allowed.