PSUC Lab 7 — Contiguous Array Memory
Active Logic: 1-Dimensional Arrays
Master 1-D array allocation, zero-based indexing (arr[0] to arr[N-1]), linear traversal, min/max extraction, odd-index filtering, sorted check, and bubble sorting.
1-D Array Memory Rules
| Concept | Rule / Formula | Key Trap / Pitfall |
|---|---|---|
| Indexing | 0 to N - 1 | arr[N] is Out-of-Bounds memory error! |
| Element Address | Base + i * sizeof(type) | Elements sit in contiguous adjacent bytes |
| Min/Max Init | min = arr[0], max = arr[0] | Initializing max = 0 fails for negative numbers! |
| Step Loop | i = 1; i < N; i += 2 | Traverses only odd index elements |
🎯 Learning Philosophy: Active Logic Building
Test your 1-D array logic across 3 Interactive Learning Modes:
- 🧠Tab 1 (Logic & Hints): Index bounds & traversal formulas.
- 🧩 Tab 2 (Logic Puzzle): Fill in missing array indices & test live visualizers!
- 💻 Tab 3 (C Solution): Reference verified C code solutions.
Solve 1-D array puzzles before inspecting C source code!
Exercise 1 of 5
Largest & Smallest Element in 1-D Array
Finds the maximum and minimum values along with their 0-based index positions by traversing the array.
Min / Max Initialization
- Initialize:
max = arr[0], min = arr[0] - Loop
i = 1 to N-1: - If
arr[i] > max→max = arr[i]; maxIdx = i; - If
arr[i] < min→min = arr[i]; minIdx = i;
Interactive Logic Hints
💡 Hint 1: Why initialize max = arr[0] instead of max = 0? ▼
If all array elements are negative (e.g. -15, -40, -8),
max = 0 would give WRONG answer 0! arr[0] is safe!🧩 Complete Min/Max Traversal Logic
int max = arr[];
for (i = 1; i < n; i++) {
if (arr[i] max) {
max = arr[];
}
}
for (i = 1; i < n; i++) {
if (arr[i] max) {
max = arr[];
}
}
Select statements above and click Check!
📊 Live Array Min / Max Visualizer
Result...
min_max_array.cReference C Code
#include <stdio.h>
int main() {
int arr[] = {45, 12, 89, 3, 67, 24};
int n = 6, i;
int min = arr[0], max = arr[0];
int minIdx = 0, maxIdx = 0;
for (i = 1; i < n; i++) {
if (arr[i] > max) { max = arr[i]; maxIdx = i; }
if (arr[i] < min) { min = arr[i]; minIdx = i; }
}
printf("Largest = %d at index %d\n", max, maxIdx);
printf("Smallest = %d at index %d\n", min, minIdx);
return 0;
}
Exercise 2 of 5
Sum of Elements at Odd Index Positions
Traverses array elements located at odd indices (index = 1, 3, 5...) and calculates running sum.
Step-2 Index Loop
- Odd indices:
i = 1, 3, 5, 7... - Efficient Loop:
for (i = 1; i < n; i += 2) - Sum accumulation:
sum += arr[i]
Interactive Logic Hints
💡 Hint 1: What is the difference between odd element vs odd index? ▼
Odd Index is element POSITION (
arr[1], arr[3]). Odd element is VALUE (arr[i] % 2 != 0). Here we filter by POSITION!🧩 Complete Odd-Index Step Loop
int sum = 0;
for (i = ; i < n; i ) {
sum += arr[];
}
for (i = ; i < n; i ) {
sum += arr[];
}
Select statements above and click Check!
âš¡ Odd Index Elements Visualizer
Result...
odd_index_sum.cReference C Code
#include <stdio.h>
int main() {
int arr[] = {10, 25, 30, 45, 50, 65};
int n = 6, i, sum = 0;
for (i = 1; i < n; i += 2) {
sum += arr[i];
}
printf("Sum of odd-index elements = %d\n", sum);
return 0;
}
Exercise 3 of 5
Count Positive, Negative, Even & Odd Elements
Classifies array elements into multiple categories in a single linear scan pass.
Category Counter Rules
if (arr[i] > 0) pos++; else if (arr[i] < 0) neg++;if (arr[i] % 2 == 0) even++; else odd++;
Interactive Logic Hints
💡 Hint 1: Does 0 count as positive or negative? ▼
Zero is neither positive nor negative! Use strict
> 0 for positive and < 0 for negative!🧩 Complete Classification Loop
for (i = 0; i < n; i++) {
if (arr[i] 0) pos++;
else if (arr[i] < 0) neg++;
if (arr[i] 2 == 0) even++;
else odd++;
}
if (arr[i] 0) pos++;
else if (arr[i] < 0) neg++;
if (arr[i] 2 == 0) even++;
else odd++;
}
Select conditions above and click Check!
🧮 Array Classification Counter
Result...
array_classifier.cReference C Code
#include <stdio.h>
int main() {
int arr[] = {-5, 12, 0, -8, 23, 14};
int n = 6, i, pos = 0, neg = 0, even = 0, odd = 0;
for (i = 0; i < n; i++) {
if (arr[i] > 0) pos++;
else if (arr[i] < 0) neg++;
if (arr[i] % 2 == 0) even++;
else odd++;
}
printf("Positives: %d, Negatives: %d\n", pos, neg);
printf("Evens: %d, Odds: %d\n", even, odd);
return 0;
}
Exercise 4 of 5
Check if 1-D Array is Sorted (Ascending Order)
Verifies if every adjacent element pair satisfies arr[i] <= arr[i+1] throughout the array.
Adjacent Pair Verification
- Loop
i = 0 to n - 2 - If
arr[i] > arr[i + 1]→ Out of order!isSorted = false; break;
Interactive Logic Hints
💡 Hint 1: Why loop up to n - 2 instead of n - 1? ▼
Because we compare
arr[i] with arr[i + 1]! If i = n - 1, arr[i + 1] would access Out-of-Bounds memory!🧩 Complete Sorted Array Verification Loop
bool isSorted = true;
for (i = 0; i < n ; i++) {
if (arr[i] arr[i + 1]) {
isSorted = false;
;
}
}
for (i = 0; i < n ; i++) {
if (arr[i] arr[i + 1]) {
isSorted = false;
;
}
}
Select loop limits above and click Check!
📈 Sorted Array Checker Visualizer
Result...
check_sorted.cReference C Code
#include <stdio.h>
#include <stdbool.h>
int main() {
int arr[] = {12, 24, 35, 48, 60};
int n = 5, i;
bool isSorted = true;
for (i = 0; i < n - 1; i++) {
if (arr[i] > arr[i + 1]) {
isSorted = false;
break;
}
}
if (isSorted) printf("Array IS Sorted in Ascending Order!\n");
else printf("Array is NOT Sorted.\n");
return 0;
}
Exercise 5 of 5
Bubble Sort Algorithm (Sorting 1-D Array)
Repeatedly swaps adjacent elements if they are in wrong order until the array is sorted.
Bubble Sort Passes
- Outer loop pass:
i = 0 to n - 2 - Inner comparison loop:
j = 0 to n - i - 2 - If
arr[j] > arr[j + 1]→temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp;
Interactive Logic Hints
💡 Hint 1: Why does inner loop limit decrease by i? ▼
After pass
i, the largest element is "bubbled" into its final sorted position at the end! No need to compare again!🧩 Complete Bubble Sort Swap Loops
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[];
arr[j + 1] = temp;
}
}
}
for (j = 0; j < n - i - 1; j++) {
if (arr[j] arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[];
arr[j + 1] = temp;
}
}
}
Select statements above and click Check!
🔮 Interactive Bubble Sort Visualizer
Sorted Array Result...
bubble_sort.cReference C Code
#include <stdio.h>
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = 7, i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printf("Sorted Array: ");
for (i = 0; i < n; i++) printf("%d ", arr[i]);
printf("\n");
return 0;
}
Knowledge Check
Test Your 1-D Array Logic Mastery
Question 1 of 5
Loading question...