Lecture Reviewer
Grouped by lecture — Arrays · Pointers · Recursion
Arrays
What is an Array?
An array is a collection of data elements that are all the same type, stored consecutively in memory. Arrays provide a way to name a collection and reference its individual elements — they behave like a list of variables with a uniform naming mechanism.
Examples: a collection of integers, doubles, or characters.
Array Declaration
Syntax:
<type> <arrayName>[<dimension>];<dimension> must be an integer constant or constant expression.int score[5]; // array of 5 integers
int A[10]; // array of 10 uninitialized integers
char B[80]; // array of 80 characters
double C[800]; // array of 800 doubles
Subscripting / Indexing
Individual variables that make up an array are called: indexed variables, subscripted variables, or elements.
The first element always has index
0. The last element has index dimension - 1.int A[10];
A[3] = 1; // assign value 1 to index 3
int x = A[3]; // read element at index 3
int i=7, j=2, k=4;
A[0] = 1;
A[i] = 5; // A[7] = 5
A[j] = A[i] + 3; // A[2] = 8
A[j+1] = A[i] + A[0]; // A[3] = 6
Array Initialization
Fewer values than size? Remaining positions auto-set to zero (0).
Omit size? Compiler sets minimum size needed for initializers.
int c[] = {6, 12, 16}; // size = 3 automatically
int d[5] = {1, 2}; // d[2], d[3], d[4] = 0
2-D Arrays
Declaration:
int A[rows][cols]; — e.g., int A[3][10]; creates 30 integers.Access with two subscripts:
A[row][col]. Manipulated using nested for-loops.char A[3][10];
A[1][2] = 'a'; // row 1, column 2
char c = A[1][2]; // c = 'a'
Searching Arrays
| Method | Array Type | How it Works |
|---|---|---|
| Unordered Linear | Unsorted | Scans every element. Returns index or -1. |
| Ordered Linear | Sorted | Stops early if element exceeds target. |
| Binary Search | Sorted (required) | Checks middle; eliminates half each step. |
// Binary Search
Set lower = 0, upper = size-1
while (lower <= upper AND not found):
m = (lower + upper) / 2
if A[m] == searchValue → FOUND at m
if A[m] > searchValue → upper = m - 1
else → lower = m + 1
Binary search requires a sorted array.
Sorting Arrays
Four sorting methods: Selection Sort, Bubble Sort, Shell Sort, Quick Sort.
Selection Sort — repeatedly finds the largest element in the unsorted portion and swaps it to the end.
Pointers
Memory Addresses
Every variable has a memory address. The
& operator yields the address. Print with %p.char ch = 'A';
printf("%p", &ch); // prints address of ch
What is a Pointer?
A pointer is a variable that stores the memory address of another variable.
char* cPtr; // pointer to char
int *numPtr; // pointer to int
float * xPtr; // pointer to float
Always initialize pointers — an uninitialized pointer holds a garbage address.
int *numPtr = NULL; // safe initialization
4 Steps to Using Pointers
Step 1 — Declare the variable:
int num;Step 2 — Declare the pointer:
int *numPtr = NULL;Step 3 — Assign the address:
numPtr = #Step 4 — Dereference to use:
*numPtr = 42;int num;
int *numPtr = NULL;
numPtr = #
*numPtr = 42;
printf("%d", num); // prints 42
Pass by Value vs. Reference
❌ Pass by Value
Function receives a copy. Changes do NOT affect original.
✅ Pass by Reference
Function receives the address. Changes DO affect original.
// GOOD: swap by reference
void swap(int* a, int* b) {
int tmp = *a; *a = *b; *b = tmp;
}
swap(&x, &y); // x and y actually swapped
Recursion
What is a Recursive Function?
A recursive function is one that calls itself. Requires a base case to stop.
void message(int times) {
if (times > 0) {
printf("This is a recursive function.\n");
message(times - 1);
}
}
Without a base case → stack overflow / infinite recursion.
Depth of Recursion
Each call creates a new instance of local variables on the call stack.
For
message(5): depth = 6 (calls for times = 5, 4, 3, 2, 1, 0).Classic Example — Recursive Factorial
int factorial(int num) {
if (num > 0)
return num * factorial(num - 1);
else
return 1; // base case: 0! = 1
}
// factorial(4) = 4 × 3 × 2 × 1 = 24
Direct vs. Indirect Recursion
Direct
Function calls itself directly. void f() { f(); }
Indirect
A calls B, B calls A back. void A(){B();} void B(){A();}
Laboratory Exercises
Based on your submitted lab files · run & practice C code in-browser
🖥️ In-Browser C Code Runner
Each exercise below has a code editor where you can write and run actual C code. Type your solution, provide sample input, and click ▶ Run to execute. Use Hint if you're stuck, or Solution to see the answer. Your submitted lab files are shown at the bottom as reference.
🔹 Arrays Exercises
Arrays
Easy
A1 · Even & Odd Counter
Your submitted lab · count even and odd numbers in an array
Task: Ask the user to enter
This is your submitted
7 integers. Count how many are even and how many are odd, then print both counts.This is your submitted
Arrays.c — try modifying it below!
Sample Input
1 2 3 4 5 6 7
Expected Output
Even numbers: 3 Odd numbers: 4
1
📥 Input
space or newline separated
Output
Click ▶ Run to execute your code...
Use the modulo operator
% to check if a number is even (num % 2 == 0) or odd (num % 2 != 0). Use two counter variables and a for-loop.
Arrays
Easy
A2 · Array Sum & Average
Compute the total and average of elements in an array
Task: Read
5 integers from the user into an array. Compute and print the sum and the average (as a decimal). Format average to 2 decimal places.
Sample Input
10 20 30 40 50
Expected Output
Sum: 150 Average: 30.00
1
📥 Input
Output
Click ▶ Run to execute your code...
Accumulate the sum in a variable inside the for-loop. Compute average as
sum / 5.0 (use .0 to force floating-point division). Use %.2f to print 2 decimal places.
Arrays
Easy
A3 · Reverse Print an Array
Print elements of an array in reverse order
Task: Read
6 integers into an array and print them in reverse order, each on its own line.
Sample Input
1 2 3 4 5 6
Expected Output
6 5 4 3 2 1
1
📥 Input
Output
Click ▶ Run to execute your code...
After filling the array with a forward loop
for(i=0; i<6; i++), use a backward loop for(i=5; i>=0; i--) to print in reverse.
Arrays
Medium
A4 · Find Maximum Element
Search an array for the largest value
Task: Read
8 integers. Find and print the maximum value and its index.
Sample Input
3 7 1 9 5 2 8 4
Expected Output
Max value: 9 Found at index: 3
1
📥 Input
Output
Click ▶ Run to execute your code...
Initialize
max = arr[0] and maxIdx = 0. Loop from index 1. If arr[i] > max, update both max and maxIdx.🔸 Pointers Exercises
Pointers
Easy
P1 · Pointer Basics
Your submitted lab · address, value, and modification via pointer
Task: Declare
int value = 24, create a pointer to it, print the address and original value, then change the value to 51 through the pointer and print the new value. This is your submitted Pointers.c.
No Input Required
(no stdin needed)
Expected Output
Address of value: 0xffe4a0 Original value: 24 Value after modification: 51* Address shown as simulated in browser runner
1
📥 Input
Output
Click ▶ Run to execute...
Use
int *p = NULL; to declare, p = &value; to assign the address, *p to dereference (read/write the value). Print address with %p.
Pointers
Medium
P2 · Swap Two Numbers Using Pointers
Classic swap via pass-by-reference
Task: Write a
swap function that takes two int* parameters and swaps the values. Call it from main() to swap two numbers entered by the user.
Sample Input
5 10
Expected Output
Before: a=5, b=10 After swap: a=10, b=5
1
📥 Input
Output
Click ▶ Run to execute...
Inside
swap: int tmp = *a; *a = *b; *b = tmp; — use the dereference operator to actually modify the values at the addresses.
Pointers
Hard
P3 · Pointer Arithmetic on Arrays
Traverse an array using only pointer arithmetic
Task: Declare
int arr[5] = {10, 20, 30, 40, 50};. Using a pointer and pointer arithmetic only (no subscript []), print each element and its address.
No Input Required
(values hardcoded)
Expected Output
Element: 10 Address: 0xffe4a0 Element: 20 Address: 0xffe4a4 Element: 30 Address: 0xffe4a8 Element: 40 Address: 0xffe4ac Element: 50 Address: 0xffe4b0* Addresses simulated in browser runner
1
📥 Input
Output
Click ▶ Run to execute...
Use a for-loop:
for(int i=0; i<5; i++, p++). Access value with *p and address with p. Remember: p++ moves the pointer to the next integer in memory (4 bytes ahead).🌀 Recursion Exercises
Recursion
Easy
R1 · Factorial
Your submitted lab · compute n! using recursion
Task: Compute
n! recursively. Base case: factorial(0) = factorial(1) = 1. This is your submitted Recursion.c.
Sample Input
5
Expected Output
Factorial of 5 is 120
1
📥 Input
Output
Click ▶ Run to execute...
The base case
n == 0 || n == 1 returns 1. The recursive case returns n * factorial(n-1). Try tracing: 5 × 4 × 3 × 2 × 1 = 120.
Recursion
Easy
R2 · Sum of N Natural Numbers
Add 1+2+3+...+n using recursion
Task: Write a recursive function
sumN(int n) that returns the sum 1 + 2 + 3 + ... + n. Base case: sumN(0) = 0.
Sample Input
10
Expected Output
Sum of 1 to 10 = 55
1
📥 Input
Output
Click ▶ Run to execute...
Base case:
if (n == 0) return 0;. Recursive case: return n + sumN(n - 1);. Similar structure to factorial, but addition instead of multiplication.
Recursion
Medium
R3 · Fibonacci Sequence
Generate Fibonacci numbers recursively
Task: Write a recursive function
fib(int n). Base cases: fib(0) = 0, fib(1) = 1. Recursive: fib(n) = fib(n-1) + fib(n-2). Print the nth Fibonacci number.
Sample Input
7
Expected Output
Fibonacci(7) = 13
1
📥 Input
Output
Click ▶ Run to execute...
Two base cases:
if (n==0) return 0; if (n==1) return 1;. Then: return fib(n-1) + fib(n-2);. Sequence: 0 1 1 2 3 5 8 13 ... so fib(7) = 13.
Recursion
Hard
R4 · Power Function (x^n)
Compute x raised to n using recursion
Task: Write a recursive function
power(int base, int exp) that returns base^exp. Base case: power(x, 0) = 1. Recursive: power(x, n) = x * power(x, n-1).
Sample Input
2 8
Expected Output
2 ^ 8 = 256
1
📥 Input
Output
Click ▶ Run to execute...
Base case:
if (exp == 0) return 1;. Recursive: return base * power(base, exp - 1);. Think of it as multiplying base by itself exp times.Identification
35 items — click to reveal answer
Find the Error & Correct It
15 items — spot the bug, then reveal fix
True or False
30 statements · click badge to reveal
Multiple Choice
30 items · click option to check
Key Terms & Concepts
arrays · pointers · recursion · errors