#include <iostream>
using namespace std;
double average(int x);
int main()
{
    int a,b,c;
    cout <<"average is:"<<average(a,b,c);
    cin >>a>>b>>c;
    return 0;
}

double average(int x)
{
  return (x+x+x)/3;
}

Dani AI

Generated

Brief summary: the original snippet calls a function with uninitialised variables and uses a function signature that doesn’t match the call. and correctly pointed out the input/order and argument-count problems, while and highlighted the need for floating-point division to avoid truncation. The fixes fall into three clear checks: initialization/input order, matching the function declaration/definition to how it’s called, and making sure arithmetic produces a floating-point result when needed.

Uninitialised variables: using variables before they get values is undefined behavior; results can be anything and differ between compilers or runs. See the C++ undefined-behaviour overview for details: Undefined behavior. Always read input into variables (or initialize them) before using them in calculations.

Integer vs. floating-point division: if all operands are integers, the division will truncate to an integer. To get a true average, make the computation use a floating type (cast numerator or use a floating literal like 3.0) so the result isn’t truncated. The rules for arithmetic conversions and integer division are explained here: Arithmetic operators.

Function signatures and robustness: the declaration/prototype must match the call in parameter count and types; see Functions. Also validate input state after extraction (check stream status) to avoid silent failures (see the I/O overview: Input/output library). Additional practical tips: use a prototype before main, prefer double for averages unless constrained, handle non-numeric input gracefully, and consider using explicit casts or floating-point parameters to avoid surprises with promotion and overflow.

Recommended Answers

All 4 Replies

int a,b,c;
cout <<"average is:"<<average(a,b,c);
cin >>a>>b>>c;

I always wonder how it makes sense to do the work before getting the values when the work depends on the values. Try this:

int a,b,c;
cin >>a>>b>>c;
cout <<"average is:"<<average(a,b,c);

Also, the function is not defined to take 3 arguments. This will fit your call better:

double average(int x, int y, int z)
{
    return (x+y+z)/3;
}

help you fix what??? The most obvious problem is that it is using uninitialized variables a, b and c. Next, function average only takes one argument, not 3. Either change that function to accept three arguments or only pass one.

[edit]^^^ what Tom said. [/edit]

Where you go

#include <iostream>
using namespace std;
double average(int,int,int);
int main()
{
	int a,b,c;	
	cin >>a>>b>>c;
	cout <<"average is:"<<average(a,b,c) << endl;
	return 0;
}

double average(int x,int y, int z)
{
	return (x+y+z)/3.0;
}

hi.. please try this one.. im not sure if this is what you wanted but i hope this code can help you..

#include<iostream>
using namespace std;

int main()
{
float a,b,c;
double x;
// x = average of a,b,c

cout<<"Insert 3 Average: "
cin>>a;
cin>>b;
cin>>c;

x = (a+b+c)/3;

cout<<"The average is "<<x;

return 0;
}

™Thug Line™

commented: Learn to use code tags. -7
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.