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.