-1
#include <iostream>
using namespace std;
int main()
{
    int n, d;
    cin >> n >> d;
    int a[101][21];
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= d; j++) {
            cin >> a[i][j];
        }
    }
    int cnt = 0;
    int output[101];
    bool s=false;
    for (int i = 1; i <= n; i++) {
    for (int j = 2; j <= d; j++) {
    if(a[i][j] == 1){
s = true;
}}
if (s) {
cnt++;
output[cnt]=i;
}
}
for(int i=1;i<=cnt;i++){
cout<<output[i]<<" ";
break;
}cout<<endl;
return 0;
}

I want to print the first column which contains only 1 and not 0 like:

4 6
0 1 1 0 0 1
1 1 1 0 1 1
1 0 1 0 0 1
1 0 1 1 1 1

the output is 3 because 3rd column is the first column which is not 0, but my code is only printing 1. Can someone find the bug???

  • `a[i][j] == 1 && a[i][j] != 0` is redundant – if the value *is* 1 then it is automatically not 0 – and if it is *not*, then first check fails already, no matter if second would or not, so entire expression fails... – Aconcagua Apr 21 '22 at 11:05
  • 1
    Side note: About [`using namespace std`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... – Aconcagua Apr 21 '22 at 11:06
  • *'first column which is 1 and not 0'* – pretty unclear wording, that could mean *anything* – only from given example we can guess that you actually mean *'first which consists of all ones'* or *'only contains ones'*, maybe you might want to rephrase... – Aconcagua Apr 21 '22 at 11:10

1 Answers1

0

Your actual problem is that you start inner loop at index 2, which means you do not consider the values at first row at all:

for (int i = 1; i <= n; i++)
{
    for (int j = 2; j <= d; j++)
    //           ^  !!!
    {

The variable s is placed at the wrong position, and it needs to be initialised to true and set to false as soon as you find a zero value; you can shortcut the loop if you find one, by the way:

for(i = 1; ...)
{
    s = true;
    for(j = 1; ...)
    {
        if(a[i][j] != 0) // == 1: handle *any other* value as 0
                         // != 0: handle *any other* value as 1
        {
            s = false;
            // cannot get true any more, so:
            break; // mentioned short cut
        }
    }
    if(s)
    {
        // print i
        break; // another shortcut; you don't want to print further lines, do you?
    }
}

Initialising to true and setting to false means that any zero value leads to a total false; the other way round would mean any one-value leads to true, i.e. a single one in the column suffices...

Yet a final note: C++ has zero-based indices, so you usually index within the range [0; size of array), e.g.

int array[7];
array[0] = 12; // first valid index
array[6] = 10; // last valid index

for(size_t i = 0; i < 7; ++i) // iterate over the entire array
// though you usually let the compiler calculate the size
// (or use a constant for) e.g.
for(size_t i = 0; i < sizeof(array)/sizeof(*array); ++i)

Your code is not wrong in that sense, just that you are wasting first row and column of your matrix as not using it – as long as you make sure that n remains smaller than 101 (n < sizeof(a)/sizeof(*a)) and d smaller than 21 (d < sizeof(*a)/sizeof(**a)). You didn't add any size checks, though; if you do so, they should consider that.

If you switch to zero-based indices then you simply start iterating from 0 up to but not including n:

for(size_t i = 0; i < n; ++i)

Now you are using the entire array. That's the usual way, and it is superior for not wasting memory. If you apply size checks, then now n and d can remain smaller or equal to the respective dimension sizes (n <= .../d <= ...).

You still can produce one-based output, just do all internal indexing zero-based and when doing the output, add 1 to the index (and generally on input, subtract 1, if it refers to a specific index, but you don't have that case in your code).

Edit – referring to the question's title:

While your original example looked for all one columns, which the answer referred to so far, then when looking for a column with all equal values – no matter which values they contain (e. g. all being 77), then you need to change your loop (to be precise: revert to original variant, but with changed comparison):

for(i = 1; ...) // or i = 0, if you switch to zero-based indexing as recommended!
{
    s = true;
    for(i = 2; ...) // now indeed one index more! 1, if zero-based!
    {
        if(a[i][j] == a[i][1]) // or [i][0] if zero-based!
        //            ^^^^^^^ (!)
        {
            s = true;
            break;
        }
    }
    // ...
}

Now you compare any subsequent element in the column to the first one! Note that again the first index is skipped in the iteration, but it re-occurs in the comparison inside!

Aconcagua
  • 24,880
  • 4
  • 34
  • 59