Lecture Week 6.2
Lecture Week 6.2
and Programming
array_name[i] = new_value;
Update Array Element // Ensure valid index
if (i >= 0 && i < 5) {
printf("Enter the new value: ");
• We can update the value of an element at scanf("%d", &new_value);
the given index i in a similar way to
accessing an element by using the array // Update the value at the given index
subscript operator [ ] and assignment arr[i] = new_value;
operator =
array_name[i] = new_value; // Print the updated array
#include <stdio.h> printf("Updated array: ");
int main() { for (int j = 0; j < 5; j++) {
int arr[5] = {10, 20, 30, 40, 50}; // Initialize an printf("%d ", arr[j]);
array of 5 elements }
int i, new_value; } else {
printf("Invalid index!\n");
// Ask for the index and new value }
printf("Enter the index to update (0-4): "); return 0;
scanf("%d", &i); }
Example: Write a C program that calculates the average of
different ages
int ages[] = {20, 22, 18, 35, 48, 26, 87, 70}; // An array storing different ages
float avg, sum = 0;
int i;
int length = sizeof(ages) / sizeof(ages[0]); // Get the length of the array
for (i = 0; i < length; i++) { // Loop through the elements of the array
sum += ages[i];
}
avg = sum / length; // Calculate the average by dividing the sum by the length
printf("The average age is: %.2f", avg); // Print the average
// An array storing different ages
int ages[] = {20, 22, 18, 35, 48, 26, 87, 70};
int i;
// Get the length of the array
Write a program int length = sizeof(ages) / sizeof(ages[0]);
that finds the // Create a variable and assign the first array element
of ages to it
lowest age int lowestAge = ages[0];
among different // Loop through the elements of the ages array to find
the lowest age
ages for (i = 0; i < length; i++) {
if (lowestAge > ages[i]) {
lowestAge = ages[i];
}}
• A multi-dimensional array can be defined as an array
that has more than one dimension.
• It can grow in multiple directions.
• Syntax:
Multidimensiona • The general form of declaring N-dimensional arrays
is shown below:
l Arrays – 2D
and 3D • type arr_name[size1][size2]….[sizeN];
• Ex.
• Two-dimensional array: int two_d[10][20];
• Three-dimensional array: int
three_d[10][20][30];
Size of Multidimensional Arrays
• The total number of elements that can be stored in a multidimensional array
can be calculated by multiplying the size of both dimensions.
• Example:
• The array arr[10][20] can store total of (10*20) = 200 elements.
• To get the size in bytes, we multiply the size of a single element (in bytes)
by the total number of elements in the array.
• Example:
• The size of array int arr[10][20] = 10 * 20 * 4 = 800 bytes, where the
size of int is 4 bytes.
Two-Dimensional • 2D array is also known as a matrix (a table of rows
Array and columns).
• Example:
• int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
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.
• Example
• int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
• The following example will change the value of the element in the first row
(0) and first column (0):
• Example
• int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
matrix[0][0] = 9;
printing
{ for (j=0;j<3;j++) {
printf("Enter a[%d][%d]: ",i,j);
elements at } }
scanf("%d",&arr[i][j]);
Declaration: Initialization:
• Functions