Click here to Skip to main content
15,910,787 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Subham and Binary Strings | Basics of Bit Manipulation & Basic Programming Practice Problems | HackerEarth[^]

What I have tried:

#include <stdio.h>
 
int main()
{
    int t,n,i;
    scanf("%d",&t);
    
    while(t--){
        scanf("%d",&n);
        char arr[100000];
        int count=0;
        
        scanf("%s",&arr);
        for(i=0;i<strlen(arr);i++){
            if(arr[i]=='0')
            count++;
        }
        
        printf("%d\n",count);
    }
    return 0;
}


but if i using arr[n] to specify the size of the array.
why we have to fix size of the array.
my code is working for only 1st test case.
plzz explain me this problem occured.
Posted
Updated 2-Nov-17 8:37am

When you declare an array, you have to tell the system how many elements (and of what type) you need - otherwise it can't give you the right number, and if it gives you too few, you will exceed the allocated memory, and write over other data. This will either cause a fault in your app, or cause it to be terminated for trying to access memory it doesn't have.

It's like going to a coffee shop: you have to tell them what size drink you want, because if they give you the biggest they can, you won't want to pay for it, and if they give you an espresso cup you might be very disappointed.

The other solution is to read n, and use malloc to allocate the space you need - n * the size of the element in bytes. Don't forget to free the memory when you are finished with it!
 
Share this answer
 
Comments
jatinp510 2-Nov-17 12:34pm    
what is the problem with changing the size of array in every test case.
and why to use dynamic memory if we have lots of memory.
jatinp510 2-Nov-17 12:36pm    
#include <stdio.h>

int main()
{
int t,n,i;
scanf("%d",&t);

while(t--){
scanf("%d",&n);
char arr[100000];// what if i write char arr[n]
int count=0;

scanf("%s",&arr);
for(i=0;i<strlen(arr);i++){
if(arr[i]=='0')
count++;
}

printf("%d\n",count);
}
return 0;
}
OriginalGriff 2-Nov-17 12:43pm    
You can't change the size of an array in a loop: it is fixed once you define it.
If you want to change the size, you need to use malloc.
jatinp510 4-Nov-17 9:27am    
sir.. waiting for your reply.
plzz refer to the above code once.
jatinp510 2-Nov-17 12:49pm    
#include<bits/stdc++.h>
using namespace std;

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);

int t,n,i;
cin>>t;
while(t--){
cin>>n;
int a[n],b[n];
for(i=0;i<n;i++){
cin>>a[i];
}
for(i=0;i<n;i++){
cin>>b[i];
}
sort(a,a+n);
sort(b,b+n);

if(a[n-1]==b[n-1])
cout<<"Tie\n";
else if(a[n-1]>b[n-1])
cout<<"Bob\n";
else
cout<<"Alice\n";
}
return 0;
}

https://www.hackerearth.com/practice/basic-programming/implementation/basics-of-implementation/practice-problems/algorithm/warcakewalk/
#include <stdio.h>
 
int main()
{
    int t,n,i;
    scanf("%d",&t);
    
    while(t--){
        scanf("%d",&n);
        char arr[n];
        int count=0;
        
        scanf("%s",&arr);
        for(i=0;i<strlen(arr);i++){
            if(arr[i]=='0')
            count++;
        }
        
        printf("%d\n",count);
    }
    return 0;


it is working for 1st test case .
and if i use char[100000] i.e. if i will fix the size of array it is working for all the test cases.
plzzz check the ques in hackerearth.com once.
 
Share this answer
 
Quote:
scanf("%d",&n);
char arr[n];
int count=0;
scanf("%s",arr);

C99 allows dynamic memory allocation on the stack, hence
C
char arr[n];
is a valid statement.
However, your code is flawed, because scanf("%s",arr); could overrun the arr buffer (e.g. suppose the user sets n=5 and then enters the string "foobar").
 
Share this answer
 
Comments
jatinp510 4-Nov-17 9:26am    
plzzz... refer to the ques in hackerearth.com ...
the user is restricted to use only 5 characters input if n=5.
he can't use 6 characters.
and if my code is wrong for arr[n];
then,
why it is working for 1 test case of the ques.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900