/*
* ============================================================================
* ACTIVITY 1: STATIC ARRAYS - BASIC OPERATIONS
* ============================================================================
*
* Purpose: Introduce students to static arrays and basic operations
* Time: 15 minutes
*
* Learning Objectives:
* - Understand static array declaration and initialization
* - Practice array traversal using loops
* - Implement basic operations like sum calculation
*/
#include <stdio.h>
/*
* Function: static_array_demo
* Purpose: Demonstrates basic static array operations
*
* Key Concepts:
* - Static arrays are allocated on the stack
* - Size must be known at compile time
* - Automatic memory management (no malloc/free needed)
*/
void static_array_demo() {
printf("\n=== STATIC ARRAY DEMO ===\n");
// Static array declaration and initialization
// Memory is automatically allocated on the stack
int static_arr[5] = {10, 20, 30, 40, 50};
// Array traversal - visiting each element
printf("Static array contents: ");
for(int i = 0; i < 5; i++) {
printf("%d ", static_arr[i]);
}
printf("\n");
// Calculate sum of all elements
int sum = 0;
for(int i = 0; i < 5; i++) {
sum += static_arr[i]; // Add each element to sum
}
printf("Sum of elements: %d\n", sum);
// Additional operations you can try:
// Find maximum element
int max = static_arr[0];
for(int i = 1; i < 5; i++) {
if(static_arr[i] > max) {
max = static_arr[i];
}
}
printf("Maximum element: %d\n", max);
// Find minimum element
int min = static_arr[0];
for(int i = 1; i < 5; i++) {
if(static_arr[i] < min) {
min = static_arr[i];
}
}
printf("Minimum element: %d\n", min);
}
/*
* Function: demonstrate_array_bounds
* Purpose: Shows the importance of staying within array bounds
*
* WARNING: This function demonstrates what NOT to do
* Accessing array elements outside bounds leads to undefined behavior
*/
void demonstrate_array_bounds() {
printf("\n=== ARRAY BOUNDS DEMONSTRATION ===\n");
int arr[3] = {1, 2, 3}; // Array with 3 elements (indices 0, 1, 2)
printf("Valid array access:\n");
for(int i = 0; i < 3; i++) {
printf("arr[%d] = %d\n", i, arr[i]);
}
// WARNING: The following would be dangerous in real code
printf("\nRemember: Never access arr[3] or beyond - it's outside array bounds!\
n");
printf("Always use proper bounds checking in your programs.\n");
}
// Main function for testing this activity
int main() {
printf("ACTIVITY 1: STATIC ARRAYS\n");
printf("========================\n");
static_array_demo();
demonstrate_array_bounds();
printf("\nActivity 1 completed successfully!\n");
return 0;
}