-2

I have a program to try to solve this Codeforces question -> https://codeforces.com/problemset/problem/1625/A

However, when I run the code an error pops up. This is my code:

#include<iostream>
#include<bitset>
#include<math.h>
using namespace std;

int main (){
  int tests;
  string aray[10000];
  cin >> tests;
  for(int a; a < tests; a++){
    int words, letters;
    cin >> words >> letters;
    for(int b; b < words; b++){
        int x;
        cin >> x;
        string binary = bitset<32>(x).to_string;
        aray[b] = binary;
    }
    int zeros, ones, total;
    for(int c; c < 32; c++){
        for(int d; d < words; d++){
            if(aray[d][c] = '0'){
                zeros++;
            }else{
                ones++;
            }
        }
        if(zeros < ones){
            total = total + pow(2, 31-c);
        }
    }
    cout << total;
  }
}

I tried searching up solutions, but I just couldn't understand or find any solutions that helped me. Can you tell me why this happens and how to fix it? Thanks! I've just started C++ and am not too familiar with syntax.

Triton Mei
  • 11
  • 1
  • This question's code/phrasing suggests that it came from one of many countless coding challenge/puzzle scam sites. They take advantage of people who want to learn C++ by offering arcane coding puzzles and promising that you don't really need to study and learn C++ with a good textbook for many years, just do a bunch of meaningless coding puzzles. Everyone eventually realizes that they got scammed; these pointless coding puzzles are a waste of time, and there's nothing to be learned from them. But only after spending a lot of time doing them, with nothing to show for it. – Sam Varshavchik Jul 29 '23 at 15:18
  • 1
    the issue is here: "string binary = bitset<32>(x).to_string;", you've forgotten to call the function, it should be "string binary = bitset<32>(x).to_string();" – Azam Bham Jul 29 '23 at 15:19
  • 1
    This is not the only "issue" here. The first issue would be `for(int a; a < tests; a++){`. – Sam Varshavchik Jul 29 '23 at 15:20
  • For future questions it would be really helpful for you to include the line that causes the error. Your compiler tells you that, so please let us know as well. Also: IMO these coding puzzles are not that bad. You just can't do them expecting they'll make you a good **programmer**, instead they just train general logic/problem solving skills - which can also be useful. – Wutz Jul 29 '23 at 15:22
  • Please [edit] your code to reduce it to a [mcve] of your problem. Your current code includes much that is peripheral to your problem - a minimal sample normally looks similar to a good unit test: only performing one task, with input values specified for reproducibility. – Toby Speight Jul 29 '23 at 15:25
  • A side note: [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – wohlstad Jul 29 '23 at 15:48
  • I wouldn't place an array of 10000 strings on the stack (`string aray[10000];`). And it's better to use `std::vector` anyway. – wohlstad Jul 29 '23 at 15:50
  • The indices in your loops (`a`, `b` etc.) are uninitialized. That's causing UB when using them. – wohlstad Jul 29 '23 at 15:52

1 Answers1

2

try replacing:

string binary = bitset<32>(x).to_string;

with:

string binary = bitset<32>(x).to_string();
                                       ^^
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
binaryescape
  • 105
  • 6