Point class. distance formula. logic issue when plugging coordinate from the constructor and getting wrong result - distance

#include <iostream>
#include <cmath>
using namespace std;
class point{ // define point class
private:
float x=0;
float y=0;
public:
point();// default constructor
point(float, float);// constructor
void setX(float);
void setY(float);
double getX()const;
double getY()const;
};
//implement all the member function
point::point(){ }
point::point(float i, float k){
x=i;
y=k;
}
void point::setX(float xc){
x=xc;
}
void point::setY(float yc){
y=yc;
}
double point::getY()const{
return y;
}
double point::getX()const{
return x;
}
double operator + (const point&lhs, const point &rhs) // free function.
{
double dx=lhs.getX()-rhs.getX();
double dy=lhs.getX()-rhs.getY();
return sqrt(dx*dx+dy*dy);
}
int main(){
point p1(2, -1);
point p2(1, 5);
int dist=0;
dist = p1 + p2;
cout << "The distance between p1 " << "and p2" << " is " << dist << endl;
return 0;
}
This should be 5 but I got 3. I do not understand why?

replace
double dy=lhs.getX()-rhs.getY();
by
double dy=lhs.getY()-rhs.getY()

Related

How to pass an struct (with no atributes, only methods) to a trigonometrical function?

my problem is, I have an class that is supposed to be an X for a function, for example
auto f = x * x + 7.0;
and when called with f.e(3) it would return the function value.
It was resolved with operator overload on the * and +, but when i would try to do with trigonometrical functions, it doesnt work.
Here's what i've been trying to do:
auto f = sin(x);
that would later be invoked with an
f.e(5);
But its unable to do that because sin only accepts numbers as arguments, how would i go about doing that?
Here's class X:
class X {
public:
double e( double v ) {
return v;
}
};
The full code required to simply make it work is quite big, so I'm posting it on pastebin:
pastebin.com/SwKUUbyr
It also calculates the 1st derivative of the function.
This is the error:
error: no matching function for call to 'sin(X&)'
I'd love any input you guys can provide.
#include <cmath>
namespace formula {
template<class Lhs, class Rhs>
struct Times;
template<class Lhs, class Rhs>
struct Plus;
template<class D>
struct Formula {
double ex( double in ) { return static_cast<D&>(*this).ex(in); }
double dx( double in ) { return static_cast<D&>(*this).dx(in); }
template<class Rhs>
friend auto operator*( Formula<D>, Formula<Rhs> ) {
return Formula< Times<D, Rhs> >{};
}
template<class Rhs>
friend auto operator+( Formula<D>, Formula<Rhs> ) {
return Formula< Plus<D, Rhs> >{};
}
};
struct X:Formula<X> {
double ex(double in){ return in; }
double dx(double in){ return 1; }
};
X x;
template<class Lhs, class Rhs>
struct Times:Formula<Times<Lhs, Rhs>> {
double ex( double in ) {
return Lhs{}.ex(in) * Rhs{}.ex(in);
}
double dx( double in ) {
return Lhs{}.ex(in) * Rhs{}.dx(in) + Lhs{}.dx(in) * Rhs{}.ex(in);
}
};
template<class Lhs, class Rhs>
struct Plus:Formula<Plus<Lhs, Rhs>> {
double ex( double in ) {
return Lhs{}.ex(in) + Rhs{}.ex(in);
}
double dx( double in ) {
return Lhs{}.dx(in) + Rhs{}.dx(in);
}
};
That is a bit better machinery that replicates what yours does.
Now to extend it:
template<class X>
struct Sin:Formula<Sin<X>> {
double ex( double in ) {
return ::std::sin(X{}.ex(in));
}
double dx( double in ) {
return X{}.dx(in) * ::std::cos(X{}.ex(in));
}
};
template<class X>
auto sin( Formula<X> ) {
return Sin<X>{};
}
}
Test code:
using namespace formula;
auto x_2 = x*x;
auto sin_x_2 = sin(x_2);
std::cout << sin_x_2.ex(0.) << "\n";
std::cout << sin_x_2.dx(0.) << "\n";
std::cout << sin(x).ex(0.) << "\n";
std::cout << sin(x).dx(0.) << "\n";
std::cout << (x*x).ex(1.) << "\n";
std::cout << (x*x).dx(1.) << "\n";
This can be extended with storing copies of the formula you depend upon to allow equations to contain some state.

When calling a member function of a class, I get Error C3867

In my displayData member function of the car class, the error says I should create a pointer to the member, do I have to create pointers to the member? If I do how so? I completely forget pointers. Do i make the object a pointer then point to the displayData member function? I get no red squiggles just an error message saying "use '&' to create a pointer to the member. I tried but had no luck.
#include<iostream>
#include<string>
using namespace std;
class Car
{
private:
int xVal, yVal, zVal;
protected:
public:
Car() { int xVal = 0; int yVal = 0; int zVal = 0; }
Car(int x,int y,int z) { xVal = x; yVal = y; zVal = z; }
~Car() {};
int getX() { return xVal; }
int getY() { return yVal; }
int getZ() { return zVal; }
void changeX(int n) { xVal = n; }
void changeY(int n) { yVal = n; }
void changez(int n) { zVal = n; }
virtual void getData();
void displayData();
};
class Sensor : public Car
{
private:
string sensorType;
protected:
public:
Sensor() { sensorType = "EMPTY"; }
Sensor(int x, int y, int z, string type) :Car(x,y,z) { sensorType = type; }
};
void Car::displayData()
{
cout << "The x values is: " << getX() << endl;
cout << "The y values is: " << getY() << endl;
cout << "The z values is: " << getZ() << endl;
}
int main()
{
Sensor n1;
Sensor n2(20,30,40, "Accelerometer");
n1.displayData;
n2.displayData;
return 0;
}
There is a syntax error in calling the function.
it should be
n1.displayData();
instead of
n1.displayData;

C++ Fix the following code so that it will correctly recursively traverse a directory tree in order to find where a particular file is

I have an assignment as following: Write a program will ask the user how many random numbers to generate. Then it will present a menu which has the options of Display, Average, Median, and Standard Deviation, Regenerate, and Quit. Without the use of a switch statement, or an if statements, or pointers to functions, have the program execute the user's selection from the menu. (Note: Function pointers are not allowed!)
This is what I have so far:
#include <iostream>
#include <cstdlib>
#include <array>
#include <cmath>
#include <stdlib.h>
#include <map>
using namespace std;
template <typename T> class wrapperclass
{
public:
static T myclass;
};
class Display
{
public:
static void myFunction(int random[], int num)
{
for(int i=0; i<num; ++i)
{
cout << random[i] <<endl;
}
}
};
class Average
{
public:
static double myFunction(int random[], int num)
{
double avg = 0;
for(int i=0; i<num; ++i)
{
avg += random[i];
}
return avg/num;
}
};
class Median
{
public:
static double myFunction(int random[], int num)
{
double mid = 0;
if(num % 2 == 0)
{
mid = (random[num/2] + random[num/2-1])/2;
}
else
{
mid = random[num/2];
}
return mid;
}
};
class StdDi
{
public:
static double myFunction(int random[], int num)
{
double avg=0;
double total=0;
for(int i=0; i<num; ++i)
{
avg += random[i];
}
avg = avg/num;
for(int i=0; i<num; ++i)
{
total += (avg-random[i])*(avg-random[i]);
}
total = total/num;
return sqrt(total);
}
};
class renerate
{
public:
static void myFunction(int)
{
}
};
class quit
{
public:
static void myFunction()
{
exit(EXIT_FAILURE);
}
};
int main()
{
int num = 0;
int option = 0;
map<int, class T> magic;
cout << "How many random numbers would u like to generate? " << endl;
cin >> num;
int random[num];
for(int i=0; i<num; ++i)
{
random[i] = rand() % 100 + 1;
}
cout << " Menu"<<endl
<< "1. Display"<<endl
<< "2. Average"<<endl
<< "3. Median"<<endl
<< "4. Standard Deviation"<<endl
<< "5. Renerate"<<endl
<< "6. Quit"<<endl;
cin >> option;
cout<<wrapperclass<Average>::myclass.myFunction(random, num);
return 0;
}
I'm about to directly pass the user input "option" into that "wrapperclass" like this "wrapperclass" so I can simply call the .myFunction since all classes have the same function name. but this won't work for c++ so is there any work around?

std::sort using member function in the same class?

I have a class "PclProc" and I want to use std::sort.
I write a compare function in the same class because this comparing need the "in_ptr" which is a variable in the same class.
But as I did as following, there is always an error:
error: no matching function for call to
‘sort(std::vector::iterator, std::vector::iterator,
)’
std::sort(cloud_indice.indices.begin(),cloud_indice.indices.end(),PclProc::MyCompare);
bool PclProc::MyCompare(int id1, int id2)
{
return in_ptr->points[id1].z<in_ptr->points[id2].z;
}
float PclProc::MedianZDist(pcl::PointIndices cloud_indice)
{
std::sort(cloud_indice.indices.begin(),cloud_indice.indices.end(),PclProc::MyCompare);
int size=cloud_indice.indices.size();
float median_x,median_y;
...
Example of a functor being used for std::sort. vector D is the data, vector I is the indices to D. I is sorted according to D with std::sort using the functor. std::sort only creates one instance of class lessthan, then uses that one instance for all of the compares.
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <vector>
typedef unsigned int uint32_t;
#define SIZE 16
class example{
public:
std::vector<uint32_t> D; // data
std::vector<uint32_t> I; // indices
example(void)
{
D.resize(SIZE);
I.resize(SIZE);
for(uint32_t i = 0; i < SIZE; i++){
D[i] = rand()%100;
I[i] = i;
}
}
void displaydata(void)
{
for(size_t i = 0; i < SIZE; i++)
std::cout << std::setw(3) << D[I[i]];
std::cout << std::endl;
}
class lessthan // lessthan functor for std::sort
{
public:
const example &x;
lessthan(const example &e ) : x(e) { }
bool operator()(const uint32_t & i0, const uint32_t & i1)
{
return x.D[i0] < x.D[i1];
}
};
void sortindices(void)
{
std::sort(I.begin(), I.end(), lessthan(*this));
}
};
int main()
{
example x;
x.displaydata();
x.sortindices();
x.displaydata();
return 0;
}

trying to link two cpp files with a header

I'm trying to call a function from a cpp file (with a list of functions) in another cpp file, I'm using a header file to set the function prototype in both cpp files, my problem is that I'm getting this LNK2019 error. I don't know what i'm doing wrong. I've been going back and forth between a few variations, the current one seems to be the most correct based on what i've read. I've been working on this for hours, reading a bunch of threads, but nothing seems to explain this problem, i'm using microsoft visual studio 2012
this is the header file, Rectangle.h
#pragma once
class Rectangle
{
private:
int width, height;
double gravWidth, gravHeight;
public:
Rectangle();
double getAreaBig();
double getAreaSmall();
int getPerimeter();
void getLength(int b);
void getWidth(int a);
int setLength();
int setWidth();
~Rectangle();
};
this is the cpp file containing the functions, Rectangle.cpp
#include <iostream>
#include "Rectangle.h"
using namespace std;
Rectangle::Rectangle()
{
width = 1;
height = 1;
gravWidth = .5;
gravHeight = .5;
}
double Rectangle::getAreaBig ()
{
return double ((width * height) - (gravWidth * gravHeight));
}
double Rectangle::getAreaSmall()
{
return ((gravWidth) * ( gravHeight));
}
int Rectangle::getPerimeter()
{
return (2 * (width + height));
}
void Rectangle::getLength(int b)
{
height = b;
gravWidth = (1/2 * width);
}
void Rectangle::getWidth(int a)
{
width = a;
gravHeight = (1/2 * height);
}
int Rectangle::setLength()
{
return height;
}
int Rectangle::setWidth()
{
return width;
}
what follows is the app.cpp file, this is where i'm getting the errors, italics are where the errors seem to be pointing too
#include <iostream>
#include "Rectangle.h"
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <string>
#include <sstream>
#include <cstddef>
using namespace std;
int main ()
{
int const WIDTH = 10;
setprecision(2);
int length = 0;
int width = 0;
cin >> width;
cin >> length;
Rectangle garden;
Rectangle gravel;
garden.getLength(length);
garden.getWidth(width);
cout << "Length of lawn: " << garden.setLength() << "Width of lawn: " << garden.setWidth();
cout << "Cost of grass: " << garden.getAreaBig();
cout << "Length of gravel: ";
cout << "Width of gravel: ";
cout << "Cost of gavel: ";
system ("pause");
}
The first thing that I see is that you are not instantiating the rectangles.
What about defining
~Rectangle();
somewhere? If your destructor is not going to do anything, you can just provide an empty body in the header file.
~Rectangle() {};
(And just in case, make it virtual, it's always a good habit.)