Reputation: 956
I am trying to write some simple C++ code to output distance between points in d-dimensions. I define a point structure which has a function for calculating distance to any other point.
Testing this out on two points, it doesn't work. I thought it might be the sqrt function not working but even the function "test" outputting a double doesn't work.
I am using VS Code and the output is ... nothing.
I'm sure I'm missing something simple...
#include <iostream>
#include <cmath>
using namespace std;
struct point
{
static int d;
double *coords;
point(){ //point class
coords=new double[d];
for (int i=0; i<d; i++){
coords[i]=0;
}
}
double dist(point &q){
double squared=0;
for (int i=0; i<d;i++){
squared+=(coords[i]-q.coords[i])*(coords[i]-q.coords[i]);
}
return sqrt(squared);
}
double test(){
return 1.4;
}
};
int point::d;
int main() {
point p;
int d=2;
p.d=d;
p.coords[0]=1;
p.coords[1]=1;
point q;
q.d=d;
q.coords[0]=0;
q.coords[1]=2;
std::cout << "\ndistance:" << p.dist(q)<<"\n\n";
std::cout << "even this doesn't work!:" << p.test()<<"\n\n";
return 0;
}
Upvotes: 0
Views: 954
Reputation: 375
You code does nothing because the constructor of point
will be called before you assign any value to d
. So by accident, d
appears to have value of 0 (static variables are zero-initialized by default).
Here is one possibility to fix such code:
#include <iostream>
#include <cmath>
using namespace std;
struct point
{
constexpr static int d = 2;
double coords[d];
point(){ //point class
for (int i=0; i<d; i++){
coords[i]=0;
}
}
double dist(point &q){
double squared=0;
for (int i=0; i<d;i++){
squared+=(coords[i]-q.coords[i])*(coords[i]-q.coords[i]);
}
return sqrt(squared);
}
};
int main(){
point p;
p.coords[0]=1;
p.coords[1]=1;
point q;
q.coords[0]=0;
q.coords[1]=2;
std::cout << "\ndistance:" << p.dist(q)<<"\n\n";
return 0;
}
Upvotes: 1
Reputation: 950
Couple of issues, some are crucial and some are conventional.
const
and const
your methods, so that you could use these methods from a const object.d
, you should write point::d = 2
and not int d = 2
d
are dimensions, and can not be negative, thus std::size_t
or unsigned int
point::d
, and thus you should set point::d
before creating any instance of p.I attached your code with my fixes applied
struct point
{
static std::size_t d;
double *coords;
point() { //point class
coords = new double[d];
for (int i = 0; i < d; i++) {
coords[i] = 0;
}
}
double dist(const point &q) const {
double squared = 0;
for (int i = 0; i < d; i++) {
squared += (coords[i] - q.coords[i])*(coords[i] - q.coords[i]);
}
return sqrt(squared);
}
double test() const {
return 1.4;
}
point(const point& p) {
coords = new double[d];
for (std::size_t i = 0; i < d; ++i) {
coords[i] = p.coords[i];
}
}
point& operator=(const point& p) {
if (this == &p) {
return *this;
}
// coords are already allocated
for (std::size_t i = 0; i < d; ++i) {
coords[i] = p.coords[i];
}
}
~point() {
delete[] coords;
}
};
std::size_t point::d;
int main() {
point::d = 2;
point p;
p.coords[0] = 1;
p.coords[1] = 1;
point q;
q.coords[0] = 0;
q.coords[1] = 2;
std::cout << "\ndistance:" << p.dist(q) << "\n\n";
std::cout << "even this doesn't work!:" << p.test() << "\n\n";
return 0;
}
Upvotes: 4
Reputation: 87
The problem is that you setting the member variable d after the constructor runs, so your double array always be an array of 0. You should provide d in your constructor parameters.
Upvotes: 0