"HELP" I have until tuesday to get this program done for instructure.

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;
}
In C++ there are three data types that can represent floating-point numbers. They are...

float
double
long double

Short example:
1
2
3
4
5
6
7
8
int main(void)
{
   int i;
   float f;
   i = 7.5;
   f = i;
   cout << f << "\n\n";
}


In this case the output will be 7, some warnings are thrown but most of the time you can ignore them.
But I messed around with your code and changed all the integers. I dunno if this is how you want it, but mess around some more.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
using namespace std;

double sum(float, float);
void sum(float, float, float&);
void print(float, float, float);

int main(void)
{
	float a, b, c, d;
	double sum, avg;

	cout << "Input your numbers: ";
	cin >> a >> b >> c >> d;

	sum = a + b + c + d;

	avg = sum/4;

	cout << "\nSum is: " << sum << "\n\n";
	cout << "Average is: " << avg << "\n\n";

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

double sum(float a1, float b2)
{
	return a1 + b2;
}

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

void print(float v1, float v2, float sum)
{
	cout << v1 << " + " << v2 << " = " << sum << "\n\n";
}
Last edited on
Thank you so much for the help. I appreciate your time and knowledge.
I did code something close to this but could not get it to compile. I see now where I may have gone wrong. I will try this code and hope it works cause I have to see my instructure tomorrow. Thanks again.
No problem. This one works, though I think you get a warning which can be ignored. Only way to be sure is to test the data and expect what you should expect. Other than that, good luck with everything.
Topic archived. No new replies allowed.