C++ double initialization - c++

#include <iostream>
#include <math.h>
using namespace std;
class circle
{
public:
circle();
circle(double radius);
double circlerad(void);
double area(void);
double circumference(void);
private:
double rad;
};
circle::circle()
{
double rad = 0;
}
circle::circle(double radius)
{
cout << radius << endl;
double rad = radius;
}
double circle::circlerad(void)
{
return rad;
}
double circle::area(void)
{
double res;
res = rad * rad * 3.14;
return res;
}
double circle::circumference(void)
{
return (double) rad * 2 * 3.14;
}
double radius(int x1, int y1, int x2, int y2)
{
double rad = 0;
double xm, ym;
xm = (double)(x1 + x2) / 2;
ym = (double)(y1 + y2) / 2;
rad = sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
return rad;
}
int main(void)
{
double rad;
int x1, y1;
int x2, y2;
cout << "x1 : ";
cin >> x1;
cout << "y1 : ";
cin >> y1;
cout << "x2 : ";
cin >> x2;
cout << "y2 : ";
cin >> y2;
*circle one((double)radius(x1, y1, x2, y2));*
cout << fixed << one.area() << endl;
//cout << fixed << one.circumference() << endl;
return 0;
}
this is my code to calculate circle area and circumference.
But the problem is that when I initialize circle one, regardless of radius(x1, y1, x2, y2) value,
it always initialize to -9.2559631349317831e+061.

You don't initialise circle::rad here (instead you use local variable rad):
circle::circle()
{
double rad = 0;
}
circle::circle(double radius)
{
cout << radius << endl;
double rad = radius;
}
you should do something like:
circle::circle(): rad(0)
{
}
circle::circle(double radius): rad(radius)
{
}

double rad declares a local variable, which is not the same as the class member rad. The simplest fix:
circle::circle()
{
rad = 0;
}
circle::circle(double radius)
{
cout << radius << endl;
rad = radius;
}

Related

error - f was not declared in this scope - secant method

I am currently working out irr using algorithms through secant method. I think I am doing it correctly so far? but my main problem is that it keeps saying f is not declared in this scope:
int fx1 =f (x1);
int fx2 =f (x2);.
when i try to declare f as a function it does not allow me to, can anyone help?
#include iostream
#include iomanip
#include cmath
using namespace std;
const int Max_Iter=1000;
const double e=0.001;
double internal_r(double c, double r, int n){
double internal_r = 0.0;
for (n=1; n<=Max_Iter; n++); {
internal_r -= c/pow(1.0+r, n);
}
return internal_r;
}
int Secant(double x1, double x2, double e, double &root) {
int fx1 =f (x1);
int fx2 =f (x2);
if (x1==x2|| f(x2)==f(x1)){
root=0; return 0;
}
double x = x1 - f(x1) - (x2-x1) / (f(x2)-f(x1));
if ( fabs(f(x)) < e){
root = x; return 1;
} else {
if (f(x1)*f(x)<0){
return Secant(x1,x,e,root);
} else {
return Secant (x,x2,e,root);
}
}
}
int main() {
int n = 1;
double x1 = 0.0, x2 = 0.0, x = 0.0, fx1 = 0.0, fx2 = 0.0;
cout << "Secant Method" << endl;
cout <<"Enter first initial approximation: ";
cin >>x1;
cout <<"Enter second initial approximation: ";
cin >>x2;
cout <<"Enter the iteration number: ";
cin >>n;
if (n>=0 && n<=Max_Iter) {
cout << "you have entered an iteration sucessfully\n";
x = x1 - (fx1 * (x2 - x1)) / (fx2 - fx1);
cout <<"\n The root of the equation is "<< x <<endl;
return 0;
}
}

How to run without vectors with c++

Is there a way to execute this code without using vectors?
Can this program be run without vectors in the polygon class?
If possible, how should I modify the code?
And is it right to write the copy constructor and the move constructor as it is now?
It's so hard to do C++ while playing Python. Help me.
Thank you.
Polygon.h
#pragma once
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
struct C2D {
double x, y;
};
class Polygon {
int point;
vector<C2D> arr;
public:
Polygon(int point_, C2D arr_[]) : arr(point_) {
point = point_;
memcpy(arr.data(), arr_, sizeof(C2D) * point);
};
Polygon(Polygon& p) : arr(p.point) {
point = p.point;
memcpy(arr.data(), p.arr.data(), sizeof(C2D) * point);
};
Polygon(Polygon&& p) {
point = p.point;
memcpy(arr.data(), p.arr.data(), sizeof(C2D) * point);
p.point = 0;
delete[]p.arr.data();
};
void print() const {
cout << "Polygon information" << endl;
for (int i = 0; i < point; i++) {
cout << i + 1<< "point" << " : " << arr[i].x << ", " << arr[i].y << endl;
}
cout << endl;
};
double area_result() {
double sum = 0;
for (int i = 1; i < point; i++) {
sum += ccw(arr[0].x, arr[i - 1].x, arr[i].x, arr[0].y, arr[i - 1].y, arr[i].y);
}
return fabs(sum);
}
static double ccw(double x1, double x2, double x3, double y1, double y2, double y3) {
double res = x1 * y2 + x2 * y3 + x3 * y1;
res += (-y1 * x2 - y2 * x3 - y3 * x1);
return res / 2;
}
};
main.cpp
int main() {
int point;
C2D* c2d;
cout << "point : ";
cin >> point;
cout << endl;
c2d = new C2D[point];
for (int i = 0; i < point; i++) {
cout << i + 1 << "x : ";
cin >> c2d[i].x;
cout << i + 1 << "y : ";
cin >> c2d[i].y;
cout << endl;
}
cout << endl;
Polygon p(point, c2d);
p.print();
cout << "Polygon area : " << p.area_result() << endl;
return 0;
}
Don't.
Vectors are one of those no-brainer improvements of C++ over C. Don't try to code around them, learn how to use them properly. You will see that many lines of code and many sources of headaches go away when you actually code C++.
Polygon.hpp
#ifndef POLYGON_HPP
#define POLYGON_HPP
#include <iostream>
#include <vector>
#include <cmath>
struct C2D
{
double x, y;
};
class Polygon
{
public:
Polygon( unsigned points_, std::vector<C2D> const & arr_ ) : points( points_ ), arr( arr_ ) {}
Polygon( Polygon & p ) : points( p.points ), arr( p.arr ) {}
Polygon( Polygon && p ) : points( p.points )
{
arr.swap( p.arr );
}
friend std::ostream & operator<<( std::ostream & out, Polygon const & p );
double area_result()
{
double sum = 0;
for ( unsigned i = 1; i < points; ++i )
{
sum += ccw( arr[0].x, arr[i - 1].x, arr[i].x, arr[0].y, arr[i - 1].y, arr[i].y );
}
return std::fabs( sum );
}
static double ccw( double x1, double x2, double x3, double y1, double y2, double y3 )
{
double res = x1 * y2 + x2 * y3 + x3 * y1;
res += ( -y1 * x2 - y2 * x3 - y3 * x1 );
return res / 2;
}
private:
unsigned points;
std::vector<C2D> arr;
};
#endif
main.cpp
#include "Polygon.hpp"
#include <vector>
#include <iostream>
std::ostream & operator<<( std::ostream & out, Polygon const & p )
{
out << "Polygon information" << std::endl;
for ( unsigned i = 0; i < p.points; ++i )
{
out << ( i + 1 ) << "point : " << p.arr[i].x << ", " << p.arr[i].y << std::endl;
}
out << std::endl;
return out;
}
int main()
{
int points;
std::vector<C2D> c2d;
std::cout << "points : ";
std::cin >> points;
c2d.reserve( points );
for (int i = 0; i < points; i++) {
C2D coord;
std::cout << i + 1 << "x : ";
std::cin >> coord.x;
std::cout << i + 1 << "y : ";
std::cin >> coord.y;
c2d.push_back( coord );
}
Polygon p( points, c2d );
std::cout << p << "Polygon area : " << p.area_result() << std::endl;
return 0;
}
Your code is using vector exactly like a C style array, so you can replace it with such an array with minimum changes.
In fact, it will make your code simpler to read, since you will not be misusing a C++ object like a C variable.
class Polygon {
int point;
C2D *arr;
public:
Polygon(int point_, C2D arr_[]) {
point = point_;
arr = new C2D[point];
//you should check here allocation is successful
memcpy(arr, arr_, sizeof(C2D) * point);
};
Polygon(Polygon& p) {
point = p.point;
arr = new C2D[point];
memcpy(arr, p.arr, sizeof(C2D) * point);
};
Polygon(Polygon&& p) {
point = p.point;
arr = p.arr;
p.point = 0;
p.arr = null;
};
//you will need to add a destructor to clean up memory
~Polygon() {
delete [] arr;
}
};
That said, as the comments suggest, this is a bad practice.
If you have some constraints, such as a limited C++ environment lacking a vector implementation, a need to interface your code with another language / runtime, or homework requirement, you should add them to your question for suggestions on better solution.

Initializing dynamic pointer to multidimensional array

I am new to programming and am trying to implement A star search algorithm on C++. I am having segmentation fault:11 because of not initializing my pointer. I have tried it several different ways to no avail.
I am still confused about the whole pointer and dynamic memory allocation concept.
Can anyone help me figure it out? Thank you.
#include <iostream>
#include <vector>
#include <fstream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
using namespace std;
// Definition of the heuristic. The heuristic in this problem is the distance between
// two coordinates
double heuristic(double x1, double y1, double x2, double y2) {
double dx, dy;
dx = x1 - x2;
dy = y1 - y2;
return sqrt(dx*dx - dy*dy);
//return sqrt(pow((x1 - x2), 2) + pow((y1 - y2), 2));
}
// ----- A Star Search Algorithm (f = g + h)----
double** a_star_search(double points[][2]) {
int count = 1;
double** points1 = NULL;
// points1[10][2];
double x1 = points[0][0];
double y1 = points[0][1];
points1[count - 1][0] = x1;
points1[count - 1][1] = y1;
while (count <= 10) {
double tempx1;
double tempy1;
double distance = 10000000;
for (int i = 0; i < 10; i++) {
if (points[i][0] != 0 && points[i][1] != 0) {
double distance2 = heuristic(x1, y1, points[i][0], points[i][1]);
if (distance2 < distance) {
tempx1 = points[i][0];
tempy1 = points[i][1];
distance = distance2;
}
}
}
x1 = tempx1;
y1 = tempy1;
count++;
points1[count - 1][0] = x1;
points1[count - 1][1] = y1;
}
return points1;
}
int main() {
double points[7][2];
int counter = 0;
ifstream infile("waypoints.txt");
int a, b;
while (infile >> a >> b)
{
points[counter][0] = a;
points[counter][1] = b;
counter++;
}
points[6][0] = points[0][0];
points[6][1] = points[0][1];
double** points1 = a_star_search(points);
cout << "Initial Sequence: ";
for (int i = 0;i < 7;i++) {
cout << "(" <<points[i][0] << " , " << points[i][1] << "), ";
}
cout << "\n\nOptimized Sequence: ";
for (int i = 0;i < 7;i++) {
cout << "(" << points1[i][0] << " , " << points1[i][1] << "), ";
}
cout << "\n\nTotal Distance after A* search: ";
double totaldistance = 0;
for (int i = 0;i < 6;i++) {
double dis = heuristic(points1[i][0], points1[i][1], points1[i + 1][0], points1[i + 1][1]);
cout << dis << "+";
totaldistance = totaldistance + dis;
}
cout<< "=" << totaldistance <<endl;
}
You are not allocating memory dynamically for double** points1 variable after setting it to NULL in your a_star_search function. As pointed out by #user4581301, use std::vector. This will simplify your code significantly and worth spending the time to learn STL containers.

how do i use the pointer variable to change the value

I am having a problem with changing the coordinate of the point to ( 7,4) using the pointer variable. I just did x = 7 and y = 4, but I don't think that is correct. Can someone help ?
What I need to do:
in main()
instantiate a Point object and initialize at the time of definition
define a pointer that points to the object defined above
using the pointer variable to
update the coordinates of the point to (7,4)
display the distance from the origin
#include <iostream>
#include <math.h>
using namespace std;
class Point
{
private:
int x, y;
public:
Point(int x_coordinate, int y_coordinate);
int getVal();
double distance(double x2, double y2);
};
// Initialize the data members
Point::Point(int x_coordinate, int y_coordinate)
{
x = x_coordinate;
y = y_coordinate;
}
// Get the values of the data members.
int Point::getVal()
{
return x,y;
}
// Calculates and returns the point's distance from the origin.
double Point::distance(double x2, double y2)
{
double d;
d = sqrt( ((x2 - 0)*(x2 - 0)) + ((y2 - 0) * (y2 - 0)) );
return d;
}
//Allows user input and changes the point to (7,4) and displays the distance from origin.
int main()
{
int x,y;
cout << "Enter x coordinate followed by the y coordinate: " << endl;
cin >> x >> y;
Point p(x,y);
Point *newPointer = &p;
double theDistance = p.distance(x,y);
cout << "The point's distance from the origin is: " << theDistance << endl;
system("PAUSE");
}
To update coordinates of point, you need a new function -
void Point::UpdateCoordinates(int x0, int y0)
{
x = x0;
y = y0;
}
For distance(), I think you only need below.
double Point::distance()
{
return sqrt( x*x + y*y );
}

Using a function's local variable in main

I tried searching for awnsers, but all the threads are different langs.
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;
void calcDistance (int x1, int y1, int x2, int y2);
int main()
{
int x1, y1, x2, y2;
cout << "Enter the points in coordinate pair form, ommiting parantheses" << endl;
cin >> x1 >> y1 >> x2 >> y2;
calcDistance (x1, y1, x2, y2);
system("pause");
// how do I cout the dist in main-- says dist isn't declared
}
void calcDistance (int x1, int y1, int x2, int y2)
{
int sideA;
sideA = x2 - x1;
int sideB;
sideB = y2 -y1;
int sideAsqd;
sideAsqd = sideA * sideA;
int sideBsqd;
sideBsqd = sideB * sideB;
int sideCsqd;
sideCsqd = sideAsqd + sideBsqd;
double dist;
dist = sqrt(sideCsqd);
cout << "The calculated distance is "<< dist << endl;
}
How do I make the second cout occur in main. I try just putting it in main, but then I get an error saying that dist is not declared in the scope.
I want to be able to use the dist value in main, while it has been calculated in the function.
Change your function:
double calcDistance (int x1, int y1, int x2, int y2)
{
int sideA = x2 - x1;
int sideB = y2 -y1;
int sideAsqd = sideA * sideA;
int sideBsqd = sideB * sideB;
int sideCsqd = sideAsqd + sideBsqd;
double dist = sqrt(sideCsqd);
return dist;
}
And in main do this:
double res = calcDistance (x1, y1, x2, y2);
cout << "The calculated distance is "<< res << endl;
Given a function, say
void calcDistance (int x1, int y1, int x2, int y2)
{
//...
double dist;
//...
}
The variable dist goes out of scope at the closing brace, so not only can you not refer to it from elsewhere, it won't exist when outside the function.
If you want the value somewhere else, return it:
double calcDistance (int x1, int y1, int x2, int y2)
{
//...
double dist;
//...
return dist;
}
To use it elsewhere just capture the return:
double distance = calcDistance(1,2,3,4);
Now you have another local variable called distance that you can use.