Sina.Mh
Sina.Mh

Reputation: 1

why in my code second "for" in function loop only once

i wrote this nested "for" program but second loop only work once and print one row of "x"

#include <stdio.h>

void draw_box(int , int);

int main(int argc, char **argv)
{
    draw_box(8 , 35);
    return 0;
}

void draw_box(int row , int column)
{
    for(;row>0;row--)
    {
        for(;column>0;column--)
            printf("X");

      printf("\n");      
    }
}

Upvotes: 0

Views: 525

Answers (3)

user6119874
user6119874

Reputation: 105

When you are using nested for loops

for(;row>0;row--)
{
    for(;column>0;column--)
        printf("X");     
}

For each value of row , you are iterating through entire column.
So, X is printed the number of column times.
But value of column is reduced to zero each time you iterate through outer loop.

For clarification consider inner loop :
If value of column is 5,
X is printed and the value of column reduces to 4.
Similarly, X will be printed 5 times and at that moment, value of column reduces to zero

Now, for the second iteration of outer loop, value of column remains zero and it will not pass the inner loop condition. Thus your pointer will not move inside the second loop to print X .

How to overcome this : Use loop-variable.
Now every time you iterate through inner loop, value of loop-variable will be reduced and not of column.
So, while iterating through through outer loop second time, value of loop-variable will be restored to value of column and then X could be printed column number of times(in this case 5).

Solution :

for(i = 0; i < row; i++){           //Good habit to start from 0, as it is
    for(j = 0; j < column; j++){   //useful when you are working with matrix
        printf("X");
    }
    printf("\n");
}

Upvotes: 0

Govind Madhu
Govind Madhu

Reputation: 192

The problem here is that, your second loop iterates to zero and was never rest to its original value. You can either assign the value to a temporary variable and use that for iteration, and at the closing of the second "for" loop, set the temp value back to the default value OR You could change the for loop structure by assigning a variable j=0, and iterating it over and over again until it does not satisfy the condition j

The first for loop does not face this problem because, when you enter the firs for loop, the second for loop is executed "column" number of times before the control goes back to the first for loop.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726599

The second loop functions only once because it runs the value of column into zero, and never resets it back to the original value passed in as a parameter:

for(;row>0;row--) {
    int origColumn = column; // Save the value
    for(;column>0;column--)
        printf("X");
    column = origColumn;     // Restore the value
    printf("\n");      
}

This illustrates why one should be very careful when modifying values passed to your function as parameters. Your function would be as easy to write and easier to read with loop variables:

for(int r = 0 ; r != row ; r++) {
    for(int c = 0 ; c != column ; c++)
        printf("X");
    printf("\n");
}

Upvotes: 9

Related Questions