#include<iostream>
#include<cmath>
#include<ctime>
#include<cstdlib>
#include<windows.h>
using namespace std;
void z()
{
Sleep(100);
}
class Car
{
double fuel;
double speed;
double X;
double Y;
public:
Car(double n, char *type)
{
fuel=n;
speed=120;
}
double Speed()
{
return speed;
}
void Position(double p, double q)
{
X = p;
Y = q;
}
void Move()
{
X=X+(Speed()/3600);
Y=Y+(Speed()/3600);
}
};
int main()
{
Car c(70,"Car");
double x,y;
c.Position(3.2,2.2);
cout<<c.Speed()<<endl;
while(1)
{
c.Move();
c.Position(x,y);
cout<<x<<","<<y<<endl;
z();
}
return 0;
}
I want to show the changes of position in the same line that means in each second the value of position will be updated and show it in the same line replacing the older value but will not create any new line.
Related
Create a class Rectangle. This class has attributes length and width each of which defaults to 1. It has methods that calculate the perimeter and area of the rectangle. It has set and get methods for both length and width. The set methods should verify that length and width are floating – point nos larger than 0.0 and less than 20.0.
#include<iostream>
using namespace std;
class rect
{
float l;
float w;
public:
void setlw();
float getl(float len);
float getw(float width);
void seta();
void setp();
};
void rect:: setlw()
{
cout<<"enter the lenght and width"<<endl;
cin>>l>>w;
}
float rect:: getl(float len)
{
if (l>=0.0 && l<=20.0)
len=l;
else
len=1.0;
return(len);
}
float rect:: getw(float width)
{
if(w>=0 && w<=20.0)
width=w;
else
width=1.0;
return(width);
}
void rect::seta()
{
float a;
a=l*w;
cout<<"the area is"<<a<<endl;
}
void rect:: setp()
{
float p;
p=2*(l+w);
cout<<"the perimeter is"<<p<<endl;
}
int main()
{
rect r;
r.setlw();
cout<<"length is"<<r.getl(float)<<endl;
cout<<"width is"<<r.getw(float)<<endl;
r.seta();
r.setp();
return (0);
}
I have corrected your code. Removed the input parameters for getl() and getw(), as it's not required when you are taking input with setlw() . You've also not declared variables len and width in functions getl and getw.
New Code:
#include<iostream>
using namespace std;
class rect
{
float l;
float w;
public:
void setlw();
float getl();
float getw();
void seta();
void setp();
};
void rect::setlw()
{
cout<<"enter the lenght and width"<<endl;
cin>>l>>w;
}
float rect::getl()
{ float len;
if (l>=0.0 && l<=20.0)
len=l;
else
len=1.0;
return(len);
}
float rect::getw()
{ float width;
if(w>=0 && w<=20.0)
width=w;
else
width=1.0;
return(width);
}
void rect::seta()
{
float a;
a=l*w;
cout<<"the area is"<<a<<endl;
}
void rect::setp()
{
float p;
p=2*(l+w);
cout<<"the perimeter is"<<p<<endl;
}
int main()
{
rect r;
r.setlw();
cout<<"length is"<<r.getl()<<endl;
cout<<"width is"<<r.getw()<<endl;
r.seta();
r.setp();
return (0);
}
I have been trying to code a program that can solve for c using the Law Of Cosines. The program runs correctly, but the answer I get is ridiculously big, noted by how it was in scientific notation.
Here is my code:
#include <iostream>
#include <cmath>
using namespace std;
class TrigMath
{
private:
double a;
double b;
double y;
public:
double LawOfCos()
{
return sqrt(pow(a,2) + pow(b,2) - 2*a*b*cos(y));
}
void seta(double A)
{
A = a;
}
void setb(double B)
{
B = b;
}
void sety(double Y)
{
Y = y;
}
};
int main()
{
TrigMath triangle1;
triangle1.seta(3);
triangle1.setb(4);
triangle1.sety(60);
cout << "c is equal to " << triangle1.LawOfCos() << endl;
return 0;
}
The cos() function there takes input as radians not as degrees.
Try to convert degrees to radians and then supply it as input.
In the class functions seta, setb and sety you have written A = a, B = b and Y = y.
You have to change them to a = A, b = B and Y = y.
So after applying all the changs the code should be like
#include <iostream>
#include <cmath>
using namespace std;
class TrigMath
{
private:
double a = 0;
double b = 0;
double y = 0;
public:
double LawOfCos()
{
return sqrt(pow(a,2) + pow(b,2) - 2*a*b*cos(y));
}
void seta(double A)
{
a = A;
}
void setb(double B)
{
b = B;
}
void sety(double Y)
{
y = Y*3.14/180;
}
};
int main()
{
TrigMath triangle1;
triangle1.seta(3.0);
triangle1.setb(4.0);
triangle1.sety(60.0);
cout << "c is equal to " << triangle1.LawOfCos() << endl;
return 0;
}
#include<iostream>
using namespace std;
class money
{
int rs;
int p;
public:
void setdata (int x , int y)
{rs=x; p=y;}
void show()
{ cout <<rs <<"." <<p; }
money operator += (int a) {
money temp;
temp.rs=rs+a.rs;
temp.p=p+a.p;
return (temp);
}
};
int main() {
money c1,c2;
c1.setdata(8,2);
c2=c1.operator+=(4);
c2.show();
}
Can someone tell me why the operator += overloading doesn't work?
My desiring output is 12.2 but the output i got is 16.2 .
I am sending 4 as argument and i want this argument is added in r (ruppee)
part
#include<iostream>
using namespace std;
class money
{
int rs;
int p;
public:
void setdata (int x , int y)
{rs=x; p=y;}
void show()
{ cout <<rs <<"." <<p; }
money& operator+=(int a)
{ rs += a; return *this; }
};
int main() {
money c1,c2;
c1.setdata(4,2);
c2=c1+=(4); //c2=c1.operator+=(4);
c2.show();
}
Try to use constructor correctly.
For example:
#include <iostream>
using namespace std;
class Example
{
public:
int x;
Example(int a)
{
x=a;
}
Example operator+(Example obj)
{
Example ans(0);
ans=x+obj.x;
return ans;
}
};
int main()
{
Example a(10),b(20);
Example ans=a+b;
cout<<ans.x<<endl;
return 0;
}
after searching i had found amazing code for integration by
quadrature boost library.
rather than
log(x)/(1+x)
want to integrate
(poly[0]+poly[1]*x+poly[2]*x^2+...+poly[n]*x^n)*log(x)/(1+x). But, i do not
know how to insert the vector
poly
to
struct f
or even how to call these operators from main function. The code :
#include<iostream>
#include<boost/math/constnats/constants.hpp>
#include<boost/multiprecision/cpp_dec_float.hpp>
#include <boost/numeric/quadrature/adaptive.hpp>
#include <boost/numeric/quadrature/kronrodgauss.hpp>
#include <boost/numeric/quadrature/epsilon.hpp>
using namespace std;
using boost::multiprecision::cpp_dec_float_50;
namespace quadrature=boost::numeric::quadrature;
struct f
{
double operator()(double x) const {
return (log(x)/(1+x); }
};
int main()
{
vector<cpp_dec_float_50> poly(0);
cpp_dec_float_50 p = 0;
for (int i=0;i<=n;i++)
{
p=polynomial(i,n);
poly.push_back(p);
}
double answer,error_estimate;
quadrature::adaptive().relative_accuracy(1e-5).absolute_accuracy(1e-7)
(f(),0.,1.,answer,error_estimate);
cout<<"ans"<<answer<<endl;
return 0;
}
cpp_dec_float_50 polynomial(int k ,int n)
{
.
.
.
}
Also, when changing the double operator, to cpp_dec_float_50 operator in
struct f
many problems arise. and the later type is necessary in my project. Any one can fix that ?
EDIT
i tried this, but i do sth wrong
#include<iostream>
#include <boost/numeric/quadrature/adaptive.hpp>
#include <boost/numeric/quadrature/kronrodgauss.hpp>
#include <boost/numeric/quadrature/epsilon.hpp>
#include<boost/math/constants/constants.hpp>
#include<boost/multiprecision/cpp_dec_float.hpp>
using namespace std;
using boost::multiprecision::cpp_dec_float_50;
namespace quadrature=boost::numeric::quadrature;
double polynomial(int k ,int n);
struct f
{ const cpp_dec_float_50 s=0;
vector<cpp_dec_float_50> poly;
cpp_dec_float_50 sum()const{
for(int i=0;i<=poly.size();i++)
s+=poly[i];
return s
}
double operator()(double x) const {
return
s*log(x)/(1+x); }
};
int main()
{
int n=2;
f fun;
cpp_dec_float_50 p = 0;
for (int i=0;i<=n;i++)
{
p=polynomial(i,n);
fun.poly.push_back(p);
}
double answer,error_estimate;
quadrature::adaptive().relative_accuracy(1e-5).absolute_accuracy(1e-7)
(fun,0.,1.,answer,error_estimate);
cout<<"ans"<<answer<<endl;
return 0;
}
double polynomial(int k ,int n)
{
return k;
}
Edit
when using Patstew suggestion
Two errors occur
Try something along the lines of:
struct f
{
vector<cpp_dec_float_50> poly;
double operator()(double x) const {
return (poly[0]+poly[1]*x+poly[2]*x^2+...+poly[n]*x^n)*log(x)/(1+x); }
};
int main()
{
f fun;
cpp_dec_float_50 p = 0;
for (int i=0;i<=n;i++)
{
p=polynomial(i,n);
fun.poly.push_back(p);
}
double answer,error_estimate;
quadrature::adaptive().relative_accuracy(1e-5).absolute_accuracy(1e-7)
(fun,0.,1.,answer,error_estimate);
cout<<"ans"<<answer<<endl;
return 0;
}
EDIT: RE you own answer, you never call sum (and s is const so you couldn't change it if you did) so s is always 0 and you will always get 0 as your answer. Also you are iterating all the way up to poly.size() in sum(), but poly[poly.size()-1] is the last element. I think you really want your sum function to calculate a polynomial? Try this:
#include<iostream>
#include <boost/numeric/quadrature/adaptive.hpp>
#include <boost/numeric/quadrature/kronrodgauss.hpp>
#include <boost/numeric/quadrature/epsilon.hpp>
#include<boost/math/constants/constants.hpp>
#include<boost/multiprecision/cpp_dec_float.hpp>
using namespace std;
using boost::multiprecision::cpp_dec_float_50;
namespace quadrature=boost::numeric::quadrature;
double polynomial(int k ,int n);
struct f
{
vector<double> poly;
double polysum(double x) {
double s = poly[0];
double p = 1;
for(int i=1;i<poly.size();i++) {
p = p*x;
s+= p*poly[i];
}
return s
}
double operator()(double x) {
return polysum(x)*log(x)/(1+x); }
};
int main()
{
int n=2;
f fun;
double p = 0;
for (int i=0;i<=n;i++)
{
p=polynomial(i,n);
fun.poly.push_back(p);
}
double answer,error_estimate;
quadrature::adaptive().relative_accuracy(1e-5).absolute_accuracy(1e-7)
(fun,0.,1.,answer,error_estimate);
cout<<"ans"<<answer<<endl;
return 0;
}
double polynomial(int k ,int n)
{
return k;
}
I have tried to code the DFS algorithm as given in CLRS. Here's the code below. When I run it I got an error as "Your program stopped unexpectedly." When I debugged the code I got this line in the call stack "msvcrt!malloc()" and "operator new(unsigned int)". I'm using CodeBlocks. Where am I wrong?
#include<iostream>
#include<cstdlib>
#include<vector>
#include<list>
#include<utility>
#include<algorithm>
#include<string>
using namespace std;
struct prop
{
int p;
int value;
int d;
int f;
string color;
};
vector<prop>v;
prop make_prop(int a,int b,int c,int d,string e)
{
prop p = {a,b,c,d,e};
return p;
}
class Dfs
{
public:
int time;
vector<list<int> >adj;
Dfs(int nv)
{
v.resize(nv);
adj.resize(nv);
for(int i=0;i<nv;i++)
{
v[i].value = i;
v[i].p = -1;
v[i].color = "WHITE";
}
}
void addinput()
{
adj[0].push_back(1);
adj[0].push_back(2);
adj[0].push_back(3);
adj[1].push_back(0);
adj[1].push_back(3);
adj[2].push_back(0);
adj[2].push_back(3);
adj[3].push_back(0);
adj[3].push_back(1);
adj[3].push_back(2);
}
void dfs();
void dfsvisit(prop);
};
void Dfs::dfs()
{
time = 0;
for(int i=0;i<v.size();i++)
{
if(v[i].color == "WHITE")
{
dfsvisit(v[i]);
}
}
}
void Dfs::dfsvisit(prop m)
{
time++;
m.d = time;
m.color = "GRAY";
int val = m.value;
for(auto it = adj[val].begin();it != adj[val].end();it++)
{
if(v[*it].color == "WHITE")
{
v[*it].p = val;
dfsvisit(v[*it]);
}
}
m.color = "BLACK";
cout<<m.value;
time++;
m.f = time;
}
int main()
{
Dfs d(4);
d.addinput();
d.dfs();
return 0;
}
void Dfs::dfsvisit(prop m) // should be prop&
dfsvisit(prop m) will make a copy of the property while dfsvisit(prop& m) receives a reference, working directly on the property you passed to the function
the stack will overflow!
In function dfsvisit,you pass parameter by value,which will never change the actual parameter.You should pass parameter by reference.
void dfsvisit(prop& m);