Presentation 33256 Content Document 20250318034608PM
Presentation 33256 Content Document 20250318034608PM
For example:
int arr1[100];
int a, b;
a = 5;
b = 6;
arr1[0] = 23;
a = arr1[12];
c = b + arr1[20];
arr1[99] = a + arr1[90];
…
..
Arrays and Loops
• It is convenient to use loops with a variable to act as the index and access
every element of an array
• For example:
{
int nums[20];
int i;
for(i=0;i<20;i++) {
nums[i] = 100 + i;
}
}
What does the above code do?
More on accessing arrays
• We can use functions like scanf to write data into a specific array
element
• For example:
{
int nums[5], i;
for(i=0;i<5;i++) {
scanf(“%d”,&nums[i]);
}
}
Traversing an array
• Traversing an array just means that you are accessing all the elements of the
array in some sequence:
• For example, the below code prints all the elements in an array by
“traversing” it:
{
int i;
int nums[]={10,11,12,13,14};
for(i=0;i<5;i++) {
printf(“At index %d, value is %d\n”,i,nums[i]);
}
}
Searching for an element in an array
• The following code searches for a value, given by variable ‘val’ and if it is found,
prints the index where it was found:
{
int i, val, nums[10] = {20,44,55,3,4,55,24,25,130,111};
scanf(“%d”,&val);
for(i=0;i<10;i++) {
if(nums[i] == val) {
printf(“value %d found at index %d\n”,val,i);
}
}
}
Variable Length Arrays
• Modern C allows us to declare an array in local scope (local
variable) where the size can be determined at run-time.
• i.e, the declaration of the array size can be a variable whose value can
be determined at run-time, for example, it can be an user input.
{
int a;
scanf(“%d”,&a); /* Read a value for ‘a’ from the user */
int darr[a]; /* Declaration for array ‘darr’ with size as ‘a’ */
….
}
Variable length arrays…
• Variable length arrays are only possible as local
variables in a C program
• We cannot declare a dynamically sized array which has
global scope
• Local variables in C are allocated in the stack space and
so we need to be careful about the size of the array
that we declare in this manner. Default stack size may
not be sufficient if we are trying to declare a very large
array – Need to be aware of this
Basic understanding
#include<stdio.h>
int main( )
name
{ a
int a; 5 Value of a
a = 5;
} location
Array
Array provide a mechanism for declaring and accessing several data items
with only one identifier, thereby simplifying the task of data management.
Array is beneficial if you have to store similar elements.
1
a
2
4
Without Array
10, 20, 30, 40, 50
#include<stdio.h>
int main()
{
int a1, a2, a3, a4, a5;
scanf("%d %d %d %d %d", &a1, &a2, &a3, &a4, &a5);
printf("%d %d %d %d %d", a1, a2, a3, a4, a5);
}
With Array
a[0] 10
10, 20, 30, 40, 50
a[1] 20
#include<stdio.h>
a[2] 30
int main()
a[3] 40
{
a[4] 50
int a[5];
scanf("%d %d %d %d %d", &a[0], &a[1], &a[2], &a[3], &a[4]);
printf("%d %d %d %d %d", a[0], a[1], a[2], a[3], a[4]);
}
Type of Array
• One Dimensional Array / 1D Array
• Two Dimensional Array / 2D Array
One Dimensional Array
• A one-dimensional array is a linear data structure that stores a collection
of elements in a single row or column, where each element is accessed
using a unique index.
• It is a contiguous block of memory, and all elements in the array must be
of the same data type.
Example:- int numbers[5] = {1, 2, 3, 4, 5};
Declaring One-D Array
Index
Syntax : - Number of Elements
data_type array_name [size];
Ex: -
int a[5]; Subscript
a[0] 10
Ex: -
a[1] 20
int a[5] = {10, 20, 30, 40, 50};
a[2] 30
int a[ ] = {10, 20, 30, 40, 50};
a[3] 40
a[4] 50
Memory Allocation One-D Array
int a[5] = {10, 20, 30, 40, 50};
1 0 a[0] 10
2 1 a[1] 20
3 2 a[2] 30
4 3 a[3] 40
5 4 a[4] 50
Accessing One-D Array Elements
int a[5] = {10, 20, 30, 40, 50};
a[0] = 10; printf(“%d\n”, a[0]);
a[1] = 20;
printf(“%d\n”, a[1]);
a[2] = 30;
a[3] = 40; printf(“%d\n”, a[2]);
a[4] = 50; printf(“%d\n”, a[3]);
printf(“%d\n”, a[4]);
Accessing 1-D Array using loop
int a[5] = {10, 20, 30, 40, 50};
/* Invalid */
int a [3] [ ];
Initializing Two-D Array
int a [3] [2];
a[0][0] = 11;
a[0][1] = 22; 0 1
a[1][0] = 10; 0 11 22
a[1][1] = 20; 1 10 20
a[2][0] = 80; 2 80 90
a[2][1] = 90;
Declaration and Initialization Two-D Array
Ex: -
/* Valid declaration*/
/* Valid declaration*/
int a[3][2] = {
int a[3][2] = {
{ 11 },
{ 11 , 22 },
{ 10 , 2 0},
{ 10, 20 },
{ 80 , 90 } { 80 }
}; };
/* Valid declaration*/
int a[3][2] = {11 , 22 , 10 , 20 , 80 , 90 };
Declaration and Initialization Two-D Array
/* Valid declaration*/
int a[ ][2] = {1, 2, 3 ,4 }
Sum Is:
Implementation of Matrix Operations-Addition
Output
Matrix Operations-Subtraction Using Two Dimensional
Array
• Matrix subtraction follows the same rule as addition but subtracts
corresponding elements.
• Same logic as addition.
Formula:
Implementation of Matrix Operations-Substraction
#include <stdio.h>
int main() {
int A[2][2] = {{9, 8}, {7, 6}};
int B[2][2] = {{5, 4}, {3, 2}};
int C[2][2]; Output
// Performing matrix subtraction
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
C[i][j] = A[i][j] - B[i][j]; // Subtract corresponding elements
}
}
return 0;
}
Matrix Operations-Multiplication Using Two Dimensional Array
The product C = A × B :
Matrix Operations-Multiplication Using Two Dimensional Array
Implementation of Matrix Operations-Multiplication
#include <stdio.h>
int main() {
int A[2][2] = {{1, 2}, {3, 4}};
int B[2][2] = {{5, 6}, {7, 8}};
int C[2][2];
// Performing matrix multiplication
for (int i = 0; i < 2; i++) { Output
for (int j = 0; j < 2; j++) {
C[i][j] = 0; // Initialize sum to zero
for (int k = 0; k < 2; k++) {
C[i][j] += A[i][k] * B[k][j]; // Multiply and sum
}
}
}
return 0;
}
Matrix Transpose Using Two Dimensional Array
• In linear algebra, the transpose of a matrix is an operator which flips a matrix over
its diagonal; that is, it switches the row and column indices of the matrix A by
producing another matrix.
• To state formally, the i-th row, j-th column element of the transpose matrix
AT is the j-th row, i-th column element of A:
• Formula:
#include <stdio.h>
int main() {
int A[2][2] = {{1, 2}, {3, 4}};
int T[2][2];
// Computing the transpose of the matrix
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
Output
T[j][i] = A[i][j]; // Swapping row and column elements
}
}
// Displaying the transpose matrix
printf("Transpose of the Matrix:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", T[i][j]);
}
printf("\n");
}
return 0;
}
THANK YOU