-3

I'm trying to solve this problem with and without using an array.

Problem:

The weather report of Chefland is Good if the number of sunny days in a week is strictly greater than the number of rainy days.

Sample input:

4            
1 0 1 0 1 1 1
0 1 0 0 0 0 1
1 1 1 1 1 1 1
0 0 0 1 0 0 0

Its output:

YES
NO
YES
NO

When I use an array to store all the values, it works. But when i use a single variable it throws a sigsegv error.

Code using array which works:

#include <stdio.h>
#define max 7
int main(void) {
    int T,Arr[max];
    scanf("%d",&T);
    while(T--)
    {
        int sun=0, rn=0;
        for(int i=0;i<max;++i)
        {
            scanf("%d",&Arr[i]);
            Arr[i]==0 ? ++rn : ++sun;
        }
        sun>rn?printf("Yes\n") : printf("No\n");
    }
    return 0;
}

Code using variable which causes sigsegv:

#include <stdio.h>
#define max 7
int main(void) {
    int T,a;
    scanf("%d",&T);
    while(T--)
    {
        int sun=0, rn=0;
        for(int i=0;i<max;++i)
        {
            scanf("%d",a);
            a==0 ? ++rn : ++sun;
        }
        sun>rn?printf("Yes\n") : printf("No\n");
    }
    return 0;
}

It's my understanding that, as 'a' is an integer variable, every time I perform a scanf it should store the new value and then perform the next operation, that way I don't have to waste more memory by using an array. But it's throwing sigsegv. Not sure why.

ikegami
  • 367,544
  • 15
  • 269
  • 518
dijkstra
  • 11
  • 3
  • `scanf("%d",a)` - You got the array element input correctly. Wonder why you missed an ampersand here. – StoryTeller - Unslander Monica Jan 18 '23 at 05:59
  • Your compiler should be warning you of this error. If not, then you should increase the warning level of your compiler. I suggest that you read this: [Why should I always enable compiler warnings?](https://stackoverflow.com/q/57842756/12149471) – Andreas Wenzel Jan 18 '23 at 06:01
  • Sorry, it was a typo. – dijkstra Jan 18 '23 at 06:02
  • Do you still have the segmentation fault after fixing the typo? – Andreas Wenzel Jan 18 '23 at 06:09
  • Is the first line of the input literally `|4 |`? If not, then please [edit] the question to specify the literal input. Also, please make sure that you are specifying the literal output. The specified output does not exactly correspond to your posted code. – Andreas Wenzel Jan 18 '23 at 06:10
  • 1
    After your most recent edit, your posted output still does not correspond exactly to your posted code. Please make sure you post the actual output of your actual code. Please use copy&paste so that there are no errors. – Andreas Wenzel Jan 18 '23 at 06:14
  • @Andreas Wenzel Re "*Is the first line of the input literally `|4 |`?*", No. That's something *you* added. It was a table before your change. (Your change caused the markup for a table to be interpreted literally.) The OP should have posted the actual input and not a table, though. – ikegami Jan 18 '23 at 06:15
  • @ikegami: Both [revision 1](https://stackoverflow.com/revisions/75155152/1) and [revision 2](https://stackoverflow.com/revisions/75155152/2) does not display the input as a table. The `|` were printed as part of the input, not as formatting. Only the output was displayed as a table, and that was only the case in revision 1. Therefore, I did not change the displayed input. All I did was put the displayed input into a code block. – Andreas Wenzel Jan 18 '23 at 06:20
  • @Andreas Wenzel, It does for the output. And it was clearly meant for the input too. – ikegami Jan 18 '23 at 06:57
  • @AndreasWenzel Yes the code is working now. The input is actually a table, like not literally what's mentioned above. I tried to edit it, but I always screw it up someway or other. I'll have to see how to format a table correctly. Thanks for your input. – dijkstra Jan 18 '23 at 09:24
  • @dijkstra: Questions seeking debugging help should generally provide a [mre] of the problem. This means that your input and output should correspond exactly with your posted code. If you instead post pseudo-code or pseudo-input and pseudo-output, then your question becomes much harder to answer. This will also cause your question to be downvoted (which has happened here). – Andreas Wenzel Jan 18 '23 at 14:46

1 Answers1

1

Your scanf usage is wrong in the second snippet.

scanf("%d",a);

should rather be

scanf("%d",&a);
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261