c++ - while loop keeps running -
so 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 values of x , corresponding functional value y : \n"; cout << "\n"; cout << "x | y \n"; (i=0; i<n; i++){ cin >> x[i] >> y[i]; } cout << "\n"; cout << "enter value of x calculate 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 domain [" << x[0] << "," << x[4] << "] \n" ; cout << "\n"; cout << "reenter value of x please : "; cout << "\n"; cin >> a; } cout << "\n"; (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] , a<=x[i+1]) cout << "s[" << << "] =" << s[i] << " when " << x[i] << " <= x <=" << x[i+1] <<endl; } return 0;
}
x array
when ever program enters loop never exits it
although when switch x[0] , x[n] constant numbers runs
any ideas? , thanks
you reading in way:
for (i = 0; < n; i++) { cin >> x[i] >> y[i]; }
array x have values in positions: [0, n-1]
but in while:
while (a < x[0] or > x[n] ){
your trying access position n of array, random number if not initialized.
Comments
Post a Comment