Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

C - Programming Language

Array

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
Basic, Control Structure, Looping, Array, Pointer

So far we have used only the fundamental data types, namely char, int, float, double and variations of int and
double. Although these types are very useful, they are constrained by the fact that a variable of these types can
store only one value at any given time. Therefore, they can be used only to handle limited amount of data. In
many application, however, we need to handle a large volume of data in terms of reading, processing and
printing. To process such large amounts of data, we need a powerful data type that would facilitate efficient
storing, accessing and manipulation of data items. C support a derived data type known as array that can be
used for such application.

An array is a fixed-size sequenced collection of elements of the same data type. It is simply grouping of like-type
data. In its simplest form, an array can be used to represent a list of numbers, or a list of names. Some example
where array can be used.

 List of temperatures recorded every hour in a day, or a month, or a year.


 List of employees in an organization
 List of products and their cost sold by a store
 Test scores of a class of students.
 List of customers and their telephone numbers
 Table of daily rainfall data.

PPT CREATED BY : Prof. Rajesh K. Jha 1


C - Programming Language

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
Basic, Control Structure, Looping, Array, Pointer

Data Structure Arrays and structures are referred to as structured data types
because they can be used to represent data values that have a
1. Derived Type structure of some sort. Structured data types provide an
organizational scheme that shows the relationships among the
 Arrays individual elements and facilitate efficient data manipulations.
 Functions In programming, such data types are known as data structures.
 Pointers
In addition to arrays and structures, C supports creation and
2. Fundamental Types manipulation of the following data structures.

 Internal Types  Linked List


 Float Types  Stacks
 Character Types  Queues
 Trees
3. User-Defined Types
 Structures
 Unions
 Enumerations

PPT CREATED BY : Prof. Rajesh K. Jha 2


C - Programming Language
Array can be categorized as :

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
1. One-Dimensional Array : Value store in linear /
Basic, Control Structure, Looping, Array, Pointer

Value storing process in single dimensional array.


sequence format.

Declaration of O.D.A : int a[5];


Address of 1000 1004 1008 1012 1016
variable index
int a[5]={1,2,3,4,5}
int a[ ] = {1,2,3,4,5} value 1 2 3 4 5
Index 0 1 2 3 4
Note: Array variable a store 5 elements like.

In memory, array variable reserve space with Input method in array:


segment index, where value of array element can
be assigned as. 1. Direct Initialization : is the technique by which a value
a[0] = 1 is being assigned at the time of declaration on array.
a[1] = 2
int a [5] a[2] = 3 2. Through the Keyboard : is the technique by which array
value entered at the time of running of program.
a[3] = 4
a[4] = 5
PPT CREATED BY : Prof. Rajesh K. Jha 3
C - Programming Language
Program : One dimensional array.

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
Basic, Control Structure, Looping, Array, Pointer

Direct Initialization : is the technique by which a Note: array without size definition.
value is being assigned at the time of declaration on
array.
# include <stdio.h>
# include <stdio.h> int main()
int main() {
{ int arr[] = {10,20,30,40,50};
int arr[5] = {10,20,30,40,50}; int i;
int i;
printf("\n Output of array element=\n");
printf("\n Output of array element=\n"); for(i=0; i<5;i++)
for(i=0; i<5;i++) {
{ printf("\n%d",&arr[i]);
printf("\n%d",&arr[i]); }
} return 0;
return 0; }
}

PPT CREATED BY : Prof. Rajesh K. Jha 4


C - Programming Language
Through the Keyboard : is the technique by Addition and Average of array element.

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
which array value entered at the time of
running of program. return main()
Basic, Control Structure, Looping, Array, Pointer

{
int main()
{ int arr[5]={1,2,3,4,5}, sum=0,i,avg=0;
int arr[5], i;
printf("enter your array element=\n"); for(i=0;i<5;i++)
for(i=0; i<5;i++) {
sum= sum+ arr[i];
{
}
scanf("%d",&arr[i]); avg=sum/5;
}
printf("\n Output of array element=\n"); printf("Sum of array elements=%d",sum)
for(i=0; i<5;i++) printf("Avg of array element=%d",avg)
{
printf("\n%d",&arr[i]); return 0;
}
return 0; }
}

PPT CREATED BY : Prof. Rajesh K. Jha 5


C - Programming Language

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
2. Two-Dimension Array : Value store Array b value representation in the
Basic, Control Structure, Looping, Array, Pointer

in rows and column format. format of matrix in memory.


Declaration of T.D.A : int b [row size][column size];
int b[10][25]; c1 c2
int b[2][2] ={{10,20} {30,40}} r1 (0,0) (0,1)
10 20
Note: Array variable b stores 4 elements with the int b[2][2]
help of row index and column index. (1.0) (1,1)
r2 20 40

b[0][0] = 10
b[0][1] = 20
b[1][0] = 30
b[1][1] = 40

PPT CREATED BY : Prof. Rajesh K. Jha 6


C - Programming Language
Program : Two dimensional array.

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
# include <stdio.h>
int main()
Basic, Control Structure, Looping, Array, Pointer

Direct Initialization : is the technique by which a


{
value is being assigned at the time of declaration on
int arr[2][2], i,j;
array.
arr[0][0] = 10 ;
arr[0][1] = 20 ;
# include <stdio.h> arr[1][0] = 30 ;
int main() arr[1][1] = 40 ;
{
int arr[2][2] = {{10,20},{30,40}}; printf("\n Output of two D. array element=\n");
int i,j;
printf("\n Output of two D. array element=\n"); for(i=0; i<2;i++)
for(i=0; i<2;i++) {
{ for(j=0; j<2; j++)
for(j=0; j<2; j++) {
{ printf("\n%d",arr[i][j]);
printf("\n%d",arr[i][j]); }
} }
} return 0;
return 0; }
}
PPT CREATED BY : Prof. Rajesh K. Jha 7
C - Programming Language
Program : Two dimensional array.

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
Basic, Control Structure, Looping, Array, Pointer

Through the Keyboard : is the technique by which


array value entered at the time of running of program.

[1].
printf("\n Output element of two D. array =\n");
for(i=0; i<2;i++)
# include <stdio.h> {
int main() for(j=0; j<2; j++)
{ {
int arr[2][2], i,j; printf("\n%d",arr[i][j]);
}
printf("\n Enter element in two D. array =\n"); }

for(i=0; i<2;i++) return 0;


{ }
for(j=0; j<2; j++)
{
scanf("\n%d",&arr[i][j]);
}
PPT CREATED}BY : Prof. Rajesh K. Jha 8
C - Programming Language
Program : Two dimensional array.

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
Basic, Control Structure, Looping, Array, Pointer

Through the Keyboard : is the technique by which


array value entered at the time of running of program.

[2].
# include <stdio.h> avg = sum/4;
int main()
{ printf("\n Sum of two d array element=%d”, sum");
int arr[2][2], i,j , sum=0, avg=0; printf("\n Avg of two d array element=%d”, avg");

printf("\n Enter element in two D. array =\n"); return 0;

for(i=0; i<2;i++) }
{
for(j=0; j<2; j++)
{
scanf("\n%d",&arr[i][j]);
sum = sum+arr[i][j];
}
}
PPT CREATED BY : Prof. Rajesh K. Jha 9
C - Programming Language
2. WAP in C to accept two array element printf("Enter second Array element:");

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
into matrix of size 3*3. Find out product of
two array element in third matrix array, that for(r=0;r<3;r++)
Basic, Control Structure, Looping, Array, Pointer

size is also 3*3 format. {


for(c=0;c<3; c++)
#include<stdio.h> {
#include<conio.h> scanf("%d",&num1[r][c]);
int main() }
{ }
int num[3][3],num1[3][3],num2[3][3],
int r,c,sum=0,avg=0; for(r=0;r<3;r++)
printf("Enter first Array element:"); {
for(r=0;r<3;r++) for(c=0;c<3; c++)
{ {
for(c=0;c<3; c++) num2[r][c]=num[r][c]*num1[r][c];
{ }
scanf("%d",&num[r][c]); }
} printf("\n Product of two array element:\n");
} for(r=0;r<3;r++)
{
for(c=0;c<3; c++)
{
PPT CREATED BY : Prof. Rajesh K. Jha 10
printf("%d\t",num2[r][c]);
C - Programming Language
3. WAP in C to accept array element in 3*3 for(r=0,c=2;c>=0; r++, c--)

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
matrix format. Find out sum of diagonal {
elements. diag2=diag2+num[r][c];
Basic, Control Structure, Looping, Array, Pointer

}
#include<stdio.h>
#include<conio.h> for(r=0;r<3;r++)
int main() {
{ for(c=0;c<3; c++)
int num[3][3],diag1=0,diag2=0,r,c; {
printf("Enter first Array element:"); printf("%d\t",num[r][c]);
for(r=0;r<3;r++) }
{ printf("\n");
for(c=0;c<3; c++) }
{
scanf("%d",&num[r][c]); printf("\n Sum of First diagonal
} element:%d",diag1);
}
printf("\n Sum of Second diagonal
for(c=0;c<3; c++) element:%d",diag2);
{ return 0;
diag1=diag1+num[c][c]; }
}
PPT CREATED BY : Prof. Rajesh K. Jha 11
C - Programming Language
4. Array element transposition program in 3*3

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
matrix. printf("Enter second Array element:");
Basic, Control Structure, Looping, Array, Pointer

for(r=0;r<3;r++)
#include<stdio.h> {
#include<conio.h> for(c=0;c<3; c++)
{
int main() num1[r][c]=num[c][r];
{ }
int num[3][3],num1[3][3],num2[3][3], }
int r,c,sum=0,avg=0;
printf("\n Transpose of array element:\n");
printf("Enter first Array element:"); for(r=0;r<3;r++)
{
for(r=0;r<3;r++) for(c=0;c<3; c++)
{ {
for(c=0;c<3; c++) printf("%d\t",num1[r][c]);
{ }
scanf("%d",&num[r][c]); printf("\n");
} }
} return 0;
}
PPT CREATED BY : Prof. Rajesh K. Jha 12
C - Programming Language

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
Array element transposition program
Basic, Control Structure, Looping, Array, Pointer

in 3*3

1 2 3 1 4 7

4 5 6 2 5 9

7 8 9 3 6 9

PPT CREATED BY : Prof. Rajesh K. Jha 13


C - Programming Language
Program : Three or multi-dimensional

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
array. 1st method of initialize value in 3d array.
Basic, Control Structure, Looping, Array, Pointer

Direct Initialization : is the technique by which a


int x[2][3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
value is being assigned at the time of declaration
on array. 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23};
Syntax : int arr [2] [3][4]
Diagram representation of multi-dimensional array.
Where,
Matrix – 0 Matrix – 1
1. First dimension of arr variable shows 0 1 2 3 0 1 2 3
numbers of matrix / block of array.
0 0 1 2 3 0 12 13 14 15
2. Second dimension shows row size of array.
3. Third dimension shows column size of array. 1 4 5 6 7 1 16 17 18 19
2 8 9 10 11 2 20 21 22 23
Access Element from first matrix : [0][0][0] = 0
Access Element from second matrix : [1][0]0] = 12
PPT CREATED BY : Prof. Rajesh K. Jha 14
C - Programming Language
Program : Three or multi-dimensional

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
array.
Basic, Control Structure, Looping, Array, Pointer

2nd method of initialize value in 3d array.


Direct Initialization : is the technique by which a int x[2][3][4] =
value is being assigned at the time of declaration {
on array. { {0,1,2,3}, {4,5,6,7}, {8,9,10,11} }, - > first matrix
{ {12,13,14,15}, {16,17,18,19}, {20,21,22,23} }
};
Syntax : int arr [2] [3][4]
Diagram representation of multi-dimensional array.
Where,
Matrix – 0 Matrix – 1
1. First dimension of arr variable shows 0 1 2 3 0 1 2 3
numbers of matrix / block of array.
0 0 1 2 3 0 12 13 14 15
2. Second dimension shows row size of array.
3. Third dimension shows column size of array. 1 4 5 6 7 1 16 17 18 19
2 8 9 10 11 2 20 21 22 23
Access Element from first matrix : [0][0][0] = 0
Access Element from second matrix : [1][0]0] = 12
PPT CREATED BY : Prof. Rajesh K. Jha 15
C - Programming Language
Program : Three dimensional array. # include <stdio.h>

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
int main()
{
Basic, Control Structure, Looping, Array, Pointer

Direct Initialization : is the technique by which a


int x[2][3][4] =
value is being assigned at the time of declaration on
{
array.
{ {0,1,2,3}, {4,5,6,7}, {8,9,10,11} },
{ {12,13,14,15}, {16,17,18,19}, {20,21,22,23} }
# include <stdio.h> };
int main()
{ int i,j,k;
int arr[2][2][2] = {1,2,3,4,5,6,7,8}; printf("\n Output of two D. array element=\n");
int i,j,z; for(i=0; i<2;i++)
printf("\n Output of two D. array element=\n"); {
for(i=0; i<2;i++) for(j=0; j<2; j++)
{ {
for(j=0; j<2; j++) for(k=0; k<2; k++)
{ {
for(k=0; k<2; k++) printf (“%d”,arr[i][j][k]);
{ }
printf (“%d”,arr[i][j][k]); }
} }
} return 0;
PPT CREATED}BY : Prof. Rajesh K. Jha } 16
C - Programming Language
Through the Keyboard : is the technique by

List, Stack, Queue,Graph,..etc.


Function, Structure, Union, File Handling, Exception, Link
which array value entered at the time of running
of program. printf("\n3 D. array element output =\n");
Basic, Control Structure, Looping, Array, Pointer

for(i=0; i<2;i++)
# include <stdio.h>
{
int main()
for(j=0; j<3; j++)
{
{
int arr[2][3][4] , i,j,z;
for(k=0; k<4; k++)
{
printf("\n Input array element in 3 D. array =\n");
printf (“%d”,arr[i][j][k]);
}
for(i=0; i<2;i++)
printf(“\n”);
{
}
for(j=0; j<3; j++)
printf(“\n”);
{
}
for(k=0; k<4; k++)
{
return 0;
scanf (“%d”,arr[i][j][k]);
}
}
}
}
PPT CREATED BY : Prof. Rajesh K. Jha 17

You might also like