0
0
Cprogramming~10 mins

Return inside loops in C - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to return the first even number from the array.

C
int first_even(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        if (arr[i] % 2 == 0) {
            return [1];
        }
    }
    return -1;
}
Drag options to blanks, or click blank then click option'
Aarr[i]
Bi
Csize
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the index instead of the value.
Returning a constant like 0 instead of the found number.
2fill in blank
medium

Complete the code to return the index of the first negative number in the array.

C
int first_negative_index(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        if (arr[i] < 0) {
            return [1];
        }
    }
    return -1;
}
Drag options to blanks, or click blank then click option'
A-1
Bi
Csize
Darr[i]
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the value instead of the index.
Returning the array size or a constant.
3fill in blank
hard

Fix the error in the code to return the sum of positive numbers, stopping early if a zero is found.

C
int sum_positive(int arr[], int size) {
    int sum = 0;
    for (int i = 0; i < size; i++) {
        if (arr[i] == 0) {
            [1];
        }
        if (arr[i] > 0) {
            sum += arr[i];
        }
    }
    return sum;
}
Drag options to blanks, or click blank then click option'
Abreak
Bcontinue
Creturn sum
Dreturn 0
Attempts:
3 left
💡 Hint
Common Mistakes
Using break instead of return, which only exits the loop but not the function.
Using continue, which skips the current iteration but does not stop.
4fill in blank
hard

Fill both blanks to return the first number greater than 10 and its index.

C
int* first_greater_than_ten(int arr[], int size) {
    static int result[2];
    for (int i = 0; i < size; i++) {
        if (arr[i] [1] 10) {
            result[0] = arr[i];
            result[1] = [2];
            return result;
        }
    }
    return NULL;
}
Drag options to blanks, or click blank then click option'
A>
B<
Ci
Darr[i]
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong comparison operator.
Storing the value instead of the index in the second result element.
5fill in blank
hard

Fill all three blanks to return the first negative number, its index, and stop the loop.

C
int find_negative(int arr[], int size, int *index) {
    for (int i = 0; i < size; i++) {
        if (arr[i] [1] 0) {
            *index = [2];
            [3] arr[i];
        }
    }
    *index = -1;
    return 0;
}
Drag options to blanks, or click blank then click option'
A<
Bi
Creturn
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators.
Not returning immediately, causing wrong results.