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.
i2? Perhaps a typo in the line:i1 = i; j2 = j;i1 = i; j2 = j;- Grundy