In a two-dimensional array, define the number of elements between the first and last zero elements.

#include <iostream> #include <ctime> #include <cstdlib> #include <conio.h> using namespace std; int main() { int r,c, i, j, count=0; cout << "\n Input number of rows: "; cin >> r; cout << " Input number of columns: "; cin >> c; cout << endl; srand(time(NULL)); int **m = new int* [r]; for (int i=0; i<r; i++) { m[i] = new int[c]; } for (i=0; i<r; i++) { m[i] = new int[c]; for (j=0; j<c; j++) { m[i][j] = rand()%100-0; cout << " " << m[i][j] << "\t"; } cout << endl; } cout << endl; int i1 = -1, j1 = -1, i2 = -1, j2 = -1; bool foundTwoZeros = false; for (i=0; i<r; i++) { for (j=0; j<c; j++) { if (m[i][j]==0) if (i1 == -1 && j1 == -1) { i1 = i; j1 = j; } else { i1 = i; j2 = j; foundTwoZeros = true; goto foundTwoZeros; } } } foundTwoZeros: if (foundTwoZeros) { int pos1 = c * i1 + j1; int pos2 = c * i2 + j2; count = pos2 - pos1 - 1; cout << "\n Total of elements between the first and last zero element: " << count << endl; } else { cout << "\n There are not two zero elements"; } for (i=0; i<r; i++) { delete [] m[i]; } delete []m; _getch(); } 

The program is compiled, but the answer is not correct when there are two zeros. Please, help.

  • Where do you get i2 ? Perhaps a typo in the line: i1 = i; j2 = j; i1 = i; j2 = j; - Grundy
  • It is probably easiest to find the first one first (as you do, moving from the beginning to the end), and then the last (in similar nested cycles, but moving from the end to the beginning). By the way, if the first is not found, then the second can no longer be sought. And note that the first and the last can match if there is exactly one zero element in the array. - avp

2 answers 2

Let's start with the little things - you allocate memory for lines twice:

 for (int i=0; i<r; i++) { m[i] = new int[c]; } 

and then, right away -

 for (i=0; i<r; i++) { m[i] = new int[c]; 

Then here

  i1 = i; j2 = j; 

obviously i2 = i;

And I also highly recommend getting rid of goto - for example, like this:

 for (i=0; i<r; i++) { for (j=0; j<c; j++) { if (m[i][j]==0) { if (i1 == -1 && j1 == -1) { i1 = i; j1 = j; } else { i2 = i; j2 = j; foundTwoZeros = true; break; //##### } } } if (foundTwoZeros) break; //##### } 
  • No need to leave the loop. By the condition of the problem a person needs the last zero. - Zealint
  • @Zealint Look at his cycle - he has a way out of the cycle. My code is equivalent to the author's code, just without goto. Is this right - this is the second question :) - Harry
  • @Harry Thanks, here "i1 = i; j2 = j;" there was an error, but a problem arises, if there are three zeros, the code will only determine the number of elements between the first and second zero elements. - Neon
  • @ Nas_04 And @Zeliant answered you this - do not leave the cycle prematurely, as soon as you find two zeros. Search to the end. In the code I proposed in the response, remove the lines marked //##### - Harry

Of course, no one will write the program for you. I can only point out a number of obvious errors, I think by correcting them, get a working code.

  1. For some reason, your memory is allocated twice, be careful.
  2. You made a mistake in the else block inside a double loop. First, there you need to assign i2, not i1. Secondly, it is impossible to leave the block, because you are only looking for the second zero, and by condition you need to look for the last (there may be 3 or more).

General recommendations, if only learn to program - never copy the code. However, even experienced programmers are mistaken in trying to save these 5 seconds of time.