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!