0% found this document useful (0 votes)
2 views

c Programming - Module IV

The document outlines a course on Introduction to Computers and Programming in C, focusing on arrays, their declaration, initialization, and operations. It includes detailed examples of one-dimensional, two-dimensional, and three-dimensional arrays, as well as string manipulation in C. Dr. Kanta Prasad Sharma, the course instructor, has extensive academic and research experience in the field.

Uploaded by

Siddhant Pandey
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

c Programming - Module IV

The document outlines a course on Introduction to Computers and Programming in C, focusing on arrays, their declaration, initialization, and operations. It includes detailed examples of one-dimensional, two-dimensional, and three-dimensional arrays, as well as string manipulation in C. Dr. Kanta Prasad Sharma, the course instructor, has extensive academic and research experience in the field.

Uploaded by

Siddhant Pandey
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

ES 202 - Introduction to Computers and Programming in C

Dr. Kanta Prasad Sharma ( K P Sharma)


Associate Professor –CSE
Amity School of Engineering & Technology
Greater Noida Campus
+91 9760 207629
Faculty Profile
Greater Noida Campus Academic Highlights
 Ph.D. – Information Technology
 Industrial Training- UI Path Automation
 Industrial Training – Global Logic – Full Stack
 Books Published : 5
Experience
14 Years as academics &Innovative Research
Research Highlights

 SCI :18 [ WOS -H Index :08] & WCIF – 01.02

 Scopus : 42 [Scopus- H Index : 12]

 Citations : 454 [ H- 11 & I- 13 ]


Dr. Kanta Prasad Sharma
Associate Professor (CSE)  Research Gate : 9.9 [ H- 8]
Amity School of Engineering & Technology
Official : Faculty Cabin - 413 ( IV Floor,)
Amity University – G –Noida Campus  Patents :15 [ Published :18 ,Grant:10]
+91 9760 20 7629 Members
MODULE - IV
Greater Noida Campus

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:

collection of elements of the same data type stored in


• An array is a
contiguous memory locations.
• Array is a linear data structure where all elements are arranged
sequentially.

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

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.
Example
Void main ()
{
int myNumbers[]= {25, 50, 75, 100};
printf("%d", myNumbers[0]);
}
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

change the value of a specific element, refer  to the index number

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

size1: Size of the first dimension  row.

size2: Size of the second dimension column.


Array Types
Greater Noida Campus

#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

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:
Array Types
Greater Noida Campus
Access Elements of 2D Array
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

Type arr_name [row] [depth]


[column];
Initialization 3 D Array
Greater Noida Campus

int arr[2][3][2] = {0, 1, 2, 3, 4, 5, 6, 7 , 8, 9,


10, 11}

int arr[2][3][2] = { { { 1, 1 }, { 2, 3 }, { 4,
5 } },
{ { 6, 7 }, { 8, 9 }, { 10, 11 } } };.
Initialization 3 D Array
Greater Noida Campus

Row Major Order


Column Major Order
Traversing 3 D Array
Greater Noida Campus
#include <stdio.h>
void main()
{
int arr[2][3][2] = {0};
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
for (int k = 0; k < 2; k++)
{
printf("%d ", arr[i][j][k]);
}
printf("\n");
}
printf("\n");
}
}
Traversing 3 D Array
Greater Noida Campus
#include <stdio.h>
int main()
{
int arr[2][3][2] = { { { 1, 1 }, { 2, 3 }, { 4, 5 } },{ { 6, 7 }, { 8, 9 }, { 10, 11 } } };
For (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
for (int k = 0; k < 2; ++k)
{
printf(“\n arr[%i][%i][%i] = %d ", i, j, k, arr[i][j][k]);
}
printf("\n");
}
printf(“\n”);
}
}
Traversing 3 D Array
Greater Noida Campus
#include <stdio.h>
int main()
{
int arr[2][3][2] = { { { 1, 1 }, { 2, 3 }, { 4, 5 } },{ { 6, 7 }, { 8, 9 }, { 10, 11 } } };
For (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
for (int k = 0; k < 2; ++k)
{
printf(“\n arr[%i][%i][%i] = %d ", i, j, k, arr[i][j][k]);
}
printf("\n");
}
printf(“\n”);
}
}
Traversing 3 D Array
Greater Noida Campus
#include <stdio.h>
int main()
{
int arr[2][3][2] = { { {0, 1}, {2, 3}, {4, 5} },{ {6, 7}, {8, 9}, {10, 11} }};
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
for (int k = 0; k < 2; k++)
{
printf(“\n%d ", arr[i][j][k]);
}
}
printf("\n");
}
}
Array String
Greater Noida Campus

#include <stdio.h>
int main()
{
char arr[3][10] = {“AMITY", “VIT", “BHU"};
printf("String array Elements are:\n");

for (int i = 0; i < 3; i++)


{
printf("%s\n", arr[i]);
}
return 0;
}
Array String
Greater Noida Campus

<string.h> #include <string.h>

strlen()  Returns the length of the string


strupr( )Converts a given string to uppercase
strlwr( )convert a given string into lowercase.
strcpy() Copy one string to another.
strcat() Concatenates two strings.
strcmp()Compares two strings.
strstr()Find the given substring in the string
strtok()Split the given string into tokens based on some character as a delimiter.
Array String
Greater Noida Campus

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

You might also like