В двумерном массиве определить к-во элементов между первым и последним нулевыми элементами.
#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();
}
Программа компилируется, но ответ не правильно когда есть два нуля. Пожалуйста помогите.
i2
? Возможно опечатка в строке:i1 = i; j2 = j;