循环保持运行
while loop keeps running
所以这是我的代码段:
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是一个数组
每当我的程序进入循环时,它永远不会退出
尽管当我用常数数字切换x [0]和x [n]时,它运行完美
有什么想法吗?谢谢
您正在以这种方式阅读:
for (i = 0; i < n; i++) {
cin >> x[i] >> y[i];
}
数组x在位置中的值:[0,n-1]
但在此时:
while (a < x[0] or a > x[n] ){
您尝试访问数组的位置n,如果不初始化,这将是一个随机数。
相关文章:
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 代码在main()中运行,但在函数中出现错误
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何在运行中期切换GTK CSS style_context
- 如何在MS Visual Studio 2019中运行QT UI
- 如何通过cpp程序运行shell脚本
- IPC使用多个管道和分支进程来运行Python程序
- 删除指向指针的指针是运行时错误吗
- 如何用参数值调用函数(仅在运行时已知)
- 为什么即使使用-cudart-static进行编译,库用户仍然需要链接到cuda运行时
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- c++中的指针和运行时错误
- 在C应用程序中运行C++(带有STL)函数
- 运行程序时出现问题
- 控制允许动态运行c++的并发操作数
- 无法获取菜单选择以运行函数.C++