0
votes

I want to print feasible solutions by adding up the maximum combinations of 2 indexes for n indexes where no index’s value can be added to itself

Expected output for 4 indexes 1,2 1,3 1,4 2,1 2,3 2,4 3,1 3,2 3,4 4,1 4,2 4,3

Where I’m going wrong :: (Here’s my Code )

for(i=1;i<n;i++)
{
    for(j=1; j!=i && j<n; j++)
    {
        printf("%d,%d",i,j);
    }
}
6

6 Answers

2
votes

The error is you have specified j!=i because of which your inner loop will exit as soon as it encounters a condition where j is not equal to i and will not continue for further values.

You just want to skip the iteration when i == j for which you should use the continue statement.

for(i = 0; i < n; i++)
{ 
    for( j = 0; j < n; j++)
    {
        if(i==j) continue;
        printf("%d,%d", i,j)
    }
}
2
votes

The loop will terminate when j!=i if that is part of the condition. You want to check that within the loop and continue the loop afterward. Since the condition is false when i and j are equal, it will do nothing for that iteration and continue onto the next.

It also seems that you want <= instead of just < if you want to include n itself.

int n = 4;
for (int i = 1; i <= n; i++)
{
    for (int j = 1; j <= n; j++)
    {
        if (i != j)
        {
            printf("%d,%d ", i, j);
        }
    }
}
2
votes

Here is a solution that till now nobody has suggested.:)

#include <stdio.h>

int main(void) 
{
    unsigned int n = 5;

    for ( unsigned int i = 1; i < n; i++ )
    {
        for ( unsigned int j = 1; j < n; j++ )
        {
            ( void )( i != j && printf( "%d,%d ", i, j ) );
        }
    }

    return 0;
}

The program output is

1,2 1,3 1,4 2,1 2,3 2,4 3,1 3,2 3,4 4,1 4,2 4,3 

As for your code then this loop

for(j=1; j!=i && j<n; j++)

stops all iterations as soon as j == i. For example for i = 1 and j = 1 the loop will be entirely skipped.

1
votes

The error here is that you should not evaluate the i!=j condition as your for loop condition because this will lead to the termination of the loop. The condition needs to be checked inside the loop as a separate condition. You can use if statements to achieve this.

for(i = 1; i < n; i++){
    for(j = 1; j < n; j++){
        if(i != j)
            printf("%d,%d", i, j);
        }
    }
0
votes

You want to skip the loop when i == j, but your code stops the loop,

for(i=1;i<n;i++)
{
    for(j=1; j<n; j++)
    {
        if( i != j )
           printf(“%d,%d”,i,j);
   }
}
0
votes

you are checking condition in inner loop ie in j loop So you are checking condition j!=i in inner loop , because of these if condition get false then it will not executed the j loop ,

Dry run :-

Consider the value of n is 4 ...

first i=1 , i<=4 true , then it will goes into j loop , then j=1 and its check j!=i means 1!=1 , the condition false here , therefore it not execute j loop and goes again in i loop .....

int main(){

    int n;

    printf("\nENTER THE NUMBER OF INDEXES : ");
    scanf("%d",&n);

    for(int i=1 ; i<=n ; i++){

            for(int j=1 ; j<=n ; j++){

                    if(i!=j)
                            printf("%d %d    ",i,j);
            }
    }

    printf("\n");

    return 0;

}