Finding Average of two numbers C++ [duplicate] - c++

This question already has answers here:
Printing the correct number of decimal points with cout
(13 answers)
Closed 1 year ago.
As a newbie in the world of programming, I have to write a bit of C++ code to find the average of two numbers.
However, my code somehow appears to be incorrect. Please take a look at my code:
#include <iostream>
using namespace std;
int main() {
float a, b, average;
cin >> a;
cin >> b;
average = (a+b)/2;
cout << average << endl;
}
however, it says I am wrong because when I input 10 10 it outputs 10 but the system wants me to output 10.00

You need to use some I/O manipulators.
std::setprecision and std::fixed
Example:
#include <iomanip>
#include <iostream>
int main() {
if(float a, b; std::cin >> a >> b) {
float average = (a+b)/2;
std::cout << std::fixed << std::setprecision(2) << average << '\n';
}
}

Like others have said you need to change your cout line to:
cout << fixed << setprecision(2) << average << endl;
Remember that with the <<s you're putting a stream of data (an iostream) into cout. The first piece of data is std::fixed which says "Display floats to a fixed number of decimal places, don't cut off any trailing zeros." And then std::setprecision(2) says "Make that fixed number of decimal places 2." You could use an int variable or another number in place of 2 if you wanted. From there the stream has your average and an endline like before.

Set decimal precision
Sets the decimal precision to be used to format floating-point values on output operations.
#include <iostream>
using namespace std;
#include <iomanip> // std::setprecision
int main() {
float a, b, average;
cin >> a;
cin >> b;
average = (a+b)/2;
cout << fixed << setprecision(2) << average << endl;
}

Related

Printing only decimal places of a number

I was wondering how can you only print the decimal places of the number?
I need to print 10.30 as 10m,30cm and I have no idea how to do that.
I have tried to google the solution, but didn't find anything helpful.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double P;
cin >> P;
cout << fixed << setprecision(0) << P << "m," << setprecision(2) << P << "cm";
return 0;
}
Given a double P, and assuming you wanted to round half-way any fractions of cm, this is one approach:
unsigned long cm = std::round(P * 100); // ToDo - check the possibility of overflow
std::cout << cm / 100 << "m, " << cm % 100 << "cm";
Where I'm using integer arithmetic to extract the m, and modulo arithmetic to extract the cm. Enhance to handle negative P as required.

Use of double in codeblocks gives me int output

#include <iostream> using namespace std;
int main()
{
double x=5.0,y=4.0,z;
z=x+y;
cout<<x<<endl<<y<<endl<<z;
return 0;
}
The above program gives me the following output:
5
4
9
When I have declared the variables to be double and even z as double why do I get the output as integer value(9)??
cout is being helpful here: if the double value is a whole number, then it, by default, does not display a decimal separator followed by an arbitrary number of zeros.
If you want to display as many numbers as the precision that your particular double on your platform has, then use something on the lines of
cout.precision(std::numeric_limits<double>::max_digits10);
cout << fixed << x << endl;
Floating point numbers with no digits after the floating point are printed as integers by default.
To always show the floating point, use setiosflags(ios::showpoint).
You can combine that with fixed and setprecision(n) I/O flags to limit how many digits to print after the floating point. For example:
double d = 5.0;
cout << setiosflags(ios::showpoint) << d << endl; // prints 5.00000
cout << setiosflags(ios::showpoint) << fixed << setprecision(1)
<< d << endl; // prints 5.0

Why does cout truncate a double?

The following is my console input/output.
Please enter a real number: -23486.33
Characters checked: 9
Thank you.
The real number you entered is -23486.3
The value I entered is -23486.33, but yet cout prints it as -23486.3.
The relevant code is below:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
// Function prototype (declaration)
string readDouble();
bool isValidDouble(string);
int main()
{
string value;
double number;
value = readDouble();
while (!isValidDouble(value)) {
cout << "The number you entered is not a valid integer." << endl;
value = readDouble();
}
number = atof(value.c_str());
cout << "Thank you." << endl
<< "The real number you entered is " << number << endl;
}
When debugging, I check the value of number right after the method call atof(value.c_str())l;. Number is shown to have a value of -23486.33. So what happens between that and the print out by cout? In no part of my code do I set the precision of cout or make it fixed.
If you have any questions, please let me know.
Have you tried
std::cout << std::setprecision(2) << number;
look at:
http://www.cplusplus.com/reference/iomanip/setprecision/
-23486.3 is displayed because std::cout prints only 6 digits by default.
To print back a number entered from standard input (convertion text → floating number → text), you can use set_precision with digits10 as precision:
double d = -23486.33;
int precision = std::numeric_limits<double>::digits10;
std::cout << std::setprecision(precision) << d << std::endl;
This displays:
-23486.33
To print a number with full precision (usually for convertion floating number → text → floating number), you can use set_precision with max_digits10 as precision:
double d = -23486.33;
int precision = std::numeric_limits<double>::max_digits10;
std::cout << std::setprecision(precision) << d << std::endl;
This displays:
-23486.330000000002
Here the printed number is not the same because -23486.33 doesn't have an exact representation in IEEE encoding (expressed in base 2 instead of base 10).
For more details with digits10 and max_digits10, you can read:
difference explained by stackoverflow
digits10
max_digits10
Set a precision when you output a double and keep precision explicitly when you compare them.
When you convert a string presentation of a DEC number to a double(float point number presentation), the data in the memory might not be mathematically equal to the string presentation. It's the best approximation by a float point number presentation, and vise versa.
You can set the precision to the maximum limit for double.
The code snippet is here:
#include <iostream>
#include <limits>
#include <iomanip>
using namespace std;
double number = ... // your double value.
cout << setprecision(numeric_limits<double>::digits10) << number << endl;

Printing floating-point GPA in C++ [duplicate]

This question already has answers here:
Floating point format for std::ostream
(8 answers)
Closed 8 years ago.
my problem is when I am trying to print a floating-point GPA in C++.
It seems like a simple issue, but I can't get it to work. Basically I have a floating point value set to 4.0 for a GPA. However, when I try to print it like this:
cout << gpa << endl;
I get the value of 4. Without the .0 on the end. However, I want the .0 to show up. I have tried setting a precision but with no luck. Any help is appreciated.
You can use std::fixed in conjunction with std::setprecision
#include <iostream> // std::fixed
#include <iomanip> // std::setprecision
int main() {
double gpa = 4.0;
std::cout << std::fixed << std::setprecision(1) << gpa << std::endl;
return 0;
}
// Output is 4.0
#include <iomanip>
...
cout.setf(ios::fixed); // use fixed-point notation
cout.setf(ios::showpoint); // show decimal point
cout.precision(1);
...
cout << gpa << endl;

Print one (float) decimal using cout

I have a float number and I want to print one digit after decimal. How can I do this using cout? I have tried the following code but its giving wrong display.
#include <iostream>
using namespace std;
int main()
{
float time = 2.2;
cout.precision(1);
cout << time << endl;
return 0;
}
You need to set tge precision to one and float formatting flags to fixed:
std::cout << std::fixed << std::setprecision(1);
BTW, don't use std::endl. To get a newline use '\n' and if you really mean to flush the stream use std::flush.