Have this task:
#include <iostream>
#include <clocale>
#include <math.h>
using namespace std;
int main()
{
using std::setlocale;
setlocale (LC_ALL,"");
double x, y, z, a, b, c, s;
cout << "Enter x: ";
cin >> x;
cout << "Enter y: ";
cin >> y;
cout << "Enter z:";
cin >> z;
a = sqrt(10*(pow(x,1/3)*(x,y+2)));
b = pow(asin(z),2);
c = fabs(x-y);
s = a*(b-c);
cout << "Result s = " << s << endl;
return 0;
}
Don't know why result is shown is NaN. Could you please help? My code is above.
Sample added:
Here you go:
#include <iostream>
#include <clocale>
#include <cmath>
using namespace std; //to simplify code, you shouldnt be using this
int main()
{
double x = 16.55*pow(10,-3);
double y = -2.75;
double z = 0.15;
double h = 1.0/3.0;
double s = sqrt(10*(pow(x,h)+pow(x,y+2)))*(pow(asin(z),2)-abs(x-y));
cout<<s;
}
Just as Sam mentioned, 1/3 is not what it seems to be. In that state it's calculated as an int, which gives 0 as a result.
Related
I want to display 265612.2 how ever the output still 265612
I input : 4.2
#include <iostream>
using namespace std;
int main()
{
double a;
double b;
cout<<"Enter the number of light year : ";
cin>>a;
b = a * 63241;
cout<<b;
return 0;
}
You need to "manipulate" the output stream (std::cout):
#include <iomanip> // manipulating helpers
#include <iostream>
int main() {
double a;
double b;
std::cout<<"Enter the number of light year : ";
if(std::cin >> a) {
std::cout << std::setprecision(1) // one decimal
<< std::fixed; // fixed precision
b = a * 63241.01;
std::cout << b;
}
}
Demo
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
I made this simple c++ program in code::blocks IDE:
#include <iostream>
#include "funct.cpp"
using namespace std;
int main()
{
float a, b;
cout << "enter a : ";
cin >> a;
cout << "enter b : ";
cin >> b;
cout << "\n\nThe result is: " << funct(a, b) << "\n";
return 0;
}
and this function:
#include <iostream>
using namespace std;
float funct(float x, float y)
{
float z;
z=x/y;
return z;
}
when I create the function in the IDE by creating new empty file and try to build the program it returns this error:
but when I create the same function file manually by the text editor and put it in the same folder of the project it works fine and the compiler can build it with no errors.
So is this because I am doing something wrong or is it a bug in the IDE ?
Can you help me out of this ?
and thanks in advance.
You are messing up the project:
1st you should do is create a header file function.h or function.hpp, in there place the header of the function
function.h:
float funct(float x, float y);
then a
function.cpp: that is where the concrete implementation happens:
float funct(float x, float y)
{
float z;
z = x / y;
return z;
}
then you are ready to include that into another file:
#include <iostream>
#include "funt.h"
using namespace std;
int main()
{
float a, b;
cout << "enter a : ";
cin >> a;
cout << "enter b : ";
cin >> b;
cout << "\n\nThe result is: " << funct(a, b) << "\n";
return 0;
}
you can for sure see a dirty/not_good-practice version where there is no header
in that version no include is required, but you need to prototype the functions you need
function.cpp: that is where the concrete implementation happens:
float funct(float x, float y)
{
float z;
z = x / y;
return z;
}
and the main:
#include <iostream>
using namespace std;
float funct(float x, float y);
int main()
{
float a, b;
cout << "enter a : ";
cin >> a;
cout << "enter b : ";
cin >> b;
cout << "\n\nThe result is: " << funct(a, b) << "\n";
return 0;
}
as Neil Butterworth said above, no cpp files must be included..
Don't include .cpp files. Instead put a forward declaration of the function in a .h or .hpp file that would look like this float funct(float x, float y);, and include that file.
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
int x, y;
int main()
{
cout << "Please give me a number:";
int x = (cin, x);
cout << "Please give me another number:";
int y = (cin, y);
cout << "The sum of " << x;
cout << "and " << y;
cout << "is " << x+y;
}
Can anyone tell me why(as simple as it is) this doesn't add?
I'm not really sure how to return user input for numbers and the like. Just started learning this.
I believe instead of this:
int x = (cin, x);
you wanted this:
cin >> x;
cin (console input) works pretty much the same way as cout (console output), which you used properly.
You may want to read about them more:
std::cout
std::cin
Also, you do not need to redefine x and y in main(), as they are global variables.
Correct code is :
#include <iostream> // for cin,cout we use iostream
#include <stdio.h> // you don't need this header file in this program
#include <string> // also you don't need this header
using namespace std;
int main()
{
int x,y;
cout<<"Please give me a number : ";
cin>>x;
cout<<"Please give me another number : ";
cin>>y;
cout<<"The sum of "<<x<<" and "<< y<<" is "<<x+y;
return 0;
}
Read Basic_Syntax
I have this code:
#include <iostream>
#include <cmath>
#include <stdlib.h>
using namespace std;
double f(double x);
double biseccion(double a, double b, double tolerancia, int maxiter);
int main()
{
double a, b, raiz;
double tolerancia=0.00000;
int maxiter=25;
cout << "Input begin of interval: ";
cin >> a;
cout << "Input end of interval: ";
cin >> b;
cout << "\n";
cout << " # de"<<"\n"<<"Iteration"<<"\t"<<" A"<<"\t"<<" B"<<"\t"<<" C"<<"\t"<<" f(c)"<<endl;
raiz=biseccion(a,b,tolerancia,maxiter);
cout << "\n";
cout << "The root is: "<< raiz <<endl;
return 0;
}
double f(double x)
{
return x*x*x-x-2;
}
double biseccion(double a, double b, double tolerancia, int maxiter)
{
double c;
int numiter=1;
do
{
c=(a+b)/2;
if(f(a)*f(c)<0)
{
b=c;
}
else
{
a=c;
}
cout<<" "<<numiter<<"\t"<<"\t"<<a<<"\t"<<b<<"\t"<<c<<"\t"<<f(c)<<endl;
numiter++;
}
while((abs(f(c))>tolerancia)&&(numiter<maxiter));
return c;
}
Instead of writing "x*x*x-x-2" in my code, I want user to input it before asking for begin of interval. How can I do that?
I try using a variable to store the "x*x*x-x-2", but none work.
You need to parse the input, its probably not as easy as you are thinking but there are some libraries that can help you.
muparser.sourceforge.net/
code.google.com/p/expressionparser/
partow.net/programming/exprtk/index.html
here is also a solution in c# that might help you too.
Is there a string math evaluator in .NET?