Array in C Module Part 1
Array in C Module Part 1
Introduction to Arrays:
An array in C is a collection of elements of the same data type stored in contiguous memory locations.
Each element in the array is accessed by its index.
Objective:
By the end of this module, students should be able to:
Understand the concept of arrays in C.
Declare and initialize arrays.
Access elements of an array.
Perform basic operations on arrays such as traversal and manipulation.
Prerequisites:
Basic knowledge of C programming.
Understanding of variables and data types.
LESSON PROPER
What is an ARRAY?
In C, an array is a collection of elements of the same data type stored in contiguous memory locations. It
provides a convenient way to store and access multiple elements of the same type using a single
identifier. Each element in an array is accessed by its index, which represents its position in the array.
Arrays are declared by specifying the data type of the elements and the number of elements in square
brackets ([]).
Initialization of Arrays:
Arrays can be initialized during declaration.
Traversal of Arrays:
Traversal means accessing each element of the array.
This can be done using loops like 'for' or 'while'.
Array Manipulation:
Arrays can be modified by assigning new values to their elements.
We declare an array called numbers of type int with a size of 5. This means the array can hold 5 integers.
We assign values to each element of the array using the array index, starting from 0 to 4.
We access and print the elements of the array using the array index.
Arrays in C are zero-indexed, meaning the index of the first element is 0, the index of the second
element is 1, and so on. This is important to remember to avoid accessing elements outside the bounds
of the array, which can lead to undefined behavior.
FYI:
In the context of memory allocation, "contiguous memory" means that blocks of memory are allocated
next to each other without any gaps between them. This is important for data structures like arrays,
where elements need to be stored sequentially in memory. When elements of an array are stored in
contiguous memory, it allows for efficient access to elements using indices, as each element's memory
address can be calculated based on its position relative to the starting address of the array.
For example, if an array arr is allocated in contiguous memory starting at address 0x1000, and each
element occupies 4 bytes, then the memory addresses of the elements might be 0x1000 for the first
element, 0x1004 for the second element, 0x1008 for the third element, and so on.
EXAMPLES:
Method 1.
#include <stdio.h>
return 0;
}
Both examples achieve the same goal of initializing an array n with values {10, 20, 30, 40, 50} and then
printing each element of the array using a loop.
Lets Check it
o Both examples declare an array n of size 5 to hold integers.
o Both examples use a for loop to iterate over the elements of the array and print them.
o The values {10, 20, 30, 40, 50} are assigned to the array elements in both examples.
The first example initializes the array n by individually assigning values to each element after
declaration, whereas the second example initializes the array during declaration using an
initializer list {10, 20, 30, 40, 50}.
More Examples:
#include <stdio.h>
int main() {
// Declare and initialize an array of characters (string)
char name[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
return 0;
}
int main() {
// Declare and initialize an array of floating point numbers
float prices[4] = {12.5, 23.7, 8.9, 15.2};
return 0;
}
Array of Characters (String)
#include <stdio.h>
int main() {
// Declare and initialize an array of characters (string)
char message[] = "Hello, World!";
return 0;
}
#include <stdio.h>
int main() {
// Declare and initialize an array of characters (string)
char message[] = "Hello, World!";
return 0;
}
int main() {
// Declare and initialize an array of characters (string)
char message[] = "Hello, World!";
return 0;
}
Quiz:
1. What is an array in C?
A) A single variable that can store multiple values of different data types.
B) A collection of elements of the same data type stored in contiguous memory locations.
C) A function used to manipulate strings.
D) An operator used for arithmetic calculations.
4. Write the syntax for declaring and initializing an integer array with 5 elements.
A) int numbers[5]; B) int numbers = {1, 2, 3, 4, 5};
C) int numbers[5] = {1, 2, 3, 4, 5}; D) int numbers[] = {1, 2, 3, 4, 5};
Submission: Submit the C program file along with a brief explanation of each step and its output.
References :
https://www.w3schools.com/c/c_arrays.php
https://www.programiz.com/c-programming/c-arrays https://www.geeksforgeeks.org/c-arrays/
https://www.tutorialspoint.com/cprogramming/c_arrays.htm
https://en.cppreference.com/w/c/language/array
Prepared by: JOHNAS E. DALUSONG , LPT