0% found this document useful (0 votes)
8 views22 pages

Understanding Arrays in C Programming

The document provides an overview of arrays in programming, including how to create, access, modify, and loop through both single-dimensional and multi-dimensional arrays. It explains the syntax for declaring arrays, accessing elements using index numbers, and calculating the size of arrays. Additionally, it covers the concept of multidimensional arrays, specifically two-dimensional arrays, and how to manipulate their elements.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views22 pages

Understanding Arrays in C Programming

The document provides an overview of arrays in programming, including how to create, access, modify, and loop through both single-dimensional and multi-dimensional arrays. It explains the syntax for declaring arrays, accessing elements using index numbers, and calculating the size of arrays. Additionally, it covers the concept of multidimensional arrays, specifically two-dimensional arrays, and how to manipulate their elements.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Array

• Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.

• To create an array, define the data type (like int) and specify the name of the array
followed by square brackets [].

• To insert values to it, use a comma-separated list, inside curly braces:

• int myNumbers[] = {25, 50, 75, 100};


• We have now created a variable that holds an array of four integers.
Access the Elements of an Array

• To access an array element, refer to its index number.

• Array indexes start with 0: [0] is the first element. [1] is the second
element, etc.

• This statement accesses the value of the first element [0] in


myNumbers
• int myNumbers[] = {25, 50, 75, 100};
printf("%d", myNumbers[0]);
Change an Array Element

• To change the value of a specific element, refer to the


index number:
• Example

• int myNumbers[] = {25, 50, 75, 100};


myNumbers[0] = 33;

printf("%d", myNumbers[0]);
// Now outputs 33 instead of 25
• Loop Through an Array
• You can loop through the array elements with the for loop.

• The following example outputs all elements in the myNumbers array:


• int myNumbers[] = {25, 50, 75, 100};
int i;

for (i = 0; i < 4; i++) {


printf("%d\n", myNumbers[i]);
}
Set Array Size

• Another common way to create arrays, is to specify the size of the array,
and add elements later:
• // Declare an array of four integers:
int myNumbers[4];

// Add elements
myNumbers[0] = 25;
myNumbers[1] = 50;
myNumbers[2] = 75;
myNumbers[3] = 100;
• Using this method, you should know the number of array elements in
advance, in order for the program to store enough memory.
• You are not able to change the size of the array after creation.
Get Array Size or Length

#include <stdio.h>

int main() {
int myNumbers[] = {10, 25, 50, 75, 100};
printf("%lu", sizeof(myNumbers));

return 0;
}
• Why did the result show 20 instead of 5, when the array contains 5
elements?
• It is because the sizeof operator returns the size of a type in bytes.

• You learned from the Data Types chapter that an int type is usually 4
bytes, so from the example above, 4 x 5 (4 bytes x 5 elements) = 20
bytes.

• Knowing the memory size of an array is great when you are working
with larger programs that require good memory management.
• But when you just want to find out how many elements an array has,
you can use the following formula (which divides the size of the array
by the size of one array element):
• int myNumbers[] = {10, 25, 50, 75, 100};
int length
=sizeof(myNumbers)/sizeof(myNumbers[0]);

printf("%d", length); // Prints 5


Making loops better
• In the array loops section in the previous slides, we wrote the size of
the array in the loop condition (i < 4). This is not ideal, since it will
only work for arrays of a specified size.

• However, by using the sizeof formula from the example above, we can
now make loops that work for arrays of any size, which is more
sustainable.
• Instead of writing:
• Example
• int myNumbers[] = {25, 50, 75, 100};
int i;

for (i = 0; i < 4; i++) {


printf("%d\n", myNumbers[i]);
}
• We can write
int myNumbers[] = {25, 50, 75, 100};
int length = sizeof(myNumbers) /
sizeof(myNumbers[0]);
int i;

for (i = 0; i < length; i++) {


printf("%d\n", myNumbers[i]);
}
Create a program that calculates
the average of different ages:
• // An array storing different ages
int ages[] = {20, 22, 18, 35, 48, 26, 87, 70};

float avg, sum = 0;


int i;

// Get the length of the array


int length = sizeof(ages) / sizeof(ages[0]);

// Loop through the elements of the array


for (i = 0; i < length; i++) {
sum += ages[i];
}
• avg = sum / length;

// Print the average


printf("The average age is: %.2f", avg);
Multidimensional Arrays
• In the previous slides, you learned about arrays, which is also known
as single dimension arrays. These are great, and something you will
use a lot while programming in C.
• However, if you want to store data as a tabular form, like a table with
rows and columns, you need to get familiar with multidimensional
arrays.
• A multidimensional array is basically an array of arrays
• Two-Dimensional Arrays
• A 2D array is also known as a matrix (a table of rows and columns).

• To create a 2D array of integers, take a look at the following example:

• int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };


• The first dimension represents the number of rows [2], while the
second dimension represents the number of columns [3]. The values
are placed in row-order, and can be visualized like this:
Access the Elements of a 2D Array
• To access an element of a two-dimensional array, you must specify
the index number of both the row and column.
• This statement accesses the value of the element in the first row (0)
and third column (2) of the matrix array.
• int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };

printf("%d", matrix[0][2]); // Outputs 2


Change Elements in a 2D Array
• To change the value of an element, refer to the index number of the
element in each of the dimensions:
• The following example will change the value of the element in the
first row (0) and first column (0):
• int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
matrix[0][0] = 9;

printf("%d", matrix[0][0]); // Now outputs 9


instead of 1
Loop Through a 2D Array
• To loop through a multi-dimensional array, you need one loop for each of the
array's dimensions.

• The following example outputs all elements in the matrix array:


• int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };

int i, j;
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
printf("%d\n", matrix[i][j]);
}
}
Thank You

You might also like