Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
for (i=0; i < n; i++){
for (j=0; j < n; j++){
if (x[i][j]!=0)
goto reject;
}
printf(“First row that satisfies the condition is %d\n”, i);
break;
reject:
}

What I have tried:

actually i don understand this
Posted
Updated 21-Jun-20 9:36am

Quote:
actually i don understand this

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
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 know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
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[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
-----
Advice: Learn to indent properly your code, it show its structure (it makes the structure visual) and it helps reading and understanding. It also helps spotting structures mistakes.
C++
for (i=0; i < n; i++){
	for (j=0; j < n; j++){
		if (x[i][j]!=0)
			goto reject;
	}
	printf("First row that satisfies the condition is %d\n", i);
	break;
	reject:
}

Indentation style - Wikipedia[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
 
Share this answer
 
This is likely an assignment, which is why we cannot help you.

A very easy explanation, however, I can provide you.

In the code above, you have two loops, and an array (x). You are checking for values, and if the value at those 2 indexes is not equal to zero, then you are using goto expression to reach a label (reject).

Secondly, the label reject simply reaches the end of the function, which makes the function return the control. What other language structure in C would make the function return the control? Use that in place of goto reject;.

If you are still facing problems, please discuss this with your teacher and check the notes they provided.
 
Share this answer
 
v2
Comments
Member 14869002 21-Jun-20 6:11am    
thank u for helping but I'm still getting errors
#include <stdio.h>
int main()
{
int a[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9};

for (int i=0; i < 3; i++){
for (int j=0; j < 4; j++){
if (a[i][j]!=0)
goto reject;
}
printf(“First row that satisfies the condition is %d\n”, i);
break;
reject:
}
}
We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
 
Share this answer
 
First, start by indenting the code. It is much easier to understand this way.
C++
for( i = 0; i < n; i++ )
{
    for( j = 0; j < n; j++ )
    {
        if( x[i][j] != 0 )
            goto reject;
    }

    printf( "First row that satisfies the condition is %d\n", i );
    break;

    reject:
}
Next, think about what the goto statement does in this code. It does two things - it breaks out of the for loop and it skips over the printf statement and this is done only if a non-zero value is found. Here is one way that can be done without a goto :
C++
bool found;  // if bool, true, or false are not defined then define them
for( i = 0; i < n; i++ )
{
    found = false;
    for( j = 0; j < n; j++ )
    {
        if( x[i][j] != 0 )
        {
            found = true;
            break;        // goto reject;
        }
    }

    if( ! found )
    {
        printf( "First row that satisfies the condition is %d\n", i );
        break;
    }
    // reject:
}
I recommend reading the documentation on the break statement so you understand it fully. Also read about its relative - continue, and the inner workings of for, do, and while loops.
 
Share this answer
 

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