This is a question when you define the initial expression of the c language for statement as an array.

Asked 2 years ago, Updated 2 years ago, 94 views

void dice(int count) {
    int i;

    if (n < count) {
        print(n);
        return;
    }

    for (i = arr[count - 1]; i <= 6; i++) {
        arr[count] = i;
        dice(count + 1);
    }
}

This is.

Looking at this code, count gave the parameter device(1) in the main function.

What I don't understand from this code is that if i is given as arr(count-1), does i mean that i increases the value by 1 from arr 0 and stores it? I don't understand

c loops for array

2022-09-21 13:50

1 Answers

for (i = arr[count - 1]; i <= 6; i++) {

Assign the count-1th element of arrr to i and increase i by 1 from the next iteration until 7.

Additional questions are asked, so I am revising my answer:

I didn't understand. I don't quite understand that i initializes to arr[count-1] and increases i. When I do a for statement, I only see the form i=0 or i=num; or i=num-1. You initialize the value of i with an array, but you don't increase i and put it in an array. So when you initialize it with an array, does it mean that the value in i remains unchanged and only the repetition door rotates 6 times?

for (i = arr[count - 1]; i <= 6; i++) {
    // a repeating sentence
}
int idx = arr[count - 1];
for (i = idx; i <= 6; i++) {
    // a repeating sentence
}

The execution results of the above two codes are the same.

To initialize i to arr[count-1] literally allocates a value in the arr array at arr-1... This is.

Since there is an increasing or decreasing expression i++, of course i increases by 1 at the end of each loop.


2022-09-21 13:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.