Code not outputting, looping input statements - c++

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;

Related

Why LAPACKE_dsygvd returns error after changing size of matrix?

I am trying to solve the generalized eigenvalue problem for the hydrogen atom by using LAPACKE_dsygvd. For the parameters of the generator functions, I use an interval that starts at 0.01 and takes N steps of 0.01. What I change is the value of N. Everythings fine for N = 14 and below, where I get the answers from the analytical solution. However, when I choose N = 15 and above, I get an error and info is returned with a value > N. After reading the documentation from LAPACK, it says the following:
N: if INFO = N + i, for 1 <= i <= N, then the leading
minor of order i of B is not positive definite.
The factorization of B could not be completed and
no eigenvalues or eigenvectors were computed.
But I have checked my matrix B and it is positive definite. I don't know what is wrong.
Below I show my scripts
#include <cmath>
#include <cstdio>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "library.h"
#include "mkl.h"
using namespace std;
double Superposition(const double ai, const double aj, const int m);
double Hamiltonian(const double ai, const double aj, const int m);
void print_matrix(double *A, int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%.7f ", A[i*n + j]);
}
cout << "\n";
}
}
void print_vector(double *vec, int n) {
for (int i = 0; i < n; i++) {
cout << vec[i] << " ";
}
cout << "\n";
}
double* interval(double min, double step) {
double *result;
result = (double *)mkl_malloc( N*sizeof( double ), 64 );
for (int i = 0; i < N; i++) {
result[i] = min + i*step;
}
return result;
}
int main() {
cout << Ry << "\n";
double *S, *H, *I, *eigenvalues;
double alpha, beta;
int i, j, info;
char* uplo = "U"; char* jobz = "V";
I = interval(0.01, 0.01);
alpha = 1.0; beta = 0.0;
S = (double *)mkl_malloc( N*N*sizeof( double ), 64 );
H = (double *)mkl_malloc( N*N*sizeof( double ), 64 );
eigenvalues = (double *)mkl_malloc( N*sizeof( double ), 64 );
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
int index = i*N + j;
if (j < i) {
S[index] = 0.0;
H[index] = 0.0;
}
else {
S[index] = Superposition(I[i], I[j], m);
H[index] = Hamiltonian(I[i], I[j], m);
}
}
}
print_matrix(S, N); cout << "\n";
print_matrix(H, N); cout << "\n" << "\n";
info = LAPACKE_dsygv(LAPACK_ROW_MAJOR, 1, *jobz, *uplo, N,
H, N, S, N, eigenvalues);
//print_matrix(H, N); cout << "\n";
//for (i = 0; i < N; i++) {
// eigenvalues[i] /= Ry;
//}
cout << info << "\n" << "\n";
print_matrix(H, N); cout << "\n";
print_vector(eigenvalues, N);
mkl_free(S);
mkl_free(H);
mkl_free(I);
mkl_free(eigenvalues);
}
*Edit: I used dsygvd as included in MKL, and the same error doesn't occur. However, I get very different results for both functions using the same inputs.

My Bubblesort won't sort anything, and won't let me pass by reference

For my CS class we are making a program that reads in numbers, calculates the inflation rate from those 2 numbers, sends those numbers to an array, bubblesorts the array, and then prints that array out after it is sorted. However, I can't get my bubblesort to sort, either there is a compiler error when I use &, or there it just doesnt sort. Can anyone help? Thank you!
#include
using namespace std;
double InflationRate(float old_cpi, float new_cpi);
double inflateRate;
void getCPIValues(float &old_cpi, float &new_cpi);
float swap_values(float&i, float&j);
void bubbleSort(float a[], int number_used);
float old_cpi=-1;
float new_cpi=-1;
const int MAX_RATES=20;
float a[MAX_RATES]={};
int main() //C++ programs start by executing the function main
{
char Continue = 'y';
double total = 0;
int i=0;
do{
getCPIValues(old_cpi, new_cpi);
InflationRate(old_cpi, new_cpi);
cout<< "Inflation rate is "<<inflateRate<<endl;
total = total+inflateRate;
cout << "Try again? (y or Y): ";
i++;
a[i-1]= inflateRate;
cin >> Continue;
}while ((Continue == 'y')||(Continue == 'Y'));
cout<<"Average rate is "<<total/i<<endl;
int number_used= i-1;
for (int p; p<=number_used; p++)
{
cout<<a[p]<<endl;
}
return 0;
}
double InflationRate(float old_cpi, float new_cpi)
{
inflateRate = (new_cpi - old_cpi)/old_cpi*100;
return(inflateRate);
}
void getCPIValues(float &old_cpi, float &new_cpi)
{
cout<<"Enter the old and new consumer price indices: ";
cin>>old_cpi>>new_cpi;
if ((old_cpi<=0)||(new_cpi<=0))
{
do
{
cout<<"Error: CPI values must be greater than 0";
cin>>old_cpi>>new_cpi;
}while ((old_cpi<=0)||(new_cpi<=0));
}
}
float swap_values(float&i, float&j)
{
int temp = i;
i=j;
j=temp;
return(i, j);
}
void bubbleSort(float a[], int number_used)
{
for (int m = 0; m < number_used-1; m++)
for (int n = 0; n < number_used-m-1; n++)
if (a[n] > a[n+1])
swap_values(&a[n], &a[n+1]);
}

Bernstein Polynomial Interpolation

For my assignment I have to implement an algorithm for the Bernstein polynomial defined on the interval [0,1].
My experiment needs to include the following function: f(x) = |ax|, for sake of simplicity in my code I let a = 1. When implementing the algorithm I used this Matlab code from our book (first picture) and the way the professors defines in his notes (second picture):matlab code professors defines in his notes
My output has all the same numbers, I think the problem is I am not sure what value k needs to be from the formula. Anyway here is my code:
#include <iostream>
#include <math.h>
using namespace std;
int combinations(int n, int k);
int fact(int n);
// Bernstein polynomial single precision
float Bernstein(float x[], float f[], int k, int n);
// Bernstein polynomial double precision
//float Bernstein(double x[], double k, int n);
//-------------------------------------------------------------
int main() {
int n,k;
cout << "Enter the degree: " << endl;
cin >> n;
cout << "Enter the k parameter: " << endl;
cin >> k;
float x[n];
double x_0[n];
// stepsize for interval [0,1]
double h = (1.0 - 0)/10.0;
for(int i = 0; i < n; i++){
x[i] = 0 + i*h;
x_0[i] = 0 + i*h;
}
// Compute the f(x_i) = y_i values single precision
float f_0[n], f_1[n], f_2[n];
float alpha = 1.0;
for(int i = 0; i < n; i++){
f_0[i] = fabs(alpha*x[i]);
f_1[i] = fabs(alpha*x[i]) + x[i]/2 - pow(x[i],2);
f_2[i] = 1/(1 + alpha*pow(x[i],2));
}
// Compute the f(x_i) = y_i values double precision
double alpha1 = 1.0;
double f0[n], f1[n], f2[n];
for(int i = 0; i < n; i++){
f0[i] = fabs(alpha1*x_0[i]);
f1[i] = fabs(alpha1*x_0[i]) + x_0[i]/2 - pow(x_0[i],2);
f2[i] = 1/(1 + alpha1*pow(x_0[i],2));
}
for(int i = 0; i < n; i++){
cout << Bernstein(x,f_0,k,n) << endl;
}
return 0;
}
//----------------------------------------------------------
float Bernstein(float x[], float f[], int k, int n){
int C;
float B;
if(k == 0){
C = 1;
}else{
C = combinations(n,k);
}
int t = n - k;
for(int i = 0; i < n; i++){
B = C*pow(x[i],k)*pow((1 - x[i]),t)*f[i];
}
return B;
}
int combinations(int n, int k){
return fact(n) / (fact(k) * fact(n - k));
}
int fact(int n){
int result = 1;
for (int i =1; i <= n; i++){
result = result*i;
}
return result;
}
Question:
Why does my output have all the same numbers, and what could be the problem?
bernstein(f,n,t) with a function handle f returns the nth-order Bernstein polynomial symsum(nchoosek(n,k)*t^k*(1-t)^(n-k)*f(k/n),k,0,n), evaluated at the point t. This polynomial approximates the function f over the interval [0,1].
Example:
bernstein(g,n,t) with a symbolic expression or function g returns the nth-order Bernstein polynomial, evaluated at the point t. This syntax regards g as a univariate function of the variable determined by symvar(g,1).
If any argument is symbolic, bernstein converts all arguments except a function handle to symbolic, and converts a function handle's results to symbolic.
I execute the program and how you say the output have the sames results. Did you make some changes at this program after your post? I am interesting on it, because i have a homework at home about bernstein polynomial.

Get a double to store a value C++

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 = &in;
}
system("pause");
return 0;
}
You want if (inV == NULL) rather than if (*inV == NULL).

how to get the last value of array after the if statement have been satisfied

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
}