-1
#include<stdlib.h>
#include<stdio.h>
#include<math.h>

int main()
{
    int a,query,in,n,b[n],sum[a];
    sum[1]=0;
    scanf("%d",&query);

    for(a=1;a<=query;a++)
    {
        scanf("%d",&in);
        for(n=1;n<=in;n++)
        {
            b[n]=1+7*(n-1)+6*(n-1)*(n-2)+(n-1)*(n-2)*(n-3);
            sum[a]=sum[a]+b[n];
        }
    }

    for(a=1;a<=query;a++)
    {
        printf("%d\n",sum[a]);
    }       
    return 0;
}

I have made this code which is running in terminal .

But in hacker rank it is showing

Input (stdin)

2

2

5

Your Output (stdout)

~ no response on stdout ~

Expected Output

9

225

Compiler Message

Segmentation Fault

Now what should I do to solve the problem .

gsamaras
  • 71,951
  • 46
  • 188
  • 305
user123733
  • 125
  • 5
  • 6
    `b[n],sum[a]` ? `n` and `a` are uninitialized. Turn your warnings on. And arrays are `0`-based, so your loops are invalid too. – Eugene Sh. Sep 28 '17 at 18:41
  • @EugeneSh. you mean I should define what are n and a .like int a,n; – user123733 Sep 28 '17 at 18:43
  • No, I mean they are *uninitialized*, i.e. do not have a defined value. – Eugene Sh. Sep 28 '17 at 18:43
  • @EugeneSh. so what should I do to solve the problem – user123733 Sep 28 '17 at 18:46
  • You need to assign some values to them during the declaration for example: `int a = 10, n = 943;` – 0___________ Sep 28 '17 at 18:49
  • @PeterJ_01 I have assinged the values as : a=query; and n=in; . But then also it is showing the same error. – user123733 Sep 28 '17 at 18:53
  • @user123733 You need a good C book and learn the basics of the language. query and in the are uninitialized as well. There is no way to make the table of the size which is given by the scanf except the dynamic allocation. – 0___________ Sep 28 '17 at 18:56
  • @PeterJ_01 Can you please help me in this . What should I do to solve this problem – user123733 Sep 28 '17 at 19:02
  • Was too long for the comment – 0___________ Sep 28 '17 at 19:08
  • "Segmentation fault" is not a compiler message; it occurs at execution time. Also, the usual message is `Segmentation fault`, not `Segmentation Fault`. The difference between `fault` and `Fault` may seem trivial, but it indicates that you didn't copy-and-paste the error message. Re-typing source code, program output, or error messages into your question can cause errors that can seriously interfere with attempts to diagnose the problem. (Not in this case, but in general.) – Keith Thompson Sep 28 '17 at 19:12
  • Also, while you're at it, don't use `scanf()` to read user input. (It is usually preferrable to `fgets()` a whole line of input, and then make sense of it using the various `str...()` funktions -- much less error-prone, and easier to debug.) And if you *do* use `*scanf()`, at least check the return value of the function to make sure you actually *did* read a valid value (instead of using the value *uninitialized* as Eugene pointed out... – DevSolar Sep 28 '17 at 19:31

4 Answers4

2

Your variables are uninitialized. As a result your program invokes Undefined Behavior.

For example you do not initialize n, but you then declare int b[n]. What is the size of array b? Nobody really knows, since n has a garbage value.

Start by figuring out what the values of your variables should be, and then start coding.


Array indexing starts from 0, thus your for loops are not looking good.

Change this:

for(a=1;a<=query;a++)

to this:

for (a = 0; a < query; a++)
gsamaras
  • 71,951
  • 46
  • 188
  • 305
1
int main()
{
    int a, query, in, n, *b, *sum;
    scanf("%d",&query);

    sum = malloc(query * sizeof(int));

    /* do some checks if the malloc was successful */ 

    for(a = 0; a < query; a++)
    {
        scanf("%d",&in) ;    /* you should check if scan has returned 1 */
        b = malloc(in * sizeof(int)); /* and again check against any allocation errors */

        for(n = 0; n < in; n++)
        {
           b[n] = 1+7*(n)+6*(n)*(n-1)+(n)*(n-1)*(n-2);
           sum[a] = sum[a] + b[n];
        }
        free(b);
    }

    /* the rest */
0___________
  • 60,014
  • 4
  • 34
  • 74
0

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.

J...S
  • 5,079
  • 1
  • 20
  • 35
  • It's undefined behaviour as soon as `b[n]` is defined. Your answer incorrectly says that it depends on what `a` "could be". Garbage value is garbage value (the formal term is "indeterminate"), it's not like rolling a random number and then proceeding in a well-defined fashion after that – M.M Sep 28 '17 at 20:53
  • @M.M Can you explain? Why won't it proceed in a proper fashion with the indeterminate array size? – J...S Sep 29 '17 at 03:23
  • Because `b[n]` causes [undefined behaviour](https://stackoverflow.com/a/4105123/1505939) which means that the program is no longer covered at all by the definition of the C language – M.M Sep 29 '17 at 03:35
  • @M.M But wouldn't that be a variable length array? Isn't that legal? – J...S Sep 29 '17 at 03:45
  • It's not legal to declare one with indeterminate value as the dimension – M.M Sep 29 '17 at 04:12
-1

Your problem is in this line:

int a,query,in,n,b[n],sum[a];

What is the meaning of b[n]? And of sum[a]? Assuming that you would like to have a language with magically growing arrays, C is a bad choice.

In C language arrays are a structure with size known at compile time. Anyway, let's assume that your compiler supports the horrible hack of variable-length arrays. You can do this:

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

int main(void) {
    int a, query, in , n;
    scanf("%d", &query);
    int sum[query+1];

    for (a = 1; a <= query; a++) {
        sum[a] = 0;
        scanf("%d", &in);
        int b[in+1];
        for (n = 1; n <= in ; n++) {
            b[n] = 1 + 7 * (n - 1) + 6 * (n - 1) * (n - 2) + (n - 1) * (n - 2) * (n - 3);
            sum[a] = sum[a] + b[n];
        }
    }

    for (a = 1; a <= query; a++) {
        printf("%d\n", sum[a]);
    }

    return 0;
}

Note the changes: first you need to know the size of the array, then you can allocate it. Moreover, I increased the size of the array in order to support your choice of starting from 1. Lastly I also zeroed the initial value for sum array.

Costantino Grana
  • 3,132
  • 1
  • 15
  • 35
  • It will not work my friend. It does not change anything. potting the scanf between the variable declarations will not help. Space for them is allocated on the function entry. It would work if your compiler supports code blocks local variables, but is hat to be written a different way scanf("%d", &query); { int sum[query+1]; ..... } - you need to wrap it into the separate code block. – 0___________ Sep 28 '17 at 19:12
  • 1
    @PeterJ_01: The standard says "6.2.4.6 For such an object that does have a variable length array type, its lifetime extends from the declaration of the object until execution of the program leaves the scope of the declaration." So, the space for them is not allocated on the function entry, but after their declaration. – Costantino Grana Sep 28 '17 at 19:31
  • @PeterJ_01: Test it here https://ideone.com/aysSNw. Of course it will not work in compilers which do not support variable-length arrays, such as the Microsoft C Compiler. – Costantino Grana Sep 28 '17 at 19:34
  • Disagree with calling VLAs a "horrible hack" . Just about every other programming language has runtime array sizing, this improvement to the language just helps to bring it out of the dinosaur era. Also your statement that array sizes are known at compile-time is false., as demonstrated by your program – M.M Sep 28 '17 at 20:57
  • @M.M: "Just about every other programming language has runtime array sizing", unfortunately variable-length arrays sizes cannot change after allocation. So you get a language construct which can solve some of the problems of arrays, but not enough. And is not portable, because now (C11) it is an optional feature. – Costantino Grana Sep 28 '17 at 21:09
  • True enough about the resizing; however all compilers support it except MSVC which is a political issue (they refuse to implement it due to being immature about the standardization process) – M.M Sep 28 '17 at 21:38