0

Can anybody tell me why it returns 0? I have Input:

5 2

1 0 1 1 0

And 2 queries 1 X to flip Xth bit in array:

1 2 (result {1 1 1 1 0} )

0 L R to take a subarray and convert it into Decimal:

0 1 4 (Here it takes {1,1,1,1})

In this case it should print 15. But this printing just zero?

My code:


#include <iostream>
using namespace std;


const int maxn = 1e6 +500;
char a[maxn];
//That's function which I'm stuck with
long bin_to_dec(char *s)
{
    short i;
    long res = 0;
    
    for (i = 0;i<sizeof(long)*8 && s[i];++i)
    {
        res<<=1;
        res |=(s[i]-'0');
        
    }

    
    return res;
}
//This function responsible for queries
//Arguments which it takes : vid - type of query,int inx - first parameter,inx1 - 2nd parameter
int zapros(int vid,int inx,int inx1 = 0)
{
    string s = "";
    if (vid == 1)
    {
        a[inx] +=1;
        
    }
    else
    {
        int c = 1;
        char ch[inx1+1];
        for (int k = inx;k<=inx1;k++)
        {
            
            ch[c] = a[k];
            c++;
            
        }
    
        cout<<bin_to_dec(ch);
        /*
        for (int k = inx;k<=inx1;k++)
        {
            s+=a[k];
        }*/
        
    }
}



int main()
{
    int n,q;
    int vid,inx,inx1;
    cin>>n>>q;
    for (int i =1;i<=n;i++)
    {
        cin>>a[i];
    }
    for (int j =1;j<=q;j++)
    {
        cin>>vid;
        if (vid == 1)
        {
            cin>>inx;
            zapros(vid,inx);
        }
        else
        {
            cin>>inx>>inx1;
            zapros(vid,inx,inx1);
        }
    }
    
    
    
    return 0;
}

I'm a beginner in C++,so please dont'be strict;)

Community
  • 1
  • 1
Blazing_Sun
  • 93
  • 1
  • 1
  • 6
  • 1
    Since you are a beginner in C++, please stop using C-style arrays and use `std::string` instead. Then, use standard library facilities like `std::bitset` to make your code easier. Do not `using namespace std;` because it is considered bad practice. See [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721) – L. F. Feb 23 '20 at 12:32
  • 1
    Better variable and function names would help to understand what that they are meant to be for. From just a first look at the code I do not understand why you are sometimes starting at array index 0 and sometimes at index 1. – Werner Henze Feb 23 '20 at 12:38

1 Answers1

0

You have done a lot of errors... The arrays start from 0, not from 1. Why did you use char array? I've corrected the errors and I changed the binary to int function, using the standard convertion(https://www.electronics-tutorials.ws/binary/bin_2.html). When you create the ch array you can't write "char ch[expression]", it's forbitten. You have to use pointers.

#include <iostream>
#include <cmath>
using namespace std;


const int maxn = 1e6 +500;
int a[maxn];
//That's function which I'm stuck with
long bin_to_dec(int s[],int n)
{
    long res = 0;
    int cont=0;
    for (int i = n-1;i>=0;i--)
    {
        res+=s[i]*pow(2,cont);
        cont++;
    }
    return res;
}
//This function responsible for queries
//Arguments which it takes : vid - type of query,int inx - first parameter,inx1 - 2nd parameter
int zapros(int vid,int inx,int inx1 = 0)
{
    string s = "";
    if (vid == 1)
    {
        if(a[inx]==1)
            a[inx]=0;
        else
            a[inx]=1;

    }
    else
    {
        int cont = 0;
        int *ch;
        ch=new int[inx1];
        for (int k = inx;k<inx+inx1;k++)
        {
            ch[cont] = a[k];
            cont++;
        }
        cout<<bin_to_dec(ch,inx1)<<endl;
        /*
        for (int k = inx;k<=inx1;k++)
        {
            s+=a[k];
        }*/
        delete[] ch;
    }
}



int main()
{
   // cout<<maxn<<endl;
    int n,q;
    int vid,inx,inx1;
    cin>>n>>q;
    for (int i =0;i<n;i++)
    {
        cin>>a[i];
    }
    for (int j =0;j<q;j++)
    {
        cin>>vid;
        if (vid == 1)
        {
            cin>>inx;
            zapros(vid,inx);
        }
        else
        {
            cin>>inx>>inx1;
            zapros(vid,inx,inx1);
        }
    }



    return 0;
}