Reputation: 3
so this is my code snippet :
double numericalMethod::splineLinearInterpolation(){
int n, i=0;
double x[100], y[100], s[100],a;
cout << "Enter no. of sample points : \n";
cout << "\n";
cin >> n;
cout << "\n";
cout << "Enter all values of x and corresponding functional value y : \n";
cout << "\n";
cout << "x | y \n";
for (i=0; i<n; i++){
cin >> x[i] >> y[i];
}
cout << "\n";
cout << "Enter the value of x for which you would like to calculate the estimated value of y : \n ";
cout << "\n";
cin >> a;
while (a<x[0] or a>x[n]){
cout << "The selected value of x must belong to the domain [" << x[0] << "," << x[4] << "] \n" ;
cout << "\n";
cout << "Reenter the value of x please : ";
cout << "\n";
cin >> a;
}
cout << "\n";
for (i=0; i<n-1; i++){
s[i]=y[i] + ((y[i+1]-y[i])/(x[i+1]-x[i]))*(a-x[i]);
if (a>=x[i] and a<=x[i+1])
cout << "s[" << i << "] =" << s[i] << " when " << x[i] << " <= x <=" << x[i+1] <<endl;
}
return 0;
}
x is an array
when ever my program enters the loop it never exits it
although when i switch x[0] and x[n] with constant numbers it runs perfectly
any ideas? and thanks
Upvotes: 0
Views: 119
Reputation: 61
You are reading in this way:
for (i = 0; i < n; i++) {
cin >> x[i] >> y[i];
}
Array x will have values in the positions: [0, N-1]
But in this while:
while (a < x[0] or a > x[n] ){
Your trying to access position N of the array, which will be a random number if not initialized.
Upvotes: 3