0% found this document useful (0 votes)
5 views11 pages

unit2-programming-in-c (1)

Uploaded by

nathiya v c
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
5 views11 pages

unit2-programming-in-c (1)

Uploaded by

nathiya v c
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 11

UNIT 2 - ARRAYS

lOMoARcPSD|22072273

1. Introduction to Arrays
• An Array is a collection of similar data elements. These data elements have same data
types stored in contiguous memory location.
• Array is a collection of elements of same data type stored under common name.
• An array is a data structure that is used for storage of homogeneous data.
• It is classified into three types
i) One dimensional Array
ii) Two dimensional Array
iii) Multi-dimensional Array
1.1 Characteristics of an Array
• The elements of the array are stored in continuous memory location.
• Individual elements of an array can be accessed with the integer value known as index.
• Array index starts from 0.

• Memory space of an array can be calculated as


Size of datatype * number of elements of an array
• The location of an array in the location of its first element.
• A variable that can store multiple values is an array variable.
• It is a derived data type which stores related information together.
• The length of an array is the number of elements in the array.
• Arrays are used in the situation where we are asked read and prints the marks of 30
students .It is inefficient way of declaring 30 variables to hold marks.
So the concept of Array is used where one common variable is used for assigning 30
values.

1.2 Advantage of an array


• It is used to represent multiple data items of same type by using only single name.
• 2D arrays are used to represent matrices.
• It allows random accessing of elements i.e. any element of the array can be
randomly accessed using indexes
UNIT 2 - ARRAYS

1.3 Disadvantage of an array


• Insertion and deletion of elements in an array is difficult
• Size of the Array should be known in advance.
• Elements in the Array must be same data type

2. One dimensional array - Declaration, Initialization


• One dimensional array is collection of elements of same data type organized as a
simple linear sequence.
• Elements of an array can be accessed by using a single subscript.
2.1 Declaration of one dimensional Array
We need to declare the array variable before they are used. To declare and define
an array we must specify its name, type and size.
Syntax:
datatype arrayname[size];
datatype : kind of values it can store Example int, float, char, double.
arrayname : variable name of an Array
Size : maximum number of values that an array can hold.
Example
int a[4];

2.2 Initialization of One Dimensional Array

2.2.1. Compile time initialization: Initialization of the values of an array at the


time of declaration.
datatype arrayname [size] = {list of values separated by comma};

Example
int a[4]={20,45,67,89};
UNIT 2 - ARRAYS

program:

Array element can be accessed using the index

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[4]={20,45,67,89};
printf(“%d \t%d\t%d\t%d”,a[0],a[1],a[2],a[3]);
getch();
}

Output:

20 45 67 89

program:

The efficient way of accessing array elements is using loop

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[4]={20,45,67,89};
int i;
for(i=0;i<4;i++)
{
printf(“%d\t”,a[i]);
}
getch();
}
Output:
20 45 67 89
2.3 Run time initialization:

The value of an Array can be initialized during run time.

int a[4];
for(i=0;i<4;i++)
{
scanf(“%d”,&a[i]);
}
UNIT 2 - ARRAYS

program to print the array elements in reverse order


#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[4],i;
printf(“Enter the elements\n”);
for(i=0;i<4;i++)
{
scanf(“%d\t”,&a[i]);
}
Printf(“The Elements are\n”);
for(i=0;i<4;i++)
{
printf(“%d\t”,a[i]);
}
getch();
}
Output:
Enter the Elements
20 45 67 89
The Elements are
20 45 67 89
Program to read the numbers and print the given numbers in reverse order
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i;
clrscr();
printf("Enter the elements");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("The elements
are");
for(i=4;i>=0;i--)
{
printf("%d\t",a[i]);
}
getch();
}
UNIT 2 - ARRAYS

Output:

Enter the elements:


1 2 3 4 5
The elements are
5 4 3 2 1
l

3. Two dimensional array


• It is a collection of data elements of same data type arranged in rows and columns.
• It is collection of one dimensional array.
• An array with two subscript is called two dimensional arrays.
• It enables us to store multiple rows of elements such as table of values or matrix.
• Referring to Array Elements:
To access the elements of a two-dimensional array, we need a pair of indices: first index
selects the row and the second index selects the column.
Syntax:
datatype arrayname[size of row][size of column];

Example: int A[3][2];


Col Col
0 1
Row 0 A[0][0] A[0][1]
Row 1 A[1][0] A[1][1]
Row 2 A[2][1] A[2][2]

4.1 Compile Time Initialization


• An two-dimensional array can be initialized along with declaration.
• For two-dimensional array initialization, elements of each row are enclosed within
curly braces and separated by commas.All rows are enclosed within curly braces.
Syntax:
datatype arrayname[size of row][size of column]={values separated by

comma}; Example: int a[3][2]={{10,20},{30,40},{50,60}};

Col 0 Col 1

Row 0 10 20
Row 1 30 40
Row 2 50 60
UNIT 2 - ARRAYS

program :

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][2]={{10,20},{30,40},{50,60}};
printf(“%d\t”,a[0][0]);
printf(“%d\n”,a[0][1]);
printf(“%d\t”,a[1][0]);
printf(“%d\n”,a[1][1]);
printf(“%d\t”,a[2][0]);
printf(“%d\n”,a[2][1]);
}

Output:
10 20
30 40
50 60
program to read the array elements and print the same

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][2]={{10,20},{30,40},{50,60}};
int i,j;
printf("\nThe Matrix");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j+
+)
{
printf("\t%d",a[i][j]);
}
}
getch();
}

Output:

10 20
30 40
50 60
UNIT 2 - ARRAYS

4.2 Run time initialization:


Two-dimensional array can be initialization at run time using loop.

int a[2][3];
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”, &a[i][j]);
}
}

program :
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][3];
int i,j;
printf(“Enter the elements\
n”);
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)

{
scanf(“%d”, &a[i][j]);
}
}

printf(“\nThe elements are”);


for(i=0;i<2;i++)
{
printf(“\n”);
for(j=0;j<3;j++)
{
printf(“%d\t”, a[i][j]);
}
}

Output:
Enter the elements
1
2
3
4
The elements are
1 2
UNIT 2 - ARRAYS

2 4

C Program to find the addition of two matrix


#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2],b[2][2],c[2][2];
int i,j,k;
clrscr();
printf("Enter Matrix A");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter matrix B");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("\nThe result");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
{
printf("\t%d",c[i][j]);
}
}
getch();
}
UNIT 2 - ARRAYS

Output:
Enter Matrix A
12
34
Enter Matrix B
12
34
The result
24
68

C Program to print Transpose of matrix


#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2] , i, j;
clrscr();
printf("Enter matrix A");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j+
+)
{
printf("\t%d",a[i][j]);
}
}
printf("\nTranspose");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
{
printf("\t%d",a[j][i]);
}
}

getch();
}

Output:
Enter matrix A
1 2
3 4
Transpose
1 3
2 4
UNIT 2 - ARRAYS

C Program to Print Matrix Multiplication


#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2],b[2][2],c[2][2],sum=0;
int i,j,k;
clrscr();
printf("Enter Matrix A");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter matrix B");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
{
c[i][j]= c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("\nThe result");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("\t%d",c[i][j]);
}
}
getch();
}
UNIT 2 - ARRAYS

Output:
Enter Matrix A
12
34
Enter Matrix B
12
34
The result
7 10
15 22

Multi dimensional array


A multidimensional array is basically an array of arrays. Arrays can have any number of dimensions.
Initializing a multidimensional array
Initialization of a 2d array
//Different ways to initialize 2D array
int c[2][3] = {{1,3,0},{-1,5,9}};
int c[][3] = {{1,3,0},{-1,5,9}};
int c[2][3]={1,3,0,-1,5,9};

Initialization of a 3d array int arr[2][3][2] = { { {0, 1}, {2, 3}, {4, 5} },{ {6, 7}, {8, 9}, {10, 11} }};
#include <stdio.h>
int main()
{
int arr[2][3][2] = { { {0, 1}, {2, 3}, {4, 5} }, { {6, 7}, {8, 9}, {10, 11} }};
// Printing the array
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");
}
return 0;
}
OUTPUT
0 1
2 3
4 5

6 7
8 9
10 11

You might also like