Search code examples
arrayscnested-loops

Printing array as sub blocks


I have array and I am trying to print this array as sub blocks, where each block has size = 5. the out put of this code not as I expected it just print the first 5 values. How to print the array as sub blocks?

int arr[298] = {some int values};
int in = 0;
int siz = 298;
int ii;
int rang = 5;
for (int i = 0; i < siz; i++) {
    if (in <= siz) {
        for (ii = in; ii < 5; ii++) {
            printf("arr=%d \n", arr[ii]);
        }
        printf("------------\n");
    }
    ind = ind + rang;
}

Solution

  • Following your request for clarification in the comment section, there are a few problems with your code, for me the biggest one is that it's needlessly complicated, but the one you are looking for is in this line:

    ind = ind + rang;
    

    ind is is not declared in your code but I assume you mean in, the first time the inner loop runs in(ind) is 0 so it all goes well, after that in will be 5, you assign it to ii and the condition ii < 5 will never be true again, the body of the loop will never be executed.

    I suppose you could fix it by using in as index for the array and scrap rang since it isn't needed, something like this:

    int arr[298] = {some int values};
    int in = 0;
    int siz = 298;
    
    for (int i = 0; i < siz; i++) {
        //if (in < siz) { moving this into the for loop
            for (int ii = 0; ii < 5 && in < siz; ii++, in++) {
                printf("arr=%d \n", arr[in]);
            }
            printf("------------\n");
        //}
    }
    

    Live demo: https://godbolt.org/z/YzG9sno1n

    But you don't need a nested loop, there are a few ways you can do this, a simple one is to have a variable that controls the block size:

    int arr[298] = {some int values};
    int siz = 298;   
    int count = 5;
    
    for (int i = 0; i < siz; i++) {
        
            printf("arr=%d \n", arr[i]);
            count--;
        
        if (count == 0) {
            printf("------------\n");
            count = 5;
        }
    }
    

    Live demo: https://godbolt.org/z/b4e8vWfhM

    In the above code count serves as the control variable, the value in the index is printed 5 times and when it reaches 0 a separator is printed and it resets and starts the new block.

    Another possible option is to use the index itself to separate the blocks, you know the remainder of a division is 0 when the numerator is divisible by the denominator, you can use that to your advantage:

    int arr[298] = {some int values};
    
    int siz = 298;
    
    for (int i = 0; i < siz; i++) {
        if (i % 5 == 0) { // && i != 0 if you want to skip the initial separator
            printf("------------\n");
        }
        printf("arr=%d \n", arr[i]);       
    }
    

    Live demo : https://godbolt.org/z/nne3z38rY

    Finally you can/should use a constant value for size:

    #include <stdio.h>
    #define SIZE 298
    
    int main() {
    
        int arr[SIZE] = {some int values};
    
        for (int i = 0; i < SIZE; i++) {
            if (i % 5 == 0  && i != 0) { // skipping the initial separator
                printf("------------\n");
            }
            printf("arr=%d \n", arr[i]);       
        }
    }
    

    Live demo: https://godbolt.org/z/Mc4Yh4cav