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?
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
#include <iostream>
using namespace std;
void mult ();
int main() {
cout << "Please enter two integers: ";
mult();
}
void mult(int a, int b){
int ans = a * b;
cout << "The answer is: " << ans << "." << endl;
}
Yes you can. But you should not take a and b as input parameter. You should declare them in function and get value from user. But its better to get the value from user in main and then send the values as parameter to function mult like code below
There are few errors also in your code. You are defining mult(int a,int b) with parameters a an b but in declaration there are no parameters. Similarly in calling function you are not sending parameter values in the function. Correct code is as follow
#include <iostream>
using namespace std;
void mult (int a,int b);
int main() {
int a=0,b=0;
cout << "Please enter two integers: ";
cin>>a;
cin>>b;
mult(a,b);
}
void mult(int a, int b){
int ans = a * b;
cout << "The answer is: " << ans << "." << endl;
}
#imran is correct. As he said, you can pass arguments after taking inputs or you can take inputs in mult() functions by declaring variables there itself.
#include <iostream>
using namespace std;
void mult ();
int main() {
mult();
}
void mult(){
int a,b;
cout << "Please enter two integers: ";
cin>>a;
cin>>b;
int ans = a * b;
cout << "The answer is: " << ans << "." << endl;
}
I am trying to use class to create a program, that with user input, can perform the operations of the Pythagorean theorum, but i get this error:
Error (active) E0304 no instance of overloaded function "getline" matches the argument list
This is my code:
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
class PythagoreanTheorum
{
public:
double a;
double b;
double c;
void seta(double A)
{
a = A;
}
void setb(double B)
{
b = B;
}
void setc(double C)
{
c = C;
}
double calcAreea()
{
return a * pow(a, 2) + b * pow(b, 2) == c * pow(c, 2);
}
};
int main()
{
//Define one right triangles
PythagoreanTheorum righttriangle1;
double a;
double b;
double c;
cout << "Enter the value for a: " << endl;
righttriangle1.a = getline(cin, a);
cout << "Enter the value for b: " << endl;
righttriangle1.b = getline(cin, b);
cout << "Enter the value for c: " << endl;
righttriangle1.c = getline(cin, c);
}
std::getline reads strings, not doubles. So you'd have to read with std::string and then convert it to double (with stod).
You can instead use >> operator for inputs, too:
cout << "Enter the value for a: " << endl;
std::cin >> righttriangle1.a;
cout << "Enter the value for b: " << endl;
std::cin >> righttriangle1.b;
cout << "Enter the value for c: " << endl;
std::cin >> righttriangle1.c;
This is how I would write the code, assuming that calcAreea() in your code is meant to show Pythagoreas Theorem being applied.
My code:
#include <iostream>
#include <cmath>
using namespace std;
class PythagoreanTheorum
{
public:
void seta(double A)
{
a = A;
}
void setb(double B)
{
b = B;
}
void setc(double C)
{
c = C;
}
double calcTheorem()
{
cout<<"Applying Pythagoreas Theorem:"<<pow(a,2)<<"+"<<pow(b,2)<<"="<<pow(c,2);
}
private:
double a;
double b;
double c;
};
int main()
{
//Define one right triangles
//Test -> a = 3, b = 4, c = 5
PythagoreanTheorum righttriangle1;
double a;
double b;
double c;
cout << "Enter the value for a: " << endl;
cin>>a;
righttriangle1.seta(a);
cout << "Enter the value for b: " << endl;
cin>>b;
righttriangle1.setb(b);
cout << "Enter the value for c: " << endl;
cin>>c;
righttriangle1.setc(c);
righttriangle1.calcTheorem();
}
I removed string header file since it wasn't being used, I also used cin instead of getline since that's way better in this case, also I wanted to not use using namespace std; but since it was in your code I retained it & also renamed calcAreea as calcTheorem since it wasn't calculating area
EDIT: I forgot to mention I declared variables in class in private instead of public
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.
I already type the function but I can't get the output. Can anyone help me. It must use predefined function. My program is running but it have some errors so please help me.
#include <iostream>
using namespace std;
float largeNum (float a, float b)
{
float largeNum;
if(a>b)
cout<<"a is larger";
if(b>a)
cout<<"b is larger";
return (largeNum);
}
int main()
{
float num1, num2;
cout<<"Enter number";
cin>>num1;
cout<<"Enter number";
cin>>num2;
cout<<largeNum<< "is larger"<<endl;
return 0;
}
#include <iostream>
using namespace std;
float largeNum (float a, float b)
{
if(a>b)
return a;
return b;
}
int main()
{
float num1, num2;
cout<<"Enter number";
cin>>num1;
cout<<"Enter number";
cin>>num2;
cout<<largeNum(num1, num2)<< "is larger"<<endl;
return 0;
}
Try to change your code to below. You have not given any thing to largeNum, so you cannot return it.
#include <iostream>
using namespace std;
float largeNum(float a, float b)
{
if (a > b)
cout << "a is larger";
if (b > a)
cout << "b is larger";
return 0;
}
int main()
{
float num1, num2;
cout << "Enter number";
cin >> num1;
cout << "Enter number";
cin >> num2;
largeNum(num1, num2);
getchar();
getchar();
return 0;
}
#include <iostream>
using namespace std;
void largeNum(float a, float b)
{
if (a > b)
{
cout << a << " larger";
}
if (b > a)
{
cout << b << " larger";
}
}
int main()
{
float num1, num2;
cout << "Enter number";
cin >> num1;
cout << "Enter number";
cin >> num2;
largeNum(num1,num2);
return 0;
}
in your code you just define the function but you are not calling this function main so thats why u get error