📦 Session 11 Homework: Arrays

Storing Many Values – Practice

Use arrays to store multiple values under one name. Remember: index starts at 0!

Quick reminder

Declare: int arr[5]; — 5 integers, indices 0 to 4.

Use in loop: for (i = 0; i < 5; i++) — never use i <= 5 for arr[5].

Read: scanf("%d", &arr[i]); — don't forget &.

Missions

Mission 1: Read 5, print 5

Declare an array of 5 integers. Use a for loop to read 5 numbers from the user into the array. Then use another for loop to print them in one line.

Loop: i from 0 to 4. scanf with &arr[i].

Mission 2: Sum and average

Use an array of size 10. Fill it (from user or with initializer). Compute the sum of all elements, then print the average (sum / 10.0).

Mission 3: Find max and min

Take an array of 5 numbers. Find the largest and smallest value. Print: "Max = ... Min = ...".

Set max = arr[0], then loop from i=1; if arr[i] > max then max = arr[i]. Same idea for min.

Mission 4: Marks of N students

Ask the user how many students (e.g. N = 5 or 10). Declare an array of that size. Read N marks, compute and print the average. (Like we did in the session for 50 students!)

Back to course