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).
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++.
In the following, you see my c++ code that it solves 1-d Schrödinger equation with Numerov's method. But I have two problems:
When I increase the size of the box, the wave function for the ground state will be zero, the problem is in Normalization and I do not know why.
It is too slow, I need to increase box sufficiently large, any idea about how to speed up would be great.
This is my code:
#include <iostream>
#include <math.h>
#include <fstream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;
#define pi 3.1415926535897932384626433
class ID_TISE{
public:
ID_TISE(int N_graid, int x_min, int x_max, double dx, double e_tr, double de, double e_up, double m, double h_bar, double c);
~ID_TISE();
//functions
double Potential(char, int);
double PotentialIon(char, int);
void Setk2(double *k2, double E, int N, char lr);
void Numerov(double *psi, double *k2, int N);
double diff_slope(double e_tr,int N_L, int N_R);;
string string_precision(int, int);
double x(char lr, int index);
void Numerov_Solver(double*, double*);
void id_tise();
int N_graid ;
double x_min;
double x_max;
double dx;
//set trial energy
double e_tr;
double de;
double e_up;
int N_E = 10000;
//constant
double m ;
double h_bar;
double c; // second point of the wavefunctions
//array
double *psi_l = NULL;
double *psi_r = NULL;
double *k2_l = NULL;
double *k2_r = NULL;
};
ID_TISE::ID_TISE(int N_graid, int x_min, int x_max, double dx, double e_tr, double de, double e_up, double m, double h_bar, double c){
this->N_graid = N_graid;
this->x_min = x_min;
this->x_max = x_max;
this->dx = dx;
this->e_tr =e_tr;
this->de = de;
this->e_up = e_up;
this->m = m;
this->h_bar=h_bar;
this->c=c;
psi_l = new double[N_graid];
psi_r = new double[N_graid];
k2_l = new double[N_graid];
k2_r = new double[N_graid];
}
ID_TISE::~ID_TISE(){
delete[] psi_l;
delete[] psi_r;
delete[] k2_l;
delete[] k2_r;
}
void ID_TISE::id_tise(){
ofstream fout;
int im = int(N_graid/2);
int N_R = N_graid - im + 2;
int N_L = im + 2;
//initialization left solution
psi_l[0] = 0.0;
psi_l[1] = c;
//initialization right solution
psi_r[0] = 0.0;
psi_r[1] = c;
int ii = 0, inode = 0, inodeUp = 1;
while(e_tr < e_up){
double ds1 = diff_slope(e_tr,N_L,N_R);
e_tr += de;
double ds2 = diff_slope(e_tr,N_L,N_R);
if(ds1*ds2 < 0.0){
if(e_tr < -9.0e-7 || e_tr > 9.0e-7){ // to prevent reaching to E = 0
cout <<"E_"<<ii++ <<" = "<< e_tr << endl;
Normalizer(psi_r,im);
Normalizer(psi_l,im);
fout.open("psi_"+string_precision(ii,0)+ ".dat");
if(e_tr < 0){
if(inode%2 != 0.){
for(int i = 0; i < im; i++)
fout << x('l',i) << "\t" << -psi_l[i] << endl;
}
else{
for(int i = 0; i < im; i++)
fout << x('l',i) << "\t" << psi_l[i] << endl;
}
for(int i = im-1; i > -1; i--)
fout << x('r',i) << "\t" << psi_r[i] << endl;
inode++;
}
else{
if(inodeUp%2 == 0.){
for(int i = 0; i < im; i++)
fout << x('l',i) << "\t" << -psi_l[i] << endl;
}
else{
for(int i = 0; i < im; i++)
fout << x('l',i) << "\t" << psi_l[i] << endl;
}
for(int i = im-1; i > -1; i--)
fout << x('r',i) << "\t" << psi_r[i] << endl;
inodeUp++;
}
fout.close();
}
}
else continue;
}
}
void ID_TISE::Normalizer(double *psi,int N){
double sum = 0.;
for(int i=0 ; i<N ; i++)
sum += psi[i]*psi[i]*dx;
sum=sqrt(sum);
for(int i=0 ; i<N ; i++){
psi[i]=psi[i]/sum;
}
}
void ID_TISE::Numerov(double *psi, double *k2 , int N){
double h12 = (dx*dx)/12.;
for(int i = 1; i < N; i++){
psi[i+1] = (2.*(1.-5.*h12*k2[i])*psi[i] - (1. + h12*k2[i-1])*psi[i-1])/(1. + h12*k2[i+1]);
}
}
void ID_TISE::Setk2(double *k2, double E, int N, char lr){
for(int i = 0; i < N; i++){
if(E > 0)
k2[i] = (2.*m/(h_bar*h_bar))*(E - PotentialIon(lr,i));
else
k2[i] = (2.*m/(h_bar*h_bar))*(E - Potential(lr,i));
}
}
double ID_TISE::Potential(char lr, int index){
return -1./sqrt(x(lr, index)*x(lr, index)+2.);
}
double ID_TISE::PotentialIon(char lr, int index){
return 0.;
}
double ID_TISE::x(char lr, int index){
if(lr == 'l')
return x_min + (index)*dx; // dx = h
else
return x_max - (index)*dx; // the right solution
}
double ID_TISE::diff_slope(double e_tr, int N_L, int N_R){
double dslope;
//initialization left solution
psi_l[0] = 0.0;
psi_l[1] = c;
//initialization right solution
psi_r[0] = 0.0;
psi_r[1] = c;
Setk2(k2_l, e_tr, N_L, 'l');
Setk2(k2_r, e_tr, N_R, 'r');
Numerov(psi_l, k2_l, N_L);
Numerov(psi_r, k2_r, N_R);
double y_m = (psi_l[N_L-1]+psi_r[N_R-1])/2.;
dslope = (2.*y_m - psi_l[N_L - 3] - psi_r[N_R - 3])/(dx*psi_r[N_R - 2]);
return dslope;
}
string ID_TISE::string_precision(int value, int n_digits){
if(n_digits < 6) n_digits = 6;
ostringstream out;
out << fixed<<setprecision(n_digits) << value;
return out.str();
}
int main(){
int N_graid = 500000;
double x_min = -500;
double x_max = -x_min;
double dx = (x_max - x_min)/(N_graid-1);
//set trial energy
double e_tr = -0.6;
double de = 0.000001;
double e_up = 1.0;
//constant
double m = 1.;
double h_bar = 1.;
double c = 0.00000001; // second point of the wavefunctions
ID_TISE &id_tise = *(new ID_TISE(N_graid,x_min,x_max,dx,e_tr,de,e_up,m,h_bar,c));
id_tise.id_tise();
}
Looking to convert x,y coordinates to polar. Code is looping asking for the initial inputs for the x and y. It never outputs what the polar coordinates are.
#include <iostream>
#include <math.h>
using namespace std;
int getrec(double x[], double y[]);
void polar(double x, double y, double& r, double& theta);
void showPolarCoord(double radius, double angle);
const int SIZE = 100;
const double toDegrees = 180.0/3.1415926;
int main()
{
double x[SIZE];
double y[SIZE];
double distance[SIZE];
double angle[SIZE];
double x_same[SIZE];
double y_same[SIZE];
int count = getrec(x,y);
for (int i=0; i < count; i++)
{
x_same[i] = x[i] + 6;
y_same[i] = y[i] + 2;
}
for(int i=0; i < count; i++)
{
polar (x_same[i], y_same[i], distance[i], angle[i]);
}
}
int getrec(double x[], double y[])
{ int count = 0;
do
{ cout << "Enter the x coordinate: ";
cin >> x[count];
cout << "Enter the y coordinate: ";
cin >> y[count];
count++;
}
while(count < SIZE && (x[count -1] != 0) && (y[count -1] != 0));
return count;
}
void polar(double x, double y, double& r, double& theta)
{
r = sqrt((pow(x,2))+(pow(y,2)));
theta = atan(y/x) * toDegrees;
return;
}
void showPolarCoord(double radius, double angle)
{
cout << "The polar coordinates are: " << showPolarCoord << endl;
return;
}
Issue one:
In your showPolarCoord(), your cout statement is printing the address of the function. This happens when you put the name of the function, which is eventually not what you want to print.
What you want is something like this (except to put the right equation for calculating polar angles out of an angle and a radius):
void showPolarCoord(double radius, double angle)
{
cout << "The polar coordinates are: " << radius * angle << endl;
}
Issue two:
You need to call the function showPolarCoord() in main() to actually use its functionality. But you did not.
Issue three:
This is a mess. In main(), what are you trying to achieve using these two statements?
while(count < SIZE && (x[count -1] != 0) && (y[count -1] != 0));
return count;
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
This is what I have so far. My problem is that the iteration function returns the first value of the array. I want it to return the last value after the if statement is satisfied.
This is solving equation using the False Position Method
# include <iostream>
using namespace std;
double iteration(double u, double l);
double f (double x);
inline bool closerlimit(double u, double l);
double e;
void main()
{
cout << "Enter the upper Limit: " <<endl;
double ul;
cin >> ul;
cout << "Enter The lower Limit: " <<endl;
double ll;
cin >> ll;
cout<<"enter level of error: "<<endl;
cin>>e;
double r;
r=iteration(ul,ll);
cout<<"root is : "<< r<<endl;
}
double f(double x)
{
return exp(x)+5*x;
}
// Evaluating the closer limit to the root
// to make sure that the closer limit is the
// one that moves and the other one is fixed
inline bool closerlimit(double u, double l)
{
return fabs(f(u)) > fabs(f(l));
}
This is my iteration function. It only returns the first value of the array. I want it so that after the if statement is satisfied, this function will return the latest value of the root array.
double iteration(double u, double l)
{
double root[100], re=0;
for (int i=0; i<=20; i++)
{
{
root[i] = u - ((f(u)*(l-u)) / (f(l)-f(u)));
if (closerlimit(u,l))
l = root[i];
else
u = root[i];
double re=0;
re=abs((root[i]-root[i-1])/root[i])*100;
if (re<=e) {break;}
}
cout<<"r = "<<root[i]<<endl;
cout<<"re = "<<re<<endl;
return (root[i]);
}
return 0;
}
You can always save what you want to return in a local variable.
double iteration(double u, double l)
{
double root[100], re=0;
double ret = 0.0; //added
int i; // you'll want to use i in cout
for (i=0; i<=20; i++)
{
{root[i] = u - ((f(u)*(l-u)) / (f(l)-f(u)));
if (closerlimit(u,l))
l = root[i];
else
u = root[i];
double re=0;
re=abs((root[i]-root[i-1])/root[i])*100;
if (re<=e) {
ret=root[i]; // save the value;
break;
}
}
cout<<"r = "<<root[i]<<endl; // well, when break is not met, this is uninitialized value
cout<<"re = "<<re<<endl;
return ret; // defaulted to 0
}