0% found this document useful (0 votes)
58 views24 pages

Array in C Programming

The document discusses different types of arrays in C programming. It defines arrays as fixed size collections of elements of the same data type. It then describes how to declare and initialize one-dimensional, two-dimensional, and multi-dimensional arrays at compile-time and run-time. Specific examples are provided for declaring, initializing, inputting values into, and outputting two-dimensional arrays. Key concepts like searching and sorting algorithms are also summarized.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
58 views24 pages

Array in C Programming

The document discusses different types of arrays in C programming. It defines arrays as fixed size collections of elements of the same data type. It then describes how to declare and initialize one-dimensional, two-dimensional, and multi-dimensional arrays at compile-time and run-time. Specific examples are provided for declaring, initializing, inputting values into, and outputting two-dimensional arrays. Key concepts like searching and sorting algorithms are also summarized.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 24

REF.

BOOK PROGRAMMING IN ANSI C-


TH
7 EDITION
E. BALAGURUSAMY

1
2
LEARNING OBJECTIVES:

Determine how one-


Define the concept of
dimensional array is
arrays
declared and initialized

Discuss how two-


Know the concept of
dimensional array is
two-dimensional arrays
declared and initialized

Describe multi- 3
Explain dynamic arrays
dimensional arrays
INTRODUCTION:

NORMALLY ONE VARIABLE OF ONE DATATYPE CAN HOLD ONLY


ONE VALUE

4 4
What is array?
An array is a fixed size sequenced collection of elements of the SAME
datatype.
Syntax to declare array:
datatype variable-name [size];

5
We don’t need to declare values for
elements. Like below:

6
One-dimensional:
Just by declaring one variable we can put an INDEX number which is merely the number variables.
We can index starting with 0 which is preferred by computer.
But we shouldn’t get confused so we can state indexing with 1.

7
7
OR,

8
8
Compile Time Initialization:
We can initialize the elements of array in same way as
the ordinary variable when they are declared.

Datatype array-name [size] = {list of values}


Int number[3] = {0, 1, 2};

We can omit the size during compile time initialization


only.
int number[ ] = {1, 2, 3, 4};

This approach works fine as long as we initialize every


element in the array. 9

9
Character array Initialization :

char name[ ] = {‘J’,ʻe’,ʻn’,ʻc’,ʻy’,ʻ\0’};


or,
char name[ ]=ʺJency”;
Compile time initialization may be partial. That is, the number of initialize may be
less than the declared size.

int number[5]={10, 20, 30};

Here, array index number initialized is 5 but there are 3 elements.


The remaining 2 places are Zero and if the array type is char the Null.

int number[2] = {1,2,3,4};

In this case the declared size has more initialized elements. The compiler will
create error. 10
Run time initialization:

An array can be Explicitly initialized at run time. This approach is usually applied for
initializing user input data. Using for loop can help in this case.

11

11
We can’t leave size empty in array

12

12
Searching And Sorting:

Sorting: The process of arranging elements in the list according to their values, in ascending or
descending order. A sorted list is called an ordered list. Sorted lists are especially important in list
searching because they facilitate rapid search operation.
Important and simple sorting techniques:

 Bubble sort  Shell sort


 Selection sort  Merge sort
 Insertion sort  Quick sort

Searching: The process of finding the location of the specific element in a list. The specified element is often
called the search key. If the search key with list element values, the search is said to be successful else
unsuccessful.
The most commonly used search techniques are:

 Sequential Search
 Binary Search

13
13
TWO-DIMENSIONAL ARRAYS:
We represent a particular value in matrix using 2 subscripts such as vrc here, r is for row
and c is for column.
Syntax:
datatype array_name[row_size][column_size];

Like single-dimensional arrays, each dimension of the array is indexed from zero to its maximum size-1.
The first index selects the row and the second index selects the column within row.

1414
2D Array Compile time initialization:
Array size declaration and values initialized in braces:
int table[2][3]={0, 0, 0, 1, 1, 1};

OR,
The initialization is done
row by row
int table[2][3] = {{0, 0, 0},{1, 1, 1} };

OR,
int table[2][3] = { {0, 0, 0},
{1, 1, 1} We can initialize a two
}; dimensional array in the
form of matrix
15
15
OR,
int table[ ][3] = { When the array is completely
{0, 0, 0}, initialized with all values, explicitly,
{1, 1, 1} we need not specify the size of the
}; dimension
*****************************
int table[2][3] = {
{1,2}, If the values are missing in
{2} initialize, they are
}; automatically set to Zero

*****************************
int table[2][3] = {{0}, {0}, {0}};
or, When all the elements are
to be initialized to Zero, this
int table[2][3] = {0, 0}; short-cut may be used 16
16
#include <stdio.h>

int main () {

/* an array with 5 rows and 2 columns*/


int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
int i, j;

/* output each array element's value */

for ( i = 0; i < 5; i++ ){


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

return 0;
} 17
17
2D matrix Run time Input and Display:
#include<stdio.h>
#define MAX 10

int main()
{ printf("Your entered 2D matrix of %dX%d
int array[MAX][MAX],i, j, r, c; elements:\n", r, c);
for(i=0;i<r; i++)
printf("Enter row and column number:\n"); {
scanf("%d %d", &r, &c); for(j=0;j<c; j++)
{
printf("Enter %d X %d elements:\n", r, c); printf("%5d", array[i][j]);
for(i = 0; i <r; i++) }
{ printf("\n");
for(j=0;j<c; j++) }
{ return 0;
printf("Enter array[%d][%d]: ",i+1,j+1); }
scanf("%d", &array[i][j]);
}
}
18
18
19
19
MULTI-DIMENSIONAL ARRAYS:

C allows three or more dimensions.


The exact limit is determined by the compiler. It's an array or collection of 2D arrays, and
a 2D array is an array of 1D array.
The general form of a multi-dimensional array is

datatype arrary_name[s1][s2]…..[si];
here si is size of i-th dimension.

Examples:
int survey[3][5][12]; >>holds: 3*5*12=180 integer type elements<<

20
20
Declaration and Initialization 3D Array :
#include<stdio.h>
int main() {37, 38, 39}
{ },
int i, j, k; };
int arr[3][3][3]=
{ printf(":::3D Array Elements:::\n\n");
{ for(i=0;i<3;i++)
{11, 12, 13}, {
{14, 15, 16}, for(j=0;j<3;j++)
{17, 18, 19} {
}, for(k=0;k<3;k++)
{ {
{21, 22, 23}, printf("%d\t", arr[i][j][k]);
{24, 25, 26}, }
{27, 28, 29} printf("\n");
}, }
{ printf("\n");
{31, 32, 33}, }
{34, 35, 36}, return 0; 21
21

}
22
22
Dynamic Array:
We create arrays at compile time. An array created at compile
time by specifying SIZE in the source code has a fixed size and
cannot be modified at run time. The process of allocating
memory at compile time is known as Static Memory Allocation.

Considering a situation where we want to use array that can vary


greatly in size. In C it is possible to allocate memory to array at
run time are called Dynamic arrays.

Dynamic arrays are created using what are known as pointer


variables and memory management function malloc, calloc and
realloc. These functions are included in header file <stdlib.h>.
These are used in data structure such as linked lists, stacks and
queues. 23
THANK YOU!!!!!!
FOR YOUR PATIENCE
JENCY

You might also like