0% found this document useful (0 votes)
4 views43 pages

Presentation 33256 Content Document 20250318034608PM

This document provides an overview of arrays in programming, specifically focusing on their definition, representation, initialization, and usage in C language. It covers one-dimensional and two-dimensional arrays, including how to declare, access, and manipulate them using loops and functions. Additionally, it discusses variable length arrays and basic operations such as searching, traversing, and performing matrix operations like addition and subtraction.

Uploaded by

bevamot734
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)
4 views43 pages

Presentation 33256 Content Document 20250318034608PM

This document provides an overview of arrays in programming, specifically focusing on their definition, representation, initialization, and usage in C language. It covers one-dimensional and two-dimensional arrays, including how to declare, access, and manipulate them using loops and functions. Additionally, it discusses variable length arrays and basic operations such as searching, traversing, and performing matrix operations like addition and subtraction.

Uploaded by

bevamot734
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
You are on page 1/ 43

C

PROGRAMMING FOR PROBLEM


SOLVING
Unit 2
Course Code: 24EN1202
Arrays
Course Instructor: Prof. Priyanka S Marellavar
Academic Year: 2024–2025
Year :1st
Semester : II
What is an Array
• Array is a collection of data items of the same type
• Each item is accessed by using what is called as the index for
that element
• Index is nothing but the position of that item in the array
• The first item has the index ‘0’, the next one has index ‘1’ and so on.
• For an array with ‘n’ elements, the index ranges from 0 to n-1
• Each item in an array can be independently read and written
• The data items are stored in consecutive locations in memory.
The first location in the array will have the item with index 0,
and the one with index 1 will follow it and so on..
Why do we need arrays?
• Arrays allow you to group multiple values of the same data
type under a single name.
• This makes it easier to organize and manage related data. For
example, you can use an array to store a list of student's marks.
• Since arrays store elements in contiguous memory locations, it
allows for efficient sequential access to the elements of an
array.
• You can easily iterate through the elements of an array using
loops like for or while.
Array Representation
• One dimensional Array: Finite ordered set of homogeneous elements.
• Finite means the number of elements in the array is fixed when you declare
the array
• Ordered means each elements in the array are arranged and can be
retrieved using it index. Index starts from zero.
• Homogeneous means all the elements in the array must be of the same
data type.
• To declare an array, you specify the data type of its elements, followed by
the array name and the number of elements it can hold (the size of the
array).
int a [100];
• The above declaration declares an array by name ‘a’ of 100 elements of type ‘int’.
More on Array representation
• Memory Allocation: The size of this memory required
for an array is determined by the data type of the
elements and the number of elements in the array.
• For example, an array of 5 integers (int data type) will occupy
5 * sizeof(int) bytes of memory.

• Indexing: Each element in array is assigned an index or


position, starting from 0.
Array Initialization
1. Initializing at Declaration (Explicit Initialization):
int numbers[] = {1, 2, 3, 4, 5};

2. Initializing at Declaration (Partial Initialization):


// Initializes the first two elements, rest are set to 0
int numbers[5] = {1, 2};

3. Initializing at Declaration (Designated Initializers):


int numbers[5] = {[2] = 42, [4] = 10};
Accessing array elements
• The syntax to access an element of an array is:
<array_name>[<index>]

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

Index always starts with 0 (zero).


a[0]
a[1]
a[2]
a[3]
a[4]
Initializing One-D Array
int a[5];
a[0] = 10; a[0] 10
a[1] = 20; a[1] 20

a[2] = 30; a[2] 30

a[3] = 40; a[3] 40

a[4] = 50; a[4] 50


Declaration and Initialization One-D Array
Syntax: -
data_type array_name [size] = {val1, val2…..valn};
data_type array_name [ ] = {val1, val2…..valn};

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

Element No. Index a Array


(Size) Reference Value

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

Using For Loop Using While Loop Using Do While Loop


int i = 0; int i = 0;
int i;
while (i<5) do
for(i = 0; i<5; i++) { {
{ printf(“%d\n”, a[i]); printf(“%d\n”, a[i]);
printf(“%d\n”, a[i]); i++; i++;
} } } while (i<5);
Two Dimensional Array
Two subscripted variable is referred to Two Dimensional or Matrix Array.
The two dimensional array in C language is represented in the form of rows
and columns.
It is also known as array of arrays or list of arrays.
Example:- int a [3] [2];
Declaring Two-D Array
Syntax: - data_type array_name[size][size];
Ex: -
Number of Row Number of Column
/* Valid */
int a [3] [2];
0 1
/* Invalid */ 0
int a [ ] [ ]; 1
2
/* Invalid */
int a [ ] [2];

/* 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 }

/* Invalid declaration – you must specify second dimension*/


int a[ ][ ] = {1, 2, 3 ,4 }

/* Invalid because of the same reason mentioned above*/


int a[2][ ] = {1, 2, 3 ,4 }
Memory Allocation in Two-D Array
int a[3][2] = {
{ 11 , 22 }, 0 1
{ 10, 20 }, 0 a[0][0] = 11 a[0][1] = 22
{ 80 , 90 } 1 a[1][0] = 10 a[1][1] = 20
};
2 a[2][0] = 80 a[2][1] = 90
Accessing Two-D Array
int a [3] [2]; printf(“%d\n”, a[0][0]);
a[0][0] = 11; printf(“%d\n”, a[0][1]);
a[0][1] = 22;
a[1][0] = 10; printf(“%d\n”, a[1][0]);
a[1][1] = 20; printf(“%d\n”, a[1][1]);
a[2][0] = 80;
printf(“%d\n”, a[2][0]);
a[2][1] = 90;
printf(“%d\n”, a[2][1]);
Accessing 2-D Array using for loop
int a[3][2] = { int i, j;
{ 11 , 22 }, for(i=0; i<3; i++)
{ 10, 20 }, {
{ 80 , 90 } for(j=0; j<2; j++)
}; {
printf(“%d\t”, a[i][j]);
}
printf(“\n”);
}
Matrix Operations-Addition Using Two Dimensional
Array
• Matrix addition is performed by adding corresponding elements of two
matrices.
Formula:

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

// Displaying the result


printf("Matrix Subtraction Result:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", C[i][j]);
}
printf("\n");
}

return 0;
}
Matrix Operations-Multiplication Using Two Dimensional Array

• Matrix multiplication follows a specific rule: the number of columns in the


first matrix must be equal to the number of rows in the second matrix.
Formula:

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

// Displaying the result


printf("Matrix Multiplication Result:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", C[i][j]);
}
printf("\n");
}

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:

The transpose is:


Implementation of Matrix Transpose:

#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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy