【问题标题】:Finding the interpolation with user input使用用户输入查找插值
【发布时间】:2014-04-01 03:57:54
【问题描述】:

我对算法做了一些修改,并在函数中得到了一个值(虽然它不正确),但是返回值拒绝将它发送回主函数。另外,我无法得到他们是的,没有要求程序重新运行以运行的代码部分。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
/*Prototype for functions used*/
float getdistance(float[], float[], float, int);
float printvalue(float);

int main()
{

      int maxtime = 18;
      float usertime = 0;
      char reiteration;
      char y, n;
      int reit_choice = 0;
      float interpolation = 0.0;
      float time_arr[]={0,3,5,8,10,12,15,18};
      float distance_arr[]={2,5.2,6,10,10.4,13.4,14.8,18};
      do
      { 
       printf("Please enter a number between 0 and 18 for the starting time:");
       scanf("%f", &usertime, "\b");
        while(usertime <0 || usertime >18)
       {
          printf("The value you have entered is not valid, please enter a value between 1 and 18");
          scanf("%f", &usertime, "\b");
       }/*End of data verification loop*/
       getdistance(time_arr, distance_arr, usertime, maxtime);
       printf("%f", interpolation);
       printvalue(interpolation);

       system("pause");

       printf("would you like to check another time?(y/n)");
       scanf("%c[y n]", &reiteration, "\b");
       while(reiteration!='y' || reiteration!='n')
       {
          printf("Please enter either y for yes or n for no");
          scanf("%c", &reiteration, "\b");
       }/*End of choice verification loop*/
       if(reiteration = 'y')
       {
         reit_choice = 1;
       }
       else
       {
         reit_choice = 0;
       }
}/*End of do loop*/
while(reit_choice);

}/*End of main loop*/

float getdistance(float time_arr[], float distance_arr[], float usertime, int maxtime)
{
   int index=0;
   float interpolation = 0;

   for(index; usertime > time_arr[index]; index++)
   {
      if(usertime<3)
      {
         break;
      }
   }/*End of value assignment for loop*/
   interpolation = (time_arr[index]) + (((time_arr[index +1] -     time_arr[index])/(distance_arr[index +1] - distance_arr[index])) * (usertime - distance_arr[index]));
   printf("%f", interpolation);
   return interpolation;
}/*End of distance calculation loop*/

float printvalue(float interpolation)
{
   printf("The interpolation was %f", interpolation);
}/*End of screen print loop*/

【问题讨论】:

  • 尝试通过调试器运行代码并检查中间值以更好地定位错误。或者,打印中间值。另请注意,这个问题是模棱两可的:有很多方法可以在数字之间进行插值。
  • 我的主要问题是我的值一直为零,并且要求用户继续的循环忽略了键盘输入。
  • 能否提供求插值的算法?
  • 插值 = (time_arr[curpos]) + (((time_arr[curpos +1] - time_arr[curpos])/(distance_arr[index +1] - distance_arr[index])) * (usertime - distance_arr[curpos]));
  • 即使知道插值算法,我仍然不会花时间调试您的代码并编写答案。代码示例包含太多的自重,主要是字符串解析。请提供错误代码的最小示例,以及您期望它执行的操作。这样,您在编写问题时至少付出了最少的努力,可能会自己发现错误,并使问题对更多读者有用。

标签: c function char interpolation do-while


【解决方案1】:

输出为0的原因是

getdistance(time_arr, distance_arr, usertime, maxtime);

int main() 函数中,您正在调用函数 getdistance 计算插值并返回值,但在主函数中 返回值未分配给变量插值。所以你必须修改代码为

interpolation = getdistance(time_arr, distance_arr, usertime, maxtime);
printvalue(interpolation);

这将打印输出

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-15
    • 2011-11-09
    • 1970-01-01
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-29
    相关资源
    最近更新 更多