C program - Seg fault, cause of
- by resonant_fractal
Running this gives me a seg fault (gcc filename.c -lm), when i enter 6 (int) as a value. Please help me get my head around this. The intended functionality has not yet been implemented, but I need to know why I'm headed into seg faults already.
Thanks!
#include<stdio.h>
#include<math.h>
int main (void)
{
int l = 5;
int n, i, tmp, index;
char * s[] = {"Sheldon", "Leonard", "Penny", "Raj", "Howard"};
scanf("%d", &n);
//Solve Sigma(Ai*2^(i-1)) = (n - k)/l
if (n/l <= 1)
printf("%s\n", s[n-1]);
else
{
tmp = n;
for (i = 1;;)
{
tmp = tmp - (l * pow(2,i-1));
if (tmp <= 5)
{
// printf("Breaking\n");
break;
}
++i;
}
printf("Last index = %d\n", i); // ***NOTE***
//Value lies in next array, therefore
++i;
index = tmp + pow(2, n-1);
printf("%d\n", index);
}
return 0;
}