Odd-Why is it giving runtime error??

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
char kases[6],no[9];
int j=0,t,n,sq,ans[100],i=0;
gets(kases);
t=atoi(kases);
while(t–)
{
fflush(stdin);
gets(no);
n=atoi(no);
sq=sqrt(n);
ans[i]=pow(2,sq);
i++;
// printf("%d\n",ans);
}
while(i–)
{
printf("%d\n",ans[j]);
j++;
}
return 0;
}

You have to solve these problems first

  • strlen(“1000000”) > 6
  • strlen(“1000000000”) > 9
  • 100 < 1000000 = 10^6

:wink:

To simulate the RE you can use this code to generate input file:

#include <stdio.h>

#define T 1000000

int main( int argc, char** argv ) {
	printf("%d\n", T);
	for ( int i = 0; i <= T; ++i ) printf( "%d\n", 1000000000 - i );
	return 0;
}

sorry…bt I’m not able to understand your answer…plz elaborate

You are reading k to kases, size of array is 6, but max k is 1.000.000 => 7 characters, same problem with no array. Last problem is, that array size for results (ans) is 100, but it has to have same size as number of cases.

The code above generates some huge input that your code fails on (on my computer). Hope it helped.

1 Like

so what to do for the answer part??

There are 2 solutions you can do: 1.) you can initialize ans to max k - int ans[1000000] or 2.) you can declare ans after you read n as int ans[n].

1 Like

thanx…:slight_smile:

bt its still showing ‘runtime error’…

one more thing: you don’t need to read string and convert it to number, using scanf() you can read using format similar to printf() instead of printf("%d", n); you have to do scanf("%d", &n ); notice the & before n :wink:

On my computer it’s failing probably because int ans[1000000] is too big, when i moved it before main() and initialized as int ans[1000000] it seems to work fine on my computer, also resize kases and no arrays, let say to 20 (there is at least '\n' at the end and there to be space for ‘\0’ too, good practice in speed programming is to use a bit bigger arrays). Hope that was the final problem.

ODD RUNNING   april 2017 printable calendar
  cleanclear

may 2017 calendar
  may 2017 calendar printable

Your account will soon going to be blocked…