In int a,query,in,n,b[n],sum[a];
, the value of a
is not initialised and is having garbage value which could be anything. This value is used as the size of the variable length array sum
. So the size of the array could be anything which is probably not what you want.
a
could be 0
in which case sum
is an array of size 0
which will in turn make sum[1]
incorrect (it would be undefined behavior).
The same applies to n
and b[n]
.
In the nested for
loop, with sum[a]=sum[a]+b[n];
you are using sum[a]
which has not been initialised. It has garbage value and the result is indeterminate.
If you wanted all elements of sum
to be initialised with 0
, you can do
int sum[20]={0};
at the time of declaring it.
The same goes for b[n]=1+7*(n-1)+6*(n-1)*(n-2)+(n-1)*(n-2)*(n-3);
as well.
And array indexing starts at 0
.
Perhaps you could use the loops like
for (a = 0; a < query; a++) {
instead of
for (a = 1; a <= query; a++)
If you choose the starting index as 0
, the inner nested loop should be like
for(n=0;n<in;n++)
{
//b[n]=1+7*(n-1)+6*(n-1)*(n-2)+(n-1)*(n-2)*(n-3);
b[n]=1+7*(n)+6*(n)*(n-1)+(n)*(n-1)*(n-2);
sum[a]=sum[a]+b[n];
}
See demo.