c Programming - Module IV
c Programming - Module IV
Outlines
Introduction to Arrays
Array Declaration
Array Initialization
Accessing Array Elements
Array Operations
Traversing
Searching
Multidimensional Arrays
Memory Allocation for Arrays
Applications of Arrays
Array Introdection
Greater Noida Campus
Definition:
Purpose:
Arrays allow storing multiple values in a single variable through a single name.
Array Declaration
Greater Noida Campus
Syntax:
data_type
array_name[array_size];
Example:
int numbers[5];
Array Initialization
Greater Noida Campus
At Declaration:
int numbers[5] = {1, 2, 3, 4, 5};
Partial Initialization:
int numbers[5] = {1, 2};
Array Initialization
Greater Noida Campus
Run-time Initialization:
Void main()
{
int numbers[5];
for( int i = 0; i < 5; i++)
{
scanf("%d", &numbers[i]);
}
Access the Elements Array
Greater Noida Campus
#include <stdio.h>
void main()
OUTPUT
{
???
int arr[5] = { 15, 25, 35, 45, 55 };
printf("Element at arr[2]: %d\n", arr[2]);
printf("Element at arr[4]: %d\n", arr[4]);
printf("Element at arr[0]: %d", arr[0]);
}
Change the Elements Array
Greater Noida Campus
Example
Void main ()
{
int myNumbers[] = {25, 50, 75, 100};
myNumbers[0] = 33;
printf("%d", myNumbers[0]);
}
Change the Elements Array
Greater Noida Campus
Change the value of a specific element using Loop to the index number
Example
Void main()
{
int i, myNumbers[] = {25, 50, 75, 100};
for (i = 0; i < 4; i++)
{
printf("%d\n", myNumbers[i]);
}
}
Array Traversing
Greater Noida Campus
Traversal is the process in which we visit every element of the data structure.
For C array traversal, we use loops to iterate through each element of the array.
Array Traversing
Greater Noida Campus
#include <stdio.h>
void main()
{ OUTPUT
int arr[5] = { 10, 20, 30, 40, 50 }; ???
arr[2] = 100;
printf("Elements in Array: ");
for (int i = 0; i < 5; i++)
{
printf("%d ", arr[i]);
}
}
Array Types
Greater Noida Campus
1D
2D
MD
Array Types
Greater Noida Campus
#include <stdio.h>
VOID main()
{
1D
char arr[6] = { ‘D’, ‘R’, ‘R’, ‘A’, ‘M’, ‘,’\0' };
2D
int i = 0;
while (arr[i])
{
MD
printf("%c", arr[i++]);
}
printf(“ Thank you “ );
}
Array Types
Greater Noida Campus
Syntax
array_name[size1] [size2];
#include <stdio.h>
int main()
{
int arr[2][3] = { 10, 20, 30, 40, 50, 60 };
printf("2D Array:\n");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
}
Array Types
Greater Noida Campus
#include <stdio.h>
int main()
{
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
printf("%d", matrix[0][2]);
}
Change Elements of 2D Array
Greater Noida Campus
#include <stdio.h>
void main()
{
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
matrix[0][0] = 9;
printf("%d", matrix[0][0]);
}
Change Elements of 2D Array
Greater Noida Campus
#include <stdio.h>
void main()
{
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
matrix[0][0] = 9;
printf("%d", matrix[0][0]);
}
Traversing Elements Through Loop
Greater Noida Campus
#include <stdio.h>
void main()
{
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]);
}
}
}
Traversing Elements Through Loop
Greater Noida Campus
#include <stdio.h>
void main()
{
int arr[3][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } };
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
printf("arr[%d][%d]: %d ", i, j, arr[i][j]);
}
printf("\n");
}
}
3 D Array
Greater Noida Campus
int arr[2][3][2] = { { { 1, 1 }, { 2, 3 }, { 4,
5 } },
{ { 6, 7 }, { 8, 9 }, { 10, 11 } } };.
Initialization 3 D Array
Greater Noida Campus
#include <stdio.h>
int main()
{
char arr[3][10] = {“AMITY", “VIT", “BHU"};
printf("String array Elements are:\n");
#include<stdio.h>
#include<string.h>
int main()
{
char str[ ] = “AMITY UNIVERSITY";
int length =strlen(str);
printf("%s\n",strlwr (str));
printf(“ Length of string: %d”, length);
return 0;
}
Array String
Greater Noida Campus
#include <stdio.h>
#include <string.h>
void main()
{
char str1[20] = “AMITY";
char str2[20];
char str3[15]=“University”
strcpy(str2, str1);
puts(str2);
strcat(str1,str3);
printf(“\n”);
puts(str2);
}