Hi everyone,
I am working on a code that generates n evenly spaced nodal points from a to b. It runs but I can't get to add interval to itself every iteration, and I am not sure if I got the formula right.
Any ideas? Thanks, I appreciate it.
Sample output should be:
Code:
a = 3
b = 5
n = 5
3 3.5 4 4.5 5
Code is:
Code:
#include <iostream>
using namespace std;
void LineSpace(float a, float b, int n, float arrayOut[]);
int main()
{
float a = 0.0f, b = 0.0f;
int n = 0;
cout << "Enter an interval [a,b] and number of nodal points n. " << endl;
cout << "Input a = ";
cin >> a;
cout << "Input b = ";
cin >> b;
cout << "Input n = ";
cin >> n;
float* arrayOut = new float[n];
cout << a;
LineSpace(a, b, n, arrayOut);
cout << " " << b << endl;
delete [] arrayOut;
arrayOut = 0;
}
void LineSpace(float a, float b, int n, float* arrayOut)
{
int i = 0;
float interval = 0.0f;
for (i = 0; i < n - 2; ++i)
{
interval = (b - a)/(n - 1); //<-----formula was originally
//(b-a)/ (n-2); I experimented with (n-1) and
//it worked better.. not sure why?!?
arrayOut[i] = a + interval;
cout << " " << arrayOut[i] << " ";
interval += interval; // <----- doesn't work... this is
//supposed to add interval to itself each iteration
}
}