Results 1 to 3 of 3
This sounds like it should be easy, but I can't get it to work.
I need to write a program that can average the absolute value of 5 numbers that ...
Enjoy an ad free experience by logging in. Not a member yet? Register.

 08192011 #1
 Join Date
 Aug 2011
 Posts
 38
Sounds easy, but somehow....?
I need to write a program that can average the absolute value of 5 numbers that the user inputs.
I've written this:
/*
This program averages the absolute value of five values entered
by the user.
*/
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
int a , b , c , d , e;
double f;
cout << "Enter the first number: ";
cin >> a;
cout << "Enter the second number: ";
cin >> b;
cout << "Enter the third number: ";
cin >> c;
cout << "Enter the fourth number: ";
cin >> d;
cout << "Enter the fifth number: ";
cin >> e;
f = ( abs(a) + abs(b) + abs(c) + abs(d) + abs(e) ) / 5;
cout << "The average of the 5 numbers is " << f << "\n";
return 0;
}
I have a few questions:
1). Why doesn't the abs() function work when I change int a , b , c , d , e; to double a , b , c , d , e;? Even negative fractional numbers have an absolute value.
2). Why doesn't this program give me fractional answers? I'm defining f to be a double, so f can take decimal values.
If someone could answer these, it would be great!
 08192011 #2
Well, according to the internet, the prototype for abs is
Code:int abs(int aNum);
There is nothing to stop you writing your own absolute function handling doubles which should
return the passed number if it is positive
otherwise
return the passed number * 1What do we want?
Time machines!
When do we want 'em?
Doesn't really matter does it!?
Conkybots: Interactive plugins for your Conkys!
 08202011 #3
 Join Date
 Aug 2011
 Posts
 7
Alright there's a few problems here. For one using abs() on the integers is a little superflous. It would be better to force the user to enter data in a range you specify versus converting their input. (At least in this case) Secondly your doing an implicit conversion of integers to a double. Therefore, the compiler is going to round the number. If you want decimal points to be included, change the integer variables to doubles. (And be sure to remove the abs() functions) Then you will get a number containing a decimal point. This is the code I used in my with my compiler. I also added a simple absolute function for you.
Code:#include <iostream> #include <vector> using namespace std; double get_absolute(double value) { if(value < 0) { value = value  (value*2); return value; }else { return value; } }; int main() { //Create vector list and double variables vector<double> dList; double a, b, c; //Loop User Input for(int i=1; i<=5; i++) { cout << "Enter number " << i << ": "; cin >> a; //This is what I would do if I was entering the data myself //Nonetheless I included the absolute function for you //Only push variable if greater than 1 a = get_absolute(a); if(a > 1) { dList.push_back(a); }else { cout << "Number not in range!" << endl; i; } } //Create iterator vector<double>::iterator iter; //Determine total value for(iter=dList.begin(); iter!=dList.end(); iter++) { b = b + *iter; } //Find average if(b > 0) { c = b / 5; cout << "The average of the 5 numbers is " << c << "\n"; return 0; }else { cout << "Error all numbers were 0!" << endl; return 1; } }
Last edited by mcross1882; 08202011 at 07:01 PM. Reason: Added get_absolute() function (Aug20/12:55pm)