Help in ADACRA

i wrote a program for ada and crayons problen(ADACRA) and submitted it. I am new in here and it showed the ‘!’ inside a red circle and showed error code SIGSEGV. can you please tell me what is the problem. here is my code:

#include<iostream>
using namespace std;
int main()
{
    char arr[50];
    int n;
    int crayon_U=0;
    int crayon_D=0;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    for(int i=0;i<n;i++)
    {
        if(arr[i]!=arr[i+1]&&arr[i]=='U')
            crayon_D++;
        else if(arr[i]!=arr[i+1]&&arr[i]=='D')
            crayon_U++;
    }
    cout<<min(crayon_D,crayon_U);
    return 0;
}

for(int i=0;i<n;i++)
{
cin>>arr[i];
}

Remove this. C++ has a separate data type “strings” to handle strings. Also, you are NOT given the length of string. You will need to find it using “.length()” function.

Also, your code does things for only 1 string. While you are expected to print output for T strings. All you have to do is-

//enter code here.
int t;
cin>>t;
while(t--)
{
//solve question in this loop
}

You can look at my code for reference, although i did the problem in a long way :stuck_out_tongue: CodeChef: Practical coding for everyone

In the problem statement, they never give you n which you are scanning. So the value that your get in n is actually value of T in the problem statement. Then you are scanning characters n times, which gives the problem.
Use std::string to scan the string, and then get its length by using string_name.length().
Or read the problem statement carefully and implement it again. :slight_smile:

#include
using namespace std;
int main()
{
int t;
cin>>t;
for(int x=0;x<t;x++)
{
string s;
cin>>s;
int u=0,d=0;
for(int m=0;s[m]!=’\0’;m++)
{
if(s[m]==‘U’)
u++;
else
d++;
}
int count=0;
if(u>d)
{
for(int i=0;s[i]!=’\0’:wink:
{
if(s[i]==‘D’)
{
while(s[i]==‘D’)
i++;
count++;
i++;
}
else
i++;
}
}
else
{
for(int j=0;s[j]!=’\0’:wink:
{
if(s[j]==‘U’)
{
while(s[j]==‘U’)
j++;
count++;
j++;
}
else
j++;
}
}
cout<<count<<endl;
}
return 0;
}
Unable to find bug in my code as it runs for all the test cases perfectly!

ty for help…i now understand it