0% found this document useful (0 votes)
10 views6 pages

Array in C Module Part 1

The document provides a learning module on arrays in C programming. It discusses the concept of arrays, how to declare and initialize arrays, access array elements, and perform basic operations like traversing and manipulating arrays. Examples of different types of arrays are also provided.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
10 views6 pages

Array in C Module Part 1

The document provides a learning module on arrays in C programming. It discusses the concept of arrays, how to declare and initialize arrays, access array elements, and perform basic operations like traversing and manipulating arrays. Examples of different types of arrays are also provided.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 6

Republic of the Philippines

President Ramon Magsaysay State University


College of Industrial Technology – Iba, Zambales Campus (2201)
Tel./Fax No. (047) 811-1683/ 0919-069-9174│rmtupresident@yahoo.com
www.prmsu.edu.ph | https://icloudph.com/prmsu-main/sias/

Learning Module: Arrays in C (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.

Declaration and Initialization of Arrays:

Arrays are declared by specifying the data type of the elements and the number of elements in square
brackets ([]).

// Declaration of an integer array with 5 elements


int numbers[5];
Accessing Elements of an Array:

Array elements are accessed using indices.


Indices start from 0 to (size - 1) for an array of size 'size'.

// Assigning value 10 to the first element


numbers[0] = 10;
// Accessing and printing the first element
printf("%d", numbers[0]);

Initialization of Arrays:
Arrays can be initialized during declaration.

// Declaration and initialization of an integer array


int numbers[5] = {10, 20, 30, 40, 50};

Traversal of Arrays:
Traversal means accessing each element of the array.
This can be done using loops like 'for' or 'while'.

for(int i = 0; i < 5; i++)


{ printf("%d ", numbers[i]); }

Array Manipulation:
Arrays can be modified by assigning new values to their elements.

// Modifying the value of the third element


numbers[2] = 35;

Here's a basic example of how to declare and use an array in C:

#include <stdio.h> numbers[4] = 50;

int main() { // Access and print the elements of the array


// Declare an array of integers with size 5 printf("Element 0: %d\n", numbers[0]);
int numbers[5]; printf("Element 1: %d\n", numbers[1]);
printf("Element 2: %d\n", numbers[2]);
// Assign values to the elements of the array printf("Element 3: %d\n", numbers[3]);
numbers[0] = 10; printf("Element 4: %d\n", numbers[4]);
numbers[1] = 20;
numbers[2] = 30; return 0;
numbers[3] = 40; }
In this example:

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>

int main() { Method 2.


int i; #include <stdio.h>

int n[5]; int main() {


n[0] = 10; int i;
n[1] = 20; int n[5] = {10, 20, 30, 40, 50};
n[2] = 30;
n[3] = 40; for (i=0; i<5; i++){
n[4] = 50; printf("Element is %d\n", n[i]);
}
for (i=0; i<5; i++){
printf("Element is %d\n", n[i]); return 0;
} }

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:

Array of Characters (String)

#include <stdio.h>

int main() {
// Declare and initialize an array of characters (string)
char name[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

// Print the string


printf("String: %s\n", name);

return 0;
}

Array of Floating Point Numbers


#include <stdio.h>

int main() {
// Declare and initialize an array of floating point numbers
float prices[4] = {12.5, 23.7, 8.9, 15.2};

// Print the elements of the array


printf("Prices:\n");
for (int i = 0; i < 4; i++) {
printf("%.2f ", prices[i]);
}
printf("\n");

return 0;
}
Array of Characters (String)

#include <stdio.h>

int main() {
// Declare and initialize an array of characters (string)
char message[] = "Hello, World!";

// Print the string


printf("Message: %s\n", message);

return 0;
}

OR something like this if you want to print character using index

#include <stdio.h>

int main() {
// Declare and initialize an array of characters (string)
char message[] = "Hello, World!";

// Print the string


printf("Message: %c\n", message[2]);

return 0;
}

Or display all character by index using loop


#include <stdio.h>

int main() {
// Declare and initialize an array of characters (string)
char message[] = "Hello, World!";

// Print the string along with the index of each character


printf("Message with indices:\n");
for (int i = 0; message[i] != '\0'; i++) {
printf("Index %d: %c\n", i, message[i]);
}

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.

2. How are elements of an array accessed?


A) By using parentheses () B) By using curly braces {}
C) By using square brackets [] D) By using angle brackets <>

3. Explain the importance of array indexing.


A) It allows access to elements of an array based on their position.
B) It determines the length of the array.
C) It converts characters to integers.
D) It defines the data type of the array.

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};

5. Provide an example of array traversal using a loop.


A) for (int i = 0; i < size; i++) B) while (i < size)
C) do { ... } while (i < size) D) for (int i = size; i > 0; i--)

TASK: Array Manipulation Program

Objective: Write a C program that demonstrates basic array manipulation techniques.


Requirements:
1. Declare an integer array with a size of 10.
2. Initialize the array with values {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.
3. Print the elements of the array in reverse order.
4. Calculate and print the sum of all elements in the array.
5. Find and print the maximum and minimum values in the array.

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

You might also like