Determine the number of elements between the first and last zero elements, that is, between the first zero and the last zero in the heap array. The following code runs without error, but the result is not correct.
#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;
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()%200-100; cout<<m[i][j]<<"\t"; }
cout<<endl; }
cout<<endl;
for (i=0; i<r; i++) {
for (j=0; j<c; j++) {
if (m[i][j]==0)
break;
else
count++;
if (j!=c)
break; } }
if (i!=r && j!=c) {
for (i=r-1; i>=0; i--) {
for (j=c-1; j>=0; j--) {
if (m[i][j]==0)
break;
else
count++;
if (j!=c)
break; } } }
cout<<"\n Total of elements between the first and last zero element: " <<r*c-count<<endl;
delete []m;
_getch();
}
As a first advice I would recommend you not to use
conio.h
. It is a non-standard library and of doubtful utility in real developments.On the other hand, when you do
delete []m;
you have to be aware that you are not deleting anything other thanm
, the size reservesc
are being left in limbo. The general rule is that for everynew
you have to include adelete
and in your code there are twonew
and a uniquedelete
.In my opinion (and this is a totally personal opinion) the way you tabulate the code makes it quite difficult to read. That the closing braces are grouped at the end of the line forces you, for example, to have to count how many there are to know which is the next instruction to execute... the fact of concatenating several instructions in the same line without meaning it also doesn't help make the code readable.
As for variables, you don't need to reuse them throughout the program. You can create the variables inside the loops. The advantage of doing it this way is that you reduce the scope of the variable and improve the readability of the program (if a variable ceases to exist it is no longer a problem).
The recommendations are just that, recommendations, they are not mandatory but they never hurt and they can help you make fewer mistakes in the future.
A detail that particularly annoys me is that of calculating the distance of two elements of an array. You said array , so I'm afraid you don't have to create an array but a vector of elements. If my suspicions are true, the algorithm will be greatly simplified. If, on the other hand, it turns out that you have to create a matrix, it is not clear to me why you understand that the distance between two elements involves linearizing the matrix as if it were a vector.
Ok, let's review the first loop of your search algorithm:
Note that it
break
only leaves one loop and you have two nested ones. Upon reachingbreak
, execution will exit the loopj
and continue executing the loopi
, eventually entering the loop againj
. In the second loop the exact same thing happens.If the idea is to search for two positions within a vector (and you're treating the array as a vector) I'd rather handle a vector directly.
Lastly, the search for the first and last feature elements X could be encapsulated in a function, avoiding duplicate code. To do this part there are several options, although perhaps the simplest is a function that returns the next position of a value given a starting position and -1 if there are no more values. So you ask for the first value and then you ask for the second in a loop until the function returns -1, the value that it gave you the previous time (and that you have to memorize) is the position of the last '0' in the list. Something with a signature such that:
All the best.
As eferion mentions, there are bugs in the search algorithm
You can use this code to find both zeros in two dimensional array
If you find a 0, you have to see if it corresponds to the first or the second to assign the variables i1 and j1 or i2 and j2 as appropriate, however if it is the first case you have to continue to continue looking for the second. If the second 0 is found we can exit both
for
using agoto
and alabel
Finally, to calculate how many elements there are between the two zeros, we do a simple mathematical transformation.
If we convert the two-dimensional array to a one-dimensional array, the new position in the array is given by the following expression
In your case it would be:
Then we just need a simple subtraction to calculate the distance between these two positions which gives us one more than the number of positions between them.
I leave you the complete code: