I got a little problem about this program i am making ,
The main purpose of this program I am making is to get 2 polynomial and sum / sub / multiply it ,
Didn't finished the output and main() parts yet, just on the code itself,
When I try to use the multiply part,
I get this error : stack around the variable 'a' is corrupted ,
Dunno what I did wrong .... took me 1 hours to think of this way and write this , but just 2 hours trying to fix this but can't get anywhere.
class PolyNomial
{
int printcounter;
double *coefficients;
int degree;
public:
PolyNomial()//the default constructor to initialize a polynomial equal to 0
{
degree = 0;
coefficients = new double[degree + 1];
coefficients[0] = 0;
}
PolyNomial(double y[], int x)//the constructor to initialize a polynomial with the given coefficient array and degree
{
degree = x;
if (degree>10)
{
cout<<"Error : The PolyNomial's Degree is bigger than 10 and can not be shown in this program";
}
else
{
int c=0;
coefficients = new double[degree + 1];
for(c=0;c<=degree;c++)
{
coefficients[c] = y[c];
}
}
}
void add(PolyNomial p)
{
int i;
if ( degree < p.degree )
{
i = p.degree;
printcounter = p.degree;
}
else
{
i = degree;
printcounter = degree;
}
for(;i>=0;i--)
{
coefficients[i] = coefficients[i] + p.coefficients[i];
}
}
void sub(PolyNomial p)
{
int i;
if ( degree < p.degree )
{
i = p.degree;
printcounter = p.degree;
}
else
{
i = degree;
printcounter = degree;
}
for(;i>=0;i--)
{
coefficients[i] = coefficients[i] - p.coefficients[i];
}
}
void print()
{
int i;
for(i=0;i<=printcounter;i++)
cout<<coefficients[i]<<" ";
}
void Multiply(PolyNomial p)
{
int i,j;
i = degree;
j = p.degree;
double a[100];
int counter;
for ( counter = 0 ; counter <= i+j; counter++)
{
a[counter]=0;
}
int x= i+ j ;
for(;i>=0;i--)
{
for(;j>=0;j--)
if (i+j>=10)
{
cout<<"Error : The PolyNomial's Degree is bigger than 10 and can not be shown in this program";
break;
}
a[i+j] = a[i+j] + ( coefficients[i] * p.coefficients[j] );
}
PolyNomial k(coefficients,x);
k.print();
}
};
int main()
{
double a[100];
int x,i;
cout<<"Enter the PolyNomial's Degree : ";
cin>>x;
cout<<"\nEnter the coefficients one by one by , from bigger to smaller : ";
for ( i=0 ; i<=x ; i++ )
{
cin>>a[i];
}
PolyNomial p(a,x);
cout<<"\n\nEnter the PolyNomial's Degree : ";
cin>>x;
cout<<"\nEnter the coefficients one by one by , from lowest degree to highest : ";
for ( i=0 ; i<=x ; i++ )
{
cin>>a[i];
}
PolyNomial o(a,x);
_getch();
// p.add(o);
p.Multiply(o);
// p.print();
_getch();
// p.sub(o);
// p.Multiply(o);
}
This line
a[i+j] = a[i+j] + ( coefficients[i] * p.coefficients[j] );
is not inside the for j loop (you haven't used { and } to enclose it}. This means that j is -1 when it gets run. If i is 0 you are assigning to a[-1] which causes the stack corruption message.
Note that you also aren't resetting j each time round the i loop, so the inner loop will only execute once.
In C and C++ local variables are generally stored on the stack, a small(ish) area of memory that programs use to store local/function level data; when an executable calls a function, the processor uses the stack to store the address that the call is being made from, and then branches to the subroutine. When the subroutine finishes, if it has cleaned up it's stack, then it should be back to a state where the top value of the stack is the address it needs to jump back to in order to resume processing.
At the same time, C/C++ programs use the stack to store local variables -- the principle being that when you leave a sub-routine all the variables go away very elegantly.
Your variables "a" are local variables, they are on the stack. Since they are arrays, that means that if you write outside the contraints of the array, you will overwrite areas of the stack being used by other variables or possibly even by the CPU for return-address tracking.
Your variables "a" have 100 elements. This array forms a single, contiguous block:
{a[0]}{a[1]}...{a[99]}
If you write to 'a[100]' you are writing past the end of the array and over some other variable's memory, or possibly the return address of a function.
You might want to consider using a [std::array][1] or a [std::vector][2].
At the time you create your polynomial, you create a coefficients array with the size of degree + 1. So far so good. Now if you add two polynomials, you simply add the coefficients but disregard the allocated space. If the polynomial with the lowest degree should be the result of the addition, you'll get in trouble
Well for now tnx to all the answers and comments i got my code working but it dose not do the job correctly,
Example : if I add a 2 degree polynomial first then a 3 degree like this :
1 2 3
1 2 3 4
The result will be :
0 0 3 6 9 12
Which is only one part of the multiply processes,
The 2 polynomial I added are :
3x^2 + 2x^1 + 1
4x^3 + 3x^2 + 2x^1 + 1
Here is the new code :
class PolyNomial
{
int printcounter;
double *coefficients;
int degree;
public:
PolyNomial()//the default constructor to initialize a polynomial equal to 0
{
degree = 0;
coefficients = new double[degree + 1];
coefficients[0] = 0;
}
PolyNomial(double y[], int x)//the constructor to initialize a polynomial with the given coefficient array and degree
{
degree = x;
if (degree>10)
{
cout<<"Error : The PolyNomial's Degree is bigger than 10 and can not be shown in this program";
}
else
{
int c=0;
coefficients = new double[degree + 1];
for(c=0;c<=degree;c++)
{
coefficients[c] = y[c];
}
}
}
void add(PolyNomial p)
{
int i;
if ( degree < p.degree )
{
i = p.degree;
printcounter = p.degree;
}
else
{
i = degree;
printcounter = degree;
}
for(;i>=0;i--)
{
coefficients[i] = coefficients[i] + p.coefficients[i];
}
}
void sub(PolyNomial p)
{
int i;
if ( degree < p.degree )
{
i = p.degree;
printcounter = p.degree;
}
else
{
i = degree;
printcounter = degree;
}
for(;i>=0;i--)
{
coefficients[i] = coefficients[i] - p.coefficients[i];
}
}
void print()
{
int i;
for(i=0;i<=printcounter;i++)
cout<<coefficients[i]<<" ";
}
void Multiply(PolyNomial p)
{
int i,j;
i = degree;
j = p.degree;
double mult[100];
int counter;
for ( counter = 0 ; counter <= i+j; counter++)
{
mult[counter]=0;
}
int x= i+ j ;
for(;i>=0;i--)
{
for(;j>=0;j--)
{
if (i+j>=10)
{
cout<<"Error : The PolyNomial's Degree is bigger than 10 and can not be shown in this program";
break;
}
mult[i+j] = mult[i+j] + ( coefficients[i] * p.coefficients[j] );
}
}
PolyNomial k(mult,x);
k.printcounter = x;
k.print();
}
};
int main()
{
double a[100];
int x,i;
cout<<"Enter the PolyNomial's Degree : ";
cin>>x;
cout<<"\nEnter the coefficients one by one by , from lowesr degree to highest : ";
for ( i=0 ; i<=x ; i++ )
{
cin>>a[i];
}
PolyNomial p(a,x);
cout<<"\n\nEnter the PolyNomial's Degree : ";
cin>>x;
cout<<"\nEnter the coefficients one by one by , from lowest degree to highest : ";
for ( i=0 ; i<=x ; i++ )
{
cin>>a[i];
}
PolyNomial o(a,x);
_getch();
// p.add(o);
p.Multiply(o);
// p.print();
_getch();
// p.sub(o);
// p.Multiply(o);
}
Related
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]);
}
I have to make a code where the user inputs altitude readings and the code is supposed to output total climb, total descent, and net change. This is what I have below. I can't figure out how to code to have it output what I want it to.
#include <iostream>
using namespace std;
int main()
{
int array[2010], n, c, d, swap; //the array
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c=0; c < n; c++)
scanf("%d", &array[c]);
for (c=0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n"); //lists in order
for ( c = 0 ; c < n ; c++ )
printf("%d\n", array[c]);
// Returns minimum difference between any pair
int findMinDiff(int arr[2010], int n); //supposed to find differce
{
// Initialize difference as infinite
int diff = INT_MAX;
// Find the min diff by comparing difference
// of all possible pairs in given array
for (int d=0; d<n-1; d++)
for (int j=d+1; j<n; j++)
if (abs(array[d] - array[d--]) < diff)
diff = abs(array[d] - array[d--]);
cout<<"Total Climb "<<diff<<endl;
}
system("pause");
return 0;
}
I don't see why you are sorting the array. Sorting the array may cause problems in calculating the "total climb" and "total descent".
My understanding is that this assignment is about calculating the difference between two numbers and processing that difference.
void Process_Data(int array[2010], unsigned int quantity_of_climbs)
{
int total_climb = 0;
int total_descent = 0;
int minimum_change = INT_MAX;
for (int i = 0; i < quantity_of_climbs - 1; ++i)
{
const int height_change = array[i] - array[i+1];
if (height_change > 0) // A "climb"
{
total_climb += height_change;
}
if (height_change < 0) // A "descent"
{
total_descent = (-1) * height_change; // Change from negative to positive.
}
const int abs_height_change = abs(height_change);
if (abs_height_change < minimum_change)
{
minimum_change = abs_height_change;
}
}
// Display results
}
I have written a function to raise matrix to a certain power.
But when running the code the result is the memory location not the actual values. I think the problem is with the pointers.
My code:
#include <iostream>
using namespace std;
typedef int array2d [2][2];
array2d A,B,r;
void pow(array2d* r,array2d C);
int main(){
array2d resa,resb;
A[0][0]=2;
A[0][1]=2;
A[1][0]=2;
A[1][1]=2;
B[0][0]=3;
B[0][1]=3;
B[1][0]=3;
B[1][1]=3;
r[0][0]=1;
r[0][1]=0;
r[1][0]=0;
r[1][1]=1;
pow(&resa,A);
for(int i=0;i<2;i++){
for(int j=0;j<2;j++)
{
cout<<resa[i][j]<<" ";
}
cout<<endl;
}
pow(&resb,B);
for(int i=0;i<2;i++){
for(int j=0;j<2;j++)
{
cout<<resb[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
void pow(array2d* r, array2d C)
{
array2d temp;
for(int w=0;w<3;w++)
{
for(int i=0;i<2;i++){
for(int j=0;j<2;j++)
{
temp[i][j]=0;
for(int k=0;k<2;k++)
temp[i][j]+=(*r)[i][k]*C[k][j];
}}
for(int i=0;i<2;i++){
for(int j=0;j<2;j++)
{
(*r)[i][j]=temp[i][j];
}}
}
}
How can I solve this problem.
Thank You.
Your error has nothing to do with pointers or addresses, but your algorithms is erroneous:
pow(&resa, A);
multiplies the matrix A with the uninitialized matrix resa which can yield any number of possible results.
The quick and dirty fix would be to initialize resa and resb as identity matrices:
array2d resa = { { 1, 0 }, { 0, 1 } };
array2d resb = { { 1, 0 }, { 0, 1 } };
EDIT or slightly better: Initialize r inside of pow
//passing r by pointer is not actually necessary here,
//but I don't want to modify too much of the code
(r*)[0][0] = 1;
(r*)[1][0] = 0;
(r*)[0][1] = 0;
(r*)[1][1] = 1;
The more elegant solution would be to first multiply the parameter C with itself, store the result in r and then go on with your algorithm.
On a side note: don't use c-style arrays, if you don't need them (especially not in typedefs). Use std::array instead, which will get rid of most of the confusion regarding to parameter passing.
you really should avoid global when you can, and using the same name in global and local variables is even worse
also the name of your variables MATTERS, here i call the input matrix IN and output matrix OUT (ok "n" should be called power)
using namespace std;
typedef int array2d [2][2];
void display( array2d a ) ;
void pow(array2d IN ,array2d OUT , int n);
int main()
{
array2d A,B;
array2d resa,resb;
A[0][0]=2;
A[0][1]=2;
A[1][0]=2;
A[1][1]=2;
B[0][0]=3;
B[0][1]=1;
B[1][0]=2;
B[1][1]=1;
pow (A , resa , 5);
pow( B , resb, 2 ) ;
return 0;
}
and
void pow(array2d IN, array2d OUT , int n )
{
for( int i = 0 ; i < 2 ; ++ i )
for( int j = 0 ; j < 2 ; ++ j )
OUT[i][j] = ( i == j ) ;
display( OUT ) ;
array2d temp;
for(int w=0;w<n;w++)
{
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
temp[i][j]=0;
for(int k=0;k<2;k++)
{
temp[i][j]+=OUT[i][k]*IN[k][j];
}
}
}
for(int i=0;i<2;i++){
for(int j=0;j<2;j++)
{
OUT[i][j]=temp[i][j];
}
}
display( OUT ) ;
}
}
with
void display( array2d a )
{
cout << endl ;
for( int i = 0 ; i < 2 ; ++ i )
{
for( int j = 0 ; j < 2 ; ++ j )
cout << a[i][j] << " " ;
cout << endl ;
}
}
The first parameter of your pow function, change it to the pass by reference: void pow(array2d &r,array2d C);
Then where you call it, call it like so: pow(resa,A);
Finally, you don't need to deference r in your pow function now, so (*r)[i][j]=temp[i][j]; can be changed to r[i][j]=temp[i][j];
I think this is what you wanted to do. ( pass by reference )
( I am not in front of a pc where I can test this right now, will confirm as soon as I can, and maybe flesh out why this is better ( can read up about pointers, heap memory and pass by reference )
all. I'm trying to set up a program that evaluates a polynomial depending on the user input of X. Another part of the program I want is to then add these polynomials together. I'm using a 2D array to do this. What do you think would be the best way to write the evaluation function. Been working at this for hours and I'm still not quite sure how to do it. Thanks in advance.
polynomial.h
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <iostream>
using namespace std;
#define MAX 100
class Polynomial {
friend ostream &operator<< (ostream &, const Polynomial &);
public :
Polynomial ();
void enterTerms();
int evaluate(Polynomial p, int x);
Polynomial operator +(const Polynomial & );
private :
int terms[MAX][2]; //either static size(MAX rows) or use "new" for dynamic allocation
int n; //number of terms
};
#endif
polynomial.cpp
#include "polynomial.h"
using namespace std;
ostream &operator<< (ostream & out, const Polynomial & p){
for ( int i = 0 ; i < p.n ; i++ ){
if ( i == p.n - 1 )//last term does not have + appended
out << p.terms[i][0] <<"x^"<<p.terms[i][1]<<endl;
else
out << p.terms[i][0]<<"x^"<<p.terms[i][1]<<" + ";
}
return out;
}
Polynomial :: Polynomial(){
for ( int i = 0; i < MAX; i++ ){
terms[i][0] = 0;
terms[i][1] = 0;
}
}
void Polynomial :: enterTerms(){//enterTerms() not in constructor so that no prompt for entering
//terms while doing + - etc., they also produce Poylnomial instance (i.e. invoke constructor)
int num;
cout<<"enter number of terms in polynomial\n";
cin >> num;
n = num >= 0 ? num : 1;
cout << "enter coefficient followed by exponent for each term in polynomial\n";
for ( int i = 0; i < n ; i++)
cin >> terms[i][0] >> terms[i][1] ;
}
Polynomial Polynomial :: operator + ( const Polynomial & p ){
Polynomial temp, sum;
temp.n = n + p.n;
int common = 0;
// first write sum as concatenation of p1 and p2
for ( int i = 0 ; i < n ; i++ ){
temp.terms[i][0] = terms[i][0];
temp.terms[i][1] = terms[i][1];
}
//notice j and k for traversing second half of sum, and whole p2 resp
for ( int j = n, k = 0; j < n + p.n, k < p.n ; j++, k++ ){
temp.terms[j][0] = p.terms[k][0];
temp.terms[j][1] = p.terms[k][1];
}
for ( int l = 0; l < temp.n - 1 ; l++ ){ // 0 to 1 less than length
for ( int m = l + 1 ; m < temp.n ; m++ ){ // 1 more than l to length,so that compared pairs are non redundant
if( temp.terms[l][1] == temp.terms[m][1] ){
common++; //common terms reduce no. of terms in sum (see sum.n decl)
temp.terms[l][0] += temp.terms[m][0]; //coefficients added if exponents same
temp.terms[m][0] = 0;
}
}
}
sum.n = temp.n - common; //if you place it above, common taken as 0 and sum.n is same as temp.n (logical error)
//just to debug, print temporary array
cout << endl << temp;
for ( int q = 0, r = 0; q < temp.n; q++ ){
if ( temp.terms[q][0] == 0 )
continue;
else{
sum.terms[r][0] = temp.terms[q][0];
sum.terms[r][1] = temp.terms[q][1];
r++;
}
}
cout << endl << sum;
return sum;
}
int Polynomial :: evaluate(Polynomial p, int x)
{
Polynomial terms;
return 0;
}
int main()
{
Polynomial p1 , p2;
p1.enterTerms();
p2.enterTerms();
cout << "Please enter the value of x:" << endl;
cin >> x;
//evaluate(p1);
//evaluate(p2);
p1 + p2;
system("PAUSE");
//cin.get();
return 1;
}
Please consider a simpler data structure. A common approach is to use a single array, where the index is the power of x. Just use zeros where no such term exists. Then x^3 + 2*x + 1 is written {1, 2, 0, 1}, since there's no x^2. Also note the reverse order, since p[0] represents x^0. This drastically simplifies operations like addition.
As far as evaluation, just think about the equation. If your polynomial is x^2 + 3*x + 5, and you want to evaluate for x=7, what do you do? Start with power 0, and accumulate each term into a single variable.
You can follow and complete my functions here:
float polyval_point(Eigen::VectorXf v,float x)
{
float s = 0;
for (int i=0;i<v.size();i++)
{
s += v[i] * pow(x,i);
}
return s;
}
Eigen::VectorXf polyval_vector(Eigen::VectorXf v,Eigen::VectorXf X)
{
Eigen::VectorXf S(X.size());
for (int i=0;i<X.size();i++)
{
S[i] = polyval_point(v,X[i]);
}
return S;
}
My assignment to develop a program to compute 3 systems of linear equations: the program must allow the user to input the coefficients and constants, number of iterations and level of acceptable error. I can't seem to include both the number of iterations and level of error as parameters to stop the loop and show the final values of the variables. Here's what I have so far:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
cout<<"Welcome. This is Problem 1. "<<endl;
cout<<"computing systems of three linear equations through gauss-seidel method"<<endl;
float coefEqxn1[3];
for (int x=0; x<3;)
{
for ( int eq1=1; eq1<=3; eq1++)
{
cout<<"Please enter Coefficient " <<eq1<< " of equation 1 : ";
cin>>coefEqxn1[x];
x++;
}
}
float coefEqxn2[3];
for (int x=0; x<3;)
{
for ( int eq2=1; eq2<=3; eq2++)
{
cout<<"Please enter Coefficient " <<eq2<<" of equation 2 :" ;
cin>>coefEqxn2[x];
x++;
}
}
float coefEqxn3[3];
for (int x=0; x<3;)
{
for ( int eq3=1; eq3<=3; eq3++)
{
cout<<"Please enter Coefficient "<<eq3<<" of equation 3 :";
cin>>coefEqxn3[x];
x++;
}
}
float constants[3];
for (int y=0; y<3;)
{
for (int con=1; con<=3; con++)
{
cout<<"Please enter the contant of equation "<<con<<" : ";
cin>>constants[y];
y++;
}
}
cout<<"Calculating through Cramer's Rule..."<<endl;
int iteration=0;
cout<<"enter # iteration"<<endl;
cin>>iteration;
int stopC=0;
cout<<"enter level of error"<<endl;
cin>>stopC;
float matrixArray[3][4];
{
for ( int y=0; y<3;)
{
for (int x=0; x<=3;x++)
matrixArray[0][y]=coefEqxn1[y];
y++;
}
matrixArray[0][3]=constants[0];
for ( int y=0; y<3;)
{
for (int x=0; x<=3;x++)
matrixArray[1][y]=coefEqxn2[y];
y++;
}
matrixArray[1][3]=constants[1];
for ( int y=0; y<3;)
{
for (int x=0; x<=3;x++)
matrixArray[2][y]=coefEqxn3[y];
y++;
}
matrixArray[2][3]=constants[2];
}
for(int a=0; a<3; a++)
{
for(int b=0; b<=3; b++)
cout<<"matrixArray["<<a<<"]["<<b<<"]: "<<matrixArray[a][b]<<endl;
}
float valueOfX[100], valueOfY[100], valueOfZ[100];
for( int i=1; i<iteration; )
{
valueOfX[0]=0, valueOfY[0]=0, valueOfZ[0]=0;
valueOfX[i]=(matrixArray[0][3]-(matrixArray[0][2]*valueOfZ[i-1]+matrixArray[0][1]*valueOfY[i-1]))/matrixArray[0][0];
valueOfY[i]=(matrixArray[1][3]-(matrixArray[1][2]*valueOfZ[i-1]+matrixArray[1][0]*valueOfX[i]))/matrixArray[1][1];
valueOfZ[i]=(matrixArray[2][3]-(matrixArray[2][1]*valueOfY[i]+matrixArray[2][0]*valueOfX[i]))/matrixArray[2][2];
float reX=0, reY=0, reZ=0;
reX=((valueOfX[i+1]-valueOfX[i])/valueOfX[i+1])*100;
reY=((valueOfY[i+1]-valueOfY[i])/valueOfY[i+1])*100;
reX=((valueOfZ[i+1]-valueOfZ[i])/valueOfZ[i+1])*100;
if (reX<=inputErrorLevel)
break;
if (reY<=inputErrorLevel)
break;
if (reZ<=inputErrorLevel)
break;
cout<<"reX = "<<reX<<endl;
cout<<"reY = "<<reY<<endl;
cout<<"reY = "<<reX<<endl;
i++;
}
cout<<"x = "<<valueOfX[iteration-1]<<endl;
cout<<"y = "<<valueOfY[iteration-1]<<endl;
cout<<"z = "<<valueOfZ[iteration-1]<<endl;
}
Based on what you want you should have something like
double inputErrorLevel; //Read from user input
// The for loop guarantees that the loop will run at max iteration times.
// Noticed I changed it to start from zero otherwise it will only run
// iteration -1 times
for( int i=0; i<iteration; ++i )
{
//Do heavy computation stuff.
double currentErrorLevel = ComputeErrorAtCurrentIteration();
if ( currentErrorLevel < inputErrorLevel )
break; // break will ensure more iterations are not done
// if error level is acceptable
}
Typical programming practice is to to
for ( int i = 0 ; i < 5; ++i )
{
}
instead of
for ( int i = 0 ; i < 5; )
{
++i;
}
This looks like a typo?
reX=((valueOfZ[i+1]-valueOfZ[i])/valueOfZ[i+1])*100;
Shouldn't that be:
reZ=((valueOfZ[i+1]-valueOfZ[i])/valueOfZ[i+1])*100;
Same here:
cout<<"reY = "<<reX<<endl;
But to answer your question this should be using i to index the results not iteration. iteration variable is going to always be constant so it will always give that result regardless of error.
Like:
cout<<"x = "<<valueOfX[i-1]<<endl;
cout<<"y = "<<valueOfY[i-1]<<endl;
cout<<"z = "<<valueOfZ[i-1]<<endl;