Search code examples
cpow

C: for (i=0;i<=N;i++) { t[i] = pow(a,0.i); } ALWAYS returning 1


C programming: Trying to store in a float table the values of a^i while (a) is an integer variable taken as input and i = 0,N (N also an integer variable taken as input)

Using this function I created:

void remplissage(double *t,int a,int N)
{
 int i;
 double aux;

 for (i=0;i<=N;i++)
  {
    t[i] = pow(a,0.i);
  }
}

Then displaying the values with:

void affichage(double *t,int N)
 {
 int i;
 for (i=0;i<=N;i++)
   {
    printf("tab[%d]= %.3f\n", i, t[i]);
   }
 }

Gives me this:

Saisir a: 990
Saisir N= 5
tab[0]= 1.000
tab[1]= 1.000
tab[2]= 1.000
tab[3]= 1.000
tab[4]= 1.000
tab[5]= 1.000

For whatever (a) and (N) I chose.


Solution

  • Remove the 0. in front of i (or use i / 10.0 as second argument to pow instead -- see comments).

    The way you have it, the exponent is always 0.

    Actually, it is not standard compliant code because i is an integer suffix and 0. a float constant. But it seems your compiler accepts it as 0 anyway.