Loops & Flow Control
Loop Types
Random Numbers
Arrays
Array Indexing and Logic
100

What is the purpose of a loop in C?

To repeat a block of code multiple times until a condition is met or no longer true.

100

What is the syntax of a for loop?

for (initialization; condition; increment) { /* code */ }

100

What library must be included to use rand() and srand()?

#include <stdlib.h>

100

What is the difference between an array index and an element?

The index is the position number; the element is the value stored at that position.

100

What happens if you access an array element beyond its size?

Undefined behavior: it may crash or give garbage values.

200

What does break; do in a loop?

It exits the loop immediately.

200

What is a pre-test loop? Give an example using while.

A loop that checks the condition before running.

200

How do you seed the random number generator with the current time and what library do you need to include?

srand(time(NULL));

requires #include <time.h>

200

Declare and initialize a 1D array of 5 integers in C.

int arr[5] = {1, 2, 3, 4, 5};

200

Write a loop to copy values from one array to another (size 5).

for (int i = 0; i < 5; i++) {

      b[i] = a[i]; }

300

What does continue; do in a loop?

It skips the rest of the loop body and moves to the next iteration.

300

What is a post-test loop? Give an example using do...while.

A loop that runs first and checks condition after.

300

What does rand() % 10 return?

A random number between 0 and 9.

300

Use a loop to fill a 10-element array with random numbers.

for (int i = 0; i < 10; i++){

       arr[i] = rand() % 100 + 1; }

300

Write a loop to reverse a 1D array in-place.

for (int i = 0; i < n / 2; i++) { 

      int temp = a[i]; 

      a[i] = a[n-1-i];

      a[n-1-i] = temp; }

400

What does return; do inside a function?

It exits the function and optionally returns a value.

400

Write a while loop that counts down from 10 to 1.

int i = 10; while (i >= 1) { printf("%d\n", i); i--; }

400

Write code to generate a random number between 20 and 50.

int num = rand() % 31 + 20;

400

Declare a 2D array with 3 rows and 4 columns.

int arr[3][4] = {0};

400

Explain the difference between element position and index.

Index starts from 0; position is index + 1.

500

What does this loop output?

for (int i = 0; i < 5; i++) { 

      for (int j = 0; j < 5; j++) {

            printf("* "); }     

printf("\n"); }

500

What is an infinite loop? Write one using for.

A loop that never ends.
for (;;) { /* endless */ }

500

Write a loop that prints 5 random numbers from 1 to 100.

srand(time(NULL));

for (int i = 0; i < 5; i++) { 

printf("%d\n", rand() % 100 + 1); }

500

Write a nested loop to print all elements of a 3x3 array.

for (int i = 0; i < 3; i++) { 

      for (int j = 0; j < 3; j++) {

            printf("%d ", arr[i][j]); }

      printf("\n"); }

500

Copy values from a[3][3] to b[3][3] using nested loops.

for (int i = 0; i < 3; i++) {

      for (int j = 0; j < 3; j++) {

           b[i][j] = a[i][j]; } }