In this program you input coordinates of points and their velocity components. It should give in output the point that is closest to (0,0,0), and its distance from (0,0,0). This is the main body of the program:
#include "tacka4.h"
#include "vektor1.h"
#include "brzina.h"
#include <iostream>
using namespace std;
int main () {
cout << "Broj tacaka? "; int n; cin >> n;
Tacka** tacke = new Tacka* [n];
for (int i=0; i<n; i++) {
cout << "Koordinate tjemena " << i << "? ";
double x, y, z; cin >> x >> y >> z;
cout << "Komponente brzine? ";
double vx, vy, vz; cin >> vx >> vy >> vz;
tacke[i] = new Tacka (Vektor (x, y, z), Brzina (vx, vy, vz));
}
cout << "\nBroj koraka? "; int k; cin >> k;
cout << "Trajanje koraka? "; double dt; cin >> dt;
const Tacka org;
cout << "ORG " << org << "\n\n";
for (int i=0; i<k; i++) {
for (int j=0; j<n; tacke[j++]->proteklo(dt));
double min = rastojanje (org, *tacke[0]); int m=0;
for (int j=1; j<n; j++) {
double d = rastojanje (org, *tacke[j]);
if (d<min) { min = d; m = j; }
}
cout << i << ' ' << *tacke[m] << ' ' << min << endl;
}
return 0;
}
I actually get something like this, for example:
T3102720.3112960.3112960.310528 0.519615
I need this result in the form:
T3 (0.3, 0.3, 0.3) 0.519615
This is the class tacka4.h
#ifndef _tacka4_h_
#define _tacka4_h_
#include "pokretan.h"
#include "vektor1.h"
#include "brzina.h"
class Tacka: public Pokretan {
static int ukId;
const int id;
Vektor r;
Brzina v;
public:
Tacka (const Vektor& rr=Vektor (),
const Brzina& vv=Brzina () )
: r (rr), v (vv), id (++ukId) {}
Tacka (const Tacka& T)
: r (T.r), v (T.v), id(++ukId) {}
Tacka& operator= (const Tacka& T)
{ r = T.r; v = T.v; return *this; }
Pokretan& proteklo (double dt)
{ r = r + v * dt; return *this; }
Vektor R () const {return r; }
friend double rastojanje (const Tacka& T1, const Tacka& T2)
{return + (T1.r + T2.r * -1);}
friend ostream& operator<< (ostream& d, const Tacka& T)
{return d << 'T' << T.id << T.r; }
};
#endif
Related
I am trying this code about Kohonen Network:
// Fausett.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
double euclidean(double *vec1,double *vec2,int n)
{
double dist = 0.0;
for(int i=0;i<n;i++) dist += (vec1[i]-vec2[i])*(vec1[i]-vec2[i]);
return dist;
}
double distance(int i,int jstar)
{
return double(i!=jstar);
// returns 1.0 if i!=jstar, returns 0.0 if i==jstar
}
double h(double d) { return 1.0-d; }
void train(double **W,int n,int cols,double *vec,double rate)
{
int i,j;
int win = 0;
double windist = euclidean(W[0],vec,n),edist;
for(i=0;i<cols;i++){
if((edist=euclidean(W[i],vec,n)) < windist)
{ win = i; windist = edist;}
for(i=0;i<cols;i++)
for(j=0;j<n;j++)
W[i][j] += (rate*h(distance(i,win)))*(vec[j]-W[i][j]);
}
int main(void)
{
int i, j;
int T = 10000; // number of iterations
double eta = 0.6; // learning rate
const int m = 4;
int cols;
// training vectors
double x0[m] = { 1.0,1.0,0.0,0.0 };
double x1[m] = { 0.0,0.0,0.0,1.0 };
double x2[m] = { 1.0,0.0,0.0,0.0 };
double x3[m] = { 0.0,0.0,1.0,1.0 };
cout << "Enter number of columns for weight matrix: ";
cin >> cols;
double** W = NULL; W = new double*[cols];
for(i=0;i<cols;i++) W[i] = new double[m];
srand(time(NULL));
for(i=0;i<cols;i++)
for(j=0;j<m;j++) W[i][j] = rand()/double(RAND_MAX);
for(i=0;i<T;i++)
{
train(W,m,cols,x0,eta); train(W,m,cols,x1,eta);
train(W,m,cols,x2,eta); train(W,m,cols,x3,eta);
eta /= 1.05; // learning rate decreased
}
for(i=0;i<cols;i++)
{
cout << "W[" << i << "]= [";
for(j=0;j<m;j++) cout << W[i][j] << " ";
cout << "]" << endl;
}
for(i=0;i<cols;i++) delete[] W[i];
delete[] W;
return 0;
}
its from book Willi - Hans Steeb, I copy paste that, but it gives me error when compiled. error: 'int' is not a class, struct, or union type typedef typename _Iterator::iterator_category iterator_category, this is one of the error messages and I have no idea at all why this happen. Can someone please explain it? I am a newbie in C++.
I know there are so many libraries in this code. That's because I was trying everything and also testing some other things. The problem is that function called "wspak" is not outputted well in my main function. I mean, for example, it outputs the first or last index 10 times instead of outputting all 10 indexes once. I hope you all will understand this code, because I use polish appellations for my variables. This is my code so far:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include <time.h>
#include <stdio.h>
#include <conio.h>
#include <random>
#include <stdlib.h>
#include <algorithm>
using namespace std;
float maxitab(int n, float tab[]); //deklaracja funkcji
float minitab(int n, float tab[]); //deklaracja funkcji
float wspak(int n, float tab[]); //deklaracja funkcji
double rand_double()
{
return ((double)rand()) / ((double)RAND_MAX);
}
double rand_double_interval(double a, double b)
{
return rand_double() * (b - a) + a;
}
int main(int argc, char* argv[])
{
srand(time(NULL));
int n;
float* tab;
cout << "Program wyszuka najwiekszy element tablicy.\n";
cout << "Podaj ilosc elementow tablicy: ";
cin >> n;
tab = new float[n];
for (int i = 0; i < n; i++)
{
float wylosowane_liczby = rand_double_interval(-1, 1);
tab[i] = wylosowane_liczby;
}
cout << "WYLOSOWANE z przedzialu <-1,1> elementy tablicy to: " << endl;
for (int i = 0; i < n; i++)
{
cout << tab[i] << endl;
}
cout << "\nNajwiekszy WYLOSOWANY z przedzialu <-1,1> element tablicy to: " << maxitab(n, tab);
cout << "\nNajmniejszy WYLOSOWANY z przedzialu <-1,1> element tablicy to: " << minitab(n, tab);
cout << "\nTablica od konca do poczatku: " << endl;
for (int i = 0; i < n; i++)
{
cout << wspak(n, tab);
}
delete[] tab;
cout << endl;
cout << endl;
cout << endl;
return 0;
}
float maxitab(int n, float tab[]) //definicja funkcji
{
float maximum = tab[0];
for (int i = 1; i < n; i++)
{
if (tab[i] > maximum)
{
maximum = tab[i];
}
}
return maximum;
}
float minitab(int n, float tab[]) //definicja funkcji
{
float minimum = tab[0];
for (int i = 1; i < n; i++)
{
if (tab[i] <= minimum)
{
minimum = tab[i];
}
}
return minimum;
}
float wspak(int n, float tab[]) //definicja funkcji
{
float wspak = 0;
for (int i = n - 1; i >= 0; i--)
{
if (tab[i] < wspak)
{
wspak = tab[i];
}
}
return wspak;
}
float wspak(int n, float tab[]); //deklaracja funkcji
wspak(n, tab);
void wspak(int n, float tab[]) //definicja funkcji
{
for (int i = n - 1; i >= 0; i--)
{
cout << tab[i] << ' ' << endl;
}
cout << '\n';
}
I am getting a segmentation fault error when compiling my code. I don't know how to fix it. I am supposed to compile my Polynomial.cpp with my professor's poly_check.o, but I get a segmentation fault error.
This is my Polynomial.h:
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <iostream>
using namespace std;
class Polynomial {
private:
double *coefficients;
int size;
public:
Polynomial();
Polynomial(double c[], int size);
Polynomial(const Polynomial &poly);
~Polynomial();
void set(double c[], int size);
inline int getDegree() const {
return size - 1;
}
double f(double x)const;
bool operator== (const Polynomial &poly)const;
Polynomial operator+ (const Polynomial &poly)const;
friend ostream & operator << (ostream &out, const
Polynomial &poly);
friend istream & operator >> (istream &in, Polynomial
&poly);
};
#endif
This is my Polynomial.cpp:
#include "Polynomial.h"
#include <iostream>
#include <cmath>
using namespace std;
Polynomial::Polynomial() {
size = 0;
coefficients = NULL;
}
Polynomial::Polynomial(double c[], int size) {
this->size = size;
coefficients = new double[size];
for (int i = 0; i < size; i++) {
coefficients[i] = c[i];
}
}
Polynomial::Polynomial(const Polynomial &poly) {
if (coefficients != NULL)
delete coefficients;
this->size = poly.size;
coefficients = new double[size];
for (int i = 0; i < size; i++)
coefficients[i] = poly.coefficients[i];
}
Polynomial::~Polynomial() {
if (coefficients != NULL)
delete coefficients;
}
void Polynomial::set(double c[], int size) {
this->size = size;
if (coefficients != NULL)
delete coefficients;
coefficients = new double[size];
for (int i = 0; i < size; i++)
coefficients[i] = c[i];
}
double Polynomial::f(double x)const {
double value = 0.0;
for (int i = 0; i < size; i++) {
value += (coefficients[i] * pow(x, i));
}
return value;
}
bool Polynomial::operator== (const Polynomial &poly)const {
if (this->size != poly.size)
return false;
for (int i = 0; i < size; i++) {
if (poly.coefficients[i] != coefficients[i])
return false;
}
return true;
}
Polynomial Polynomial::operator+ (const Polynomial &poly)const {
int maxSize = size;
if (poly.size > maxSize)
maxSize = poly.size;
double sum[maxSize] = {0.0};
for (int i = 0; i < size; i++) {
sum[i] = coefficients[i];
}
for (int i = 0; i < poly.size; i++) {
sum[i] += poly.coefficients[i];
}
Polynomial sumP(sum, maxSize);
return sumP;
}
ostream &operator << (ostream &out, const Polynomial &poly) {
for (int i = poly.size - 1; i >= 0; i--) {
if (i != poly.size - 1) {
if (poly.coefficients[i] >= 0)
out << " + ";
else
out << " - ";
}
out << poly.coefficients[i];
if (i == 0)
continue;
if (i == 1)
out << "x";
else
out << "x^" << i;
}
return out;
}
istream &operator >> (istream &in, Polynomial &poly) {
int degree;
in >> degree;
double c[100];
int size = 0;
while (in >> c[size]) {
size++;
if ((size-1) == degree)
break;
}
poly.set(c, size);
return in;
}
This is my poly_test.cpp:
#include "Polynomial.h"
#include <iostream>
using namespace std;
int main(void) {
double c1[] = {0, 1, 2, 3, 4};
double c2[] = {0, 1, 2, 3, 4, 5, 6};
Polynomial p1(c1, 5);
Polynomial p2(c2, 7);
Polynomial p3;
cout << "Enter p3: ";
cin >> p3;
cout << "p1: ";
cout << p1 << endl;
cout << "p2: ";
cout << p2 << endl;
cout << "p3: ";
cout << p3 << endl;
Polynomial p4;
p4 = p1 + p2;
cout << "p4 = p1 + p2, p4: ";
cout << p4 << endl;
double value = p1.f(2);
cout << "Evaluating p1 at x = 2, p1 = ";
cout << value << endl;
Polynomial p6(c1, 5);
cout << "p6: ";
cout << p6 << endl;
if (p6 == p1) {
cout << "p6 and p1 are equal. Equality test passed" <<
endl;
}
else {
cout << "Equality test failed" << endl;
}
return 0;
}
This is the error that I am getting:
segmentation fault error
In general, you should test your own code as you develop it. Don't write this much code and then plug it into a test function; it will fail, and the process of debugging it will be long and discouraging.
The specific problem (or one of them) is that you neglected to implement operator=. Are you familiar with shallow copies and deep copies? The default copy constructor is a shallow copier, so that two instances of Polynomial wind up with pointers to the same array. Then when they die, they both try to delete it.
I am trying to run the following code on c++ but keep geting error. Can anyone solve it for me please.
the error message from c++ says:
Error 6 error C2679: binary '<<' : no operator found which takes a
right-hand operand of type 'MVector'
#include <iostream>
#include <cmath>
#ifndef MVECTOR_H // the 'include guard'
#define MVECTOR_H // see C++ Primer Sec. 2.9.2
#include <vector>
#include <string>
#include <fstream>
class MVector
{
public:
// constructors
MVector() {}
explicit MVector(int n) : v(n) {}
MVector(int n, double x) : v(n, x) {}
void push_back(double x)
{
v.push_back(x);
}
double AV()
{
double sum = 0.0, average = 0.0;
for (double i = 0; i<v.size(); i++)
{
sum += v[i];
}
average = sum / v.size();
return average;
}
MVector RunningAverage(int m, int p)
{
MVector movingaverage(v.size() - m - p - 1);
for (int i = m; i < v.size() - p; i++)
{
double sum = 0.0;
if ((i + p < v.size()))
{
for (int j = i - m; j <= i + p; j++)
{
sum += v[j];
movingaverage[i - m] = sum / (m + p + 1);
}
}
}
return movingaverage; // Edit by jpo38
}
// access element (lvalue)
double &operator[](int index) { return v[index]; }
// access element (rvalue)
double operator[](int index) const { return v[index]; }
int size() const { return v.size(); } // number of elements
private:
std::vector<double> v;
};
#endif
int main()
{
using namespace std;
// create an MVector
MVector x;
// add elements to the vector
x.push_back(1.3);
x.push_back(3.5);
x.push_back(3.0);
x.push_back(2.0);
// x now contains 1.3 and 3.5
// print x
std::cout << " x:= ( ";
for (int i = 0; i < x.size(); i++)
std::cout << x[i] << " ";
std::cout << ")\n";
std::cout << x.RunningAverage(0, 1.0) << endl;
return 0;
}
x.RunningAverage(0, 1.0) returns a MVector that cannot be sent to std::cout, unless you declare the operator<< taking a MVector as parameter.
Alternatively, you can replace:
std::cout << x.RunningAverage(0, 1.0) << endl;
By:
MVector av = x.RunningAverage(0, 1.0);
for (int i = 0; i < av.size(); i++)
std::cout << av[i] << " ";
Or, declare the operator:
std::ostream& operator<<(std::ostream& out,const MVector& b)
{
for (int i = 0; i < b.size(); i++)
out << b[i] << " ";
return out;
}
And then std::cout << x.RunningAverage(0, 1.0) << std::endl; will work.
And you can then also replace, in your main function:
for (int i = 0; i < x.size(); i++)
std::cout << x[i] << " ";
By:
std::cout << x;
Live demo: http://cpp.sh/7afyi
You haven't overloaded operator<< for MVector.
You can do that with:
std::ostream& operator<<(std::ostream& output, MVector const& vec)
{
// use "output << …;" to do printing
return output;
}
Just wanted to know what is wrong with this. Double in is suppose to be set to 50 if it is NULL. Then after it is set to 50 it gets subtracted with the user input of double bbet with the analyze(). But after it loops it self it still thinks double in is NULL. Can any one point me in the right direction to get in to remember the value it subtracted its self with.
#include <iostream>
#include <string>
using namespace std;
class Bet
{
public:
void set_stack(double n);
void set_bet(double n);
void analyze();
double get_stack();
double get_bet();
private:
double pCount;
double bet;
};
void Bet::analyze()
{
double p = pCount;
double b = bet;
double z = p - b;
pCount = z ;
}
void Bet::set_bet(double n)
{
double z = n;
bet = z;
}
double Bet::get_bet()
{
return bet;
}
void Bet::set_stack(double n)
{
double z = n;
pCount = z;
}
double Bet::get_stack()
{
double p = pCount;
double b = bet;
double z = p - b;
return z;
}
double bbet;
double in ;
double* inV;
int main()
{
bool con = true;
while(con){
double start = 50;
if(*inV == NULL){
in = start;}
Bet rr;
rr.set_stack(in);
cout << "Enter min 1 Bet: ";
cin >> bbet;
rr.set_bet(bbet);
double newStack = rr.get_stack();
cout << "Stack: " << newStack << endl;
cout << "Bet: " << bbet << endl;
inV = ∈
}
system("pause");
return 0;
}
You want if (inV == NULL) rather than if (*inV == NULL).