1

So, my program is supposed to receive test inputs like:

3
1 0 1
0 1 1
1 0 1
5
1 1 1 0 0
1 1 0 1 1
1 0 1 0 1
0 1 0 1 0
0 1 1 1 1
3
1 0 0
0 1 0
0 0 1
2
1 1
1 1
0

where the single-valued lines (n) are the size of a NxN matrix located in the following n entries like shown above. If n = 0, the program stops. The output must be the biggest sum amongst the columns of the matrix. So I expect outputs like this:

3
4
1
2

After a lot of effort and wasted time, I managed to get the first output correctly, but I noticed the following ones sometimes summed up and suggested some variable was not being reset. Here's my code:

#include <iostream>
using namespace std;

int pop = 0;

int main() {
    int n, i, j, k;
    cin >> n;
    while (n!=0) {
        int alunos[n]={0};
        pop = 0;
        for (i=0;i<n;i++) {
            int array[n]={0};
            for (j=0;j<n;j++) {
                cin >> array[j];
                if (array[j]==1) alunos[j]++;
            }  
        }
        for (k=0;k<n;k++) {
            if(alunos[k]>pop) pop = alunos[k];
        }
        cout << pop << endl;
        cin >> n;
    }
    return 0;
}

Noticed that I'm outputting pop(the biggest sum) and resetting it to 0 everytime a new n is given. alunos[n] is an array with the sums of each column (also resetted on every while loop) and array[n] is just an auxiliary array for reading each line of input. My outputs with this are:

3
5
6
8

Thanks in advance!

Gabriel Rebello
  • 1,077
  • 1
  • 11
  • 17
  • _"After a lot of effort and wasted time ..."_ What about inspecting your code with a debugger by stepping through line by line? Any anomalies you can observe doing this? Doing so usually amazingly reduces the time to analyze what's actually going wrong. Better, than making wild guesses, and changing statements based on these. – πάντα ῥεῖ Jun 01 '15 at 22:17
  • 3
    I can't reproduce this. The output (mingled with the input) is `3 4 1 2`. – isanae Jun 01 '15 at 22:20
  • 1
    Please note that VLA's are not supported by every major compiler ( not saying that this is the issue,I'm just pointing this out since you do use it in your code) – Alejandro Jun 01 '15 at 22:21
  • I'm using pocket++ with notepad++ on Windows for compiling my code and I don't think it has that functionality. It only shows compilation errors by default. Maybe if I use some extra debugger... But since I'm new into this I don't know any ): – Gabriel Rebello Jun 01 '15 at 22:22
  • What's your actual toolchain? Pocket c++ isn't an obvious one? – πάντα ῥεῖ Jun 01 '15 at 22:26
  • Aside from the variable array issue, I'm not getting any warnings (g++5.1), and your program runs just fine. – vsoftco Jun 01 '15 at 22:27
  • I dunno about your toolchain, but clang doesn't allow VLA's to be initialized like that even when supporting gcc extensions. The code as-written won't even compile on clang. – WhozCraig Jun 01 '15 at 22:30
  • 1
    I guess it could only be the compiler then. From their GitHub page, it's supposedy a MinGW's gcc 5.1 (dunno if that's all you mean by 'toolchain') integration with notepad++. I do get the error message `error: variable-sized object 'alunos' may not be initialized` when compiling but I thought it would make no difference since the compilation went on anyways. – Gabriel Rebello Jun 01 '15 at 22:38
  • I also thought clang only worked for C programs. I may try it later. Is there a way to rewrite the code to make it functional without changing the compiler? Or maybe a better way of making sure I don't get these code-unrelated errors in the future? – Gabriel Rebello Jun 01 '15 at 22:42

1 Answers1

2

You cannot use initializers with variable length arrays. Either switch to some sort of container:

std::vector<int> alunos(n);

or fill the array with zeros manually:

int alunos[n];
std::fill(alunos, alunos+n, 0);

Also, ignoring errors is unhealthy. Don't do it.

Community
  • 1
  • 1
isanae
  • 3,253
  • 1
  • 22
  • 47