How to make negative numbers not show? - c++

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
float V1, V0, t;
cout << fixed << showpoint << setprecision(2);
cout << "Please enter starting V0, V1, and t in terms of seconds" << endl;
cin >> V1 >> V0 >> t;
cout << (V1 - V0) / t << endl;
cin.get();
cin.get();
return 0;
}
(Formula is in code) I'm doing a problem and the example shows like this
Enter V0, V1 and t: 5.5 50.9 4.5
The Average is : 10.0889
However from my code which is rounded two decimal places when I enter those numbers I get
The Average is : - 10.09
Am I wrong? Can the average of velocity not be a negative number? And if so how do I make it so the negative does not appear. If anyone could lend me a hand that would be great.

You can use abs from cmath
#include <cmath>
...
cout << abs((V1 - V0) / t) << endl;

Related

How can a floating-point number be printed with more precision?

So i was attempting to code this question Link and i developed the logic and i started coding it. The code is shown below, but there is an issue with it. When i gave the code the following input (Image 1), the output came out to be 2.22582e+007, whereas the correct accepted output is 22258199.500000. What changes should i make in the data type to amend this error. How can i change the notation. Please bear with me as my knowledge of data types is limited.
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
int n,l;
cin >> n;
cin >> l;
vector<float> v;
vector<float> b;
for(int i=0; i<n; i++){
int x;
cin >> x;
v.push_back(x);
}
sort(v.begin(),v.end());
if(v[0]!=0){
b.push_back(v[0]);
for(int i=1; i<n; i++){
b.push_back((v[i] - v[i-1])/2.0);
}
}else if(v[0] == 0){
for(int i=1; i<n; i++){
b.push_back((v[i] - v[i-1])/2.0);
}
}
sort(b.begin(), b.end());
cout << b[b.size()-1];
}
You can use std::cout << std::setprecision(std::numeric_limits<float>::max_digits10) to output the value in a round-trippable format. (This will need headers <limits> and <iomanip>).
If that doesn't work, try also replacing float with double.
You're going to want to use iomanip.
#inlcude <iomanip>
cout<<fixed<<setprecision(6)
fixed will disable the scientific notation.
setprecision will allow you to be as precise as you want.
This resolved the issue.
link
The issue primarily was that the notation of the output was scientific and sometimes the online judges do not accept that, hence to change the notation from scientific to fixed and to other forms, the following code snippet can be used.
Either you can increase the precision or you can change the notation. Either of the latter cases can help.
#include <iostream>
#include <sstream>
int main()
{
std::cout << "The number 0.01 in fixed: " << std::fixed << 0.01 <<
'\n'
<< "The number 0.01 in scientific: " << std::scientific <<
0.01 << '\n'
<< "The number 0.01 in hexfloat: " << std::hexfloat << 0.01
<< '\n'
<< "The number 0.01 in default: " << std::defaultfloat <<
0.01 << '\n';
double f;
std::istringstream("0x1P-1022") >> std::hexfloat >> f;
std::cout << "Parsing 0x1P-1022 as hex gives " << f << '\n';
}
output of the given code - click here

Why my code is given me wrong results?

I don't know why my code is giving to me incorrect results.
When I put a number like 6670680902 the result is 6.67068e+0.7 (which is 66706800). That is not the correct result.
When I use the calculator the correct result of 667006080902 / 100 is 66706809.02.
What should I do to fix it?
#include "stdafx.h"
#include "conio.h"
#include "iostream"
using namespace System;
using namespace std;
int main()
{
float a;
float b;
cout << "Ingrese el codigo: "; cin >> a;
b = a / 100;
cout << "result: " << b;
_getch();
return 0;
}
The first issue here is by default C++ will show larger numbers using scientific notation, there are ways to prevent this for floating point numbers like floats. One simple way is to add << fixed before your number:
cout << "result: " << fixed << b;
Which will return 66706812.0.
The next problem is that floats are not good at being precise, which is why the number still isn't correct. Floats are less precise compared to something like a double which has twice the precision. If you use a double instead for a and b:
int main()
{
double a;
double b;
//...
cout << "result: " << fixed << b;
//...
}
you will get the value you expect: 66706809.02
Can do by using 'limits`
#include "iostream"
#include <string>
#include <limits>
using namespace System;
using namespace std;
int main()
{
double a;
double b;
cout << "Ingrese el codigo: "; cin >> a;
b = a / 100;
cout.precision(numeric_limits<double>::digits10 + 1);
cout << "result: " << b << endl;
_getch();
return 0;
}
Output:
result: 66706809.02

Adding a Probability Equation Into the Program

I've been working on this program in which it should calculate the probability based on the following formula:
𝑃(𝑥) = (𝑁!) / (𝑥!) * (𝑁−𝑥)!) * (p^x) * ((1-p)^(N-x))
Also, when the user types in a value, N must be an integer, x must be an integer which can be between 0 and N, and p must be a positive real number between 0 and 1. Till now this part works just fine but I don't know how to properly add the probability formula in the program.
The following is my code so far:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
long int factorial (int N, int x, int p);
int main ()
{
double N, x, p;
cout << "Input N value" << endl;
cin >> N;
cout << "Input x Value" << endl;
cin >> x;
while(x<=0 || x>=N){
cout << "x value is NOT between 0 and N." << endl;
cout << "Input x Value" << endl;
cin >> x;
}
cout << "Input p value" << endl;
cin >> p;
while(p<=0 || p>=1){
cout << "p value is NOT a real number between 0 and 1." << endl;
cout << "Input p value" << endl;
cin >> p;
}
return 0;
}
Can anyone help me out just to understand how to properly add an equation in my program?
Thank you!
This is my new code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double factorial (double N, double x, double p);
int main ()
{
double N;
double x;
double p;
cout << "Input N value" << endl;
cin >> N;
cout << "Input x Value" << endl;
cin >> x;
while(x<=0 || x>=N){
cout << "x value is NOT between 0 and N." << endl;
cout << "Input x Value" << endl;
cin >> x;
}
cout << "Input p value" << endl;
cin >> p;
while(p<=0 || p>=1){
cout << "p value is NOT a real number between 0 and 1." << endl;
cout << "Input p value" << endl;
cin >> p;
}
double Probability;
Probability = factorial(N, x, p);
cout << "Probability= " << Probability << endl;
return 0;
}
double factorial (double N, double x, double p){
double answer = ((tgamma(N+1))/((tgamma(x+1)) * (tgamma((N-x)+1)))) * (pow(p,x)) * (pow((1-p),(N-x)));
return answer;
}
The program recognizes the values I put in the system but when it calculates the answer, it gives a really small number. I tried out each section of the formula to make sure their was not a mistake but everything works fine when I tested it independently. Does anyone know what's wrong with the equation?
Thank you!
First you need to write a factorial function, check out this stackoverflow link:
How do you implement the factorial function in C++?
Then just write a function for your calculation. Assuming your factorial function is called getFact(int n) then:
double solve(int N, int x, double p) {
double answer = ( getFact(N)/getFact(x) )*getFact((N-x))* pow(p,x)* pow((1-p),(N-x));
return answer;
}
Then call the solve function in your main after having set your values.
double P_x;
P_x = solve(N,x,p);
Also, I use doubles because they can be more accurate, especially for p since its is 0 <= p <= 1.

C++ Variable keeps resetting to 0

So I've got this simple piece of code. What it's supposed to do is calculate centigrade from Fahrenheit. For some reason, however, it keeps resetting the value of cent to 0. Can anyone please explain why it's doing this? I'm sure I'm just missing something small and stupid.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main (void)
{
ofstream fout("temps.dat");
float fahr=0, avgcent=0, avgfahr=0, totalcent=0, totalfahr=0, gettemp=0;
double cent = ((5/9) * (fahr - 32));
const int gatekeeper=-99999;
fout << setw(25) << "Fahrenheit" << setw(20) << "Centigrade" << endl << endl;
fout.setf(ios :: fixed);
fout.setf(ios :: showpoint);
cout << "Please input Fahrenheit temp (-99999 to quit)" << endl;
cin >> fahr;
while (fahr != gatekeeper)
{
totalfahr = totalfahr + fahr;
totalcent = cent + totalcent;
cout << cent;
cin >> fahr;
}//End of while (gettemp != gatekeeper)
}//End of int main (void) for HW2
In your line double cent = ((5/9) * (fahr - 32));, the expression (5/9) resolves to 0 because it uses integer division. Replace it with (5.0/9.0) and you should get the correct result.

How to read the entire value of a double using cin?

long double m;
cout << "enter double: "; cin >> m;
cout << "m = " << m <<endl;
Input:
enter double: 1.546640625
Output:
m = 1.54664
I have to convert into a binary with point, and when I read numbers like 2.359375000
Output:
m = 2.35938
And it works, but I think the problem is the zero in 1.546640625
You have read the whole value of the double. The problem is with the cout. It by default rounds the value to 6 digits after the decimal point.
To set the precision cout uses, use setprecision from <iomanip>:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
long double d;
cin >> d;
cout << setprecision(10) << d << endl;
return 0;
}