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

Chapter - 5 Arrays

Uploaded by

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

Chapter - 5 Arrays

Uploaded by

suma.angadi925
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

UNIT -2

CONTINUATION

Chapter -5 ARRAYS
Definition

• An array is a group of homogeneous data


elements that all have the same name and
data type.
• To refer an element in the array, we specify
the name of the array and the position
number of the element to which we refer and
Data type. An array is a collection of elements
of same data type.
• For example: if you want to add 100 numbers,
you can create an array for it.
• int numbers[100];

• The size and type of arrays cannot be changed


after its declaration.
**Arrays are of 3 types:

• One-dimensional arrays
• Two - dimensional arrays
• Multidimensional arrays
One Dimensional Arrays

• An Array which have elements with single


subscript or index are known as 1D Arrays.
*Declaration of an Array
• Datatype arrayname[array_size];
• For example,
• int mark[5];
Elements of an Array and How to access them?

• You can access elements of an array by


indices.
• Index of the array starts from the 0 to n-1.
• Suppose you declared an array mark as
int marks[5];
The first element is mark[0], second element
is mark[1] and so on.
*Initialization of an array
• It's possible to initialize an array during
declaration.
• For example,
int mark[5] = {19, 10, 8, 17, 9};
• Another method to initialize array during
declaration:
int mark[ ] = {19, 10, 8, 17, 9};
• Here,
• mark[0] is equal to 19
• mark[1] is equal to 10
• mark[2] is equal to 8
• mark[3] is equal to 17
• mark[4] is equal to 9
How to insert and print array elements?

• Ex: int a [5] = { 10 , 20, 30, 40, 50 };


**
STRINGS IN C
**
• Use gets(name) instead to read the string with
whitespace
*Commonly used string functions
• strlen() - calculates the length of a string
• strcpy() - copies a string to another
• strcmp() - compares two strings
• strcat() - concatenates two strings
• strrev()- reverse the string
• strupr() – convert the letters to uppercase
• strlwr() – convert the letters to lowercase

*Two Dimensional Arrays
A group of homogeneous data elements that all
have the same name and data type with two
subscripts is known as Two- Dimensional Array.
A Two – dimensional Array consists of rows &
columns.
*Declaration of a two-Dimensional Array
Syntax:
datatype Arrayname [rowsize][columnsize];
Eg: int x[3][4];
• Here, x is the name of a two-dimensional (2D)
array. The array can hold 12 elements. You can
think the array as table with 3 row and each row
has 4 column.
The below is the representation of an array with
3 rows & 4 columns
*Code for accept or read 2D-array elements:
printf ( “ \n Enter elements to an array”);
for ( i=0; i<n ; i++)
for (j=0; j<n ; j++)
scanf ( “%d” , &a[i][j]);
*Code for print or display 2D-array elements:
printf ( “ \n Elements of an array are”);
for ( i=0; i<n ; i++)
{
for (j=0; j<n; j++)
printf ( “%d” , a[i][j]);
}
ORDER OF A MATRIX

• The number of rows and columns that a


matrix has is called its order or its dimension.
• Eg: If the order of a matrix is 4 then it has 4
rows and 4 columns.
Program to find the sum of two matrices
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10], b[10][10],sum[10][10], i, j ,n; clrscr();
printf(“\n enter the order of matrix”);
scanf(“%d”,&n);
printf ( “ \n Enter elements to the first array”);
for ( i=0; i<n ; i++)
for (j=0; j<n ; j++)
scanf ( “%d” , &a[i][j]);
printf ( “ \n Enter elements to the second array”);
for ( i=0; i<n ; i++)
for (j=0; j<n ; j++)
for ( i=0; i<n ; i++)
for (j=0; j<n ; j++)
{
sum[i][j] = a[i][j] + b[i][j];
}
printf ( “ \n sum of elements of an array are”);
for ( i=0; i<n ; i++)
for (j=0; j<n; j++)
printf ( “%d \t” , sum[i][j]);
}
getch();
}
Multi dimensional Array
A multidimensional array is an array with more than
two dimensions or two subscripts.
Or
An array of arrays is known as multidimensional arrays.
Syntax
datatype Array_name[size1][size2].......[sizeN];
For example, the following declaration creates a three
dimensional integer array −
int threedim[3][3][3];
3- page or dimension
3-row
3- column
Representation
Initialization of a multi dimensional Array

int test[3][4][2] = {
{
{2,4},
{7,8}, -----------Page 1
{3,4},
{5,6},
},
{
{7,6},
{3,4},-------------Page 2
{5,3},
{2,3},
},
{
{8,9},
{7,2},
{3,4},--------Page 3

You might also like