【问题标题】:Power of "number" accumulating in an Array数组中累积的“数”的幂
【发布时间】:2016-08-22 09:42:26
【问题描述】:

我在一个程序中得到了一个公式,该公式依赖于在数组中累积数字的幂。我在其他页面上注册了这个,但这不是我需要的:

   #include <stdio.h>
   #include <math.h>
   int main (){
   int x, y, powernumber;
   scanf ("%d %d",&x,&y);
   powernumber = pow(x,y);
   printf ("Powernumber = %d",powernumber);
   return 0;}

没关系,但我必须为每个 powernumber 分配一个插槽。

目前的问题是这样的:

   int array [100];
   int i, int1;

   scanf ("%d",&int1);
   for (i=0; i<=100; i++)
   {array [i] = 1 / pow(2, int1); // <-----------------ERROR
   printf ("%d %d\n",i+1,array [i]);}
   return 0;}

公式的要点是,用户输入一个数字,这个数字将作为一个幂数累加,每次循环进行一轮,直到循环在 i=100 处终止。假设用户输入数字 3。程序第一次执行 2^3,然后下一次执行 2^4,然后是 2^5,在一个数组中。它适用于乘法和除法等普通运算符,但不适用于幂数。上面的示例是一个剥离版本,因此它可能适用于编译器。但希望这个问题是可以理解的。

【问题讨论】:

  • 当出现错误时,请务必输入错误信息,以方便stack-overflowers快速了解问题。
  • 你至少有一个错误,i&lt;=100 应该是i &lt; 100。现在您的循环尝试在最后一次迭代中分配给array[100],这是一个溢出。

标签: c


【解决方案1】:

要获得你需要做的功率数字:

array [i] = (pow(2, int1));

要使用除法,您需要使用浮点数数组和浮点数表示。还需要打印为 %f 而不是 %d:

float array [100];
int i;
float int1;
scanf ("%d",&int1);
for (i=0; i<=100; i++)
{
    array [i] = 1.0 / pow(2.0, int1);
    printf ("%d %f\n",i+1,array [i]);
}
return 0;}

【讨论】:

    【解决方案2】:

    我在这里找到了方法:Change int array to float array in C

    以及我将在此处打印的例程版本以表示感谢。

       #include <stdio.h>
       #include <math.h>
       //1D_Array(float). Create array, then divide 2 numbers and display sum.
       //KHO2016.no3. mingw (TDM-GCC-32) . c-ansi .
       int main()
       {
       //Declare
       int i,int1;
       float flp1,flp2,flp3,array[30];
    
       //valuate
       printf ("The program request 4 numbers\n");
       printf ("1. Number of Rows or loops\n2. Value above the fraction line\n");
       printf ("3. Value beneath the fraction line\n4. Value of Powernumber\n\n");
    
       jump :
       printf ("Type in number of loops, 1-30 :");
       scanf ("%d",&int1);
       if (int1>31)
       {printf ("Number is too large\n");
       goto jump;}
    
       else if (int1<=30)
       printf ("Type the number Above the Fraction line (Dividend) ");
       scanf ("%f",&flp1);
       printf ("Type the number beneath the Fraction line (Divisor) ");
       scanf ("%f",&flp2);
       printf ("Value of Powernumber ");
       scanf ("%f",&flp3);
    
       //calculate
       for (i<0;i<=int1-1;i++)
       {array[i]=(float)(flp1/pow(flp2, (++flp3)-1)); // <------ interesting part !
       printf ("Row [%d]\t%.0f / %.0f^%.0f = %.6f\n",i+1,flp1,flp2,flp3-1,(float)array[i]);}
    
       //terminate
       return 0;
       }
    

    【讨论】:

      【解决方案3】:

      谢谢。对于初学者来说,它有助于做到这一点

         for (i=0; i<=100; i++)
         {array [i] = 1 / (pow(2, int1++));
      

      我想我把它整理好了。我现在遇到的问题是在数组中获取浮点数。该数组执行预期的操作。但是在这里:

         float int1;
         printf ("%d %f\n",i+1,array [i]);
         // The array refuse to be display floating numbers.
         //If the array is defined as a Float it also refuses.
      

      【讨论】:

      • 嗯,它是一个整数数组,所以你不能打印一个浮点数并期望它是正确的
      • 我同意!但我认为让我摸索的是我将powernumber除以1。将输出数字设为小数。现在我必须找出如何制作一个浮点数数组。问题应该是关于数组中的浮点数。好吧。
      猜你喜欢
      • 2021-11-27
      • 2017-04-02
      • 2023-01-20
      • 1970-01-01
      • 2015-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-10
      相关资源
      最近更新 更多