Click here to Skip to main content
15,063,166 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys, I'm trying to write a code that checks if there exists -1 in a 2-dimensional array and if it does exits count's it, but if there are more than one -1 in one line it shouldn't count more than once. for example, there are two -1 in our line 1 of our array, so after checking and finding the first one it should skip that line and go and check the second line.

this is my code after reaching the first -1 it increases the value of j which is the number of lines, and the value of a which value contains our final result

What I have tried:

                                    //  counting the -1
for(int j=0;j<M;j++)
                                   // it should be done with a while loop
    {
        int i=0;
        while (i<N)
        {
            
            if (Play [j] [i] == -1)
            {
                a++;
                j++;
            }
     i++;
        }
       
    }
    
            cout<< a << endl;
Posted
Updated 10-Dec-17 11:57am

Looks like it is time to learn how to debug your code by yourself, luckily, your code is rather simple.
Test your code with
C++
Play [1][5] = -1;
Play [2][2] = -1;

and pay attention to which array elements are checked and which ones are not.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

In case debugger is too tough for you, you can change your code to track what it does.
C++
// initialize array with known value
for(int j=0;j<M;j++)
{
    for(int i=0;i<N;i++)
    {
        Play [j] [i] =0
    }
}// set a few -1
Play [1][5] = -1;
Play [1][6] = -1;
Play [2][2] = -1;
// then put your code with a single change
for(int j=0;j<M;j++)
    {
        int i=0;
        while (i<N)
        {
            if (Play [j] [i] == -1)
            {
                a++;
                j++;
            }
            Play [j] [i] = 2;
            i++;
        }
    }
    cout<< a << endl;
// then print the resulting array, every 0 is untested
   
v2
Try :

 for(int j=0; j < M; ++j )  // it should be done with a while loop
{
    int i=0;
    while( i < N )
    {
       if( Play [j] [i] == -1 )
       {
           a++;
           j++;
           break;       // skip out of inner loop
       }
       i++;
    }
}
cout<< a << endl;
   
Comments
Patrice T 10-Dec-17 19:05pm
   
I fear the 'break' is not enough to correct the problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900