chp 2 arrays
Download
Skip this Video
Download Presentation
CHP-2 ARRAYS

Loading in 2 Seconds...

play fullscreen
1 / 14

CHP-2 ARRAYS - PowerPoint PPT Presentation


  • 111 Views
  • Uploaded on

CHP-2 ARRAYS. 1.INTRODUCTION. An array is a collection of data elements of similar data types. The data elements grouped in an array can be of any basic data types like integer, float or character or user-defined data types. 2.ARRAYS.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' CHP-2 ARRAYS' - vea


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
1 introduction
1.INTRODUCTION
  • An array is a collection of data elements of similar data types.
  • The data elements grouped in an array can be of any basic data types like integer, float or character or user-defined data types.
2 arrays
2.ARRAYS
  • One of the data types, which can be used for storing a list of elements, is an array.
  • Whenever programmers want to store a list of elements under a single variable name but still want to access and manipulate an individual element of the list, arrays are used.
  • Arrays are defined as fixed size sequence of elements of the same data type. These elements are stored at continuous memory locations and can be accessed sequentially or randomly.
  • The program can access a particular element of an array by using one or more indices or subscripts.
  • If only one subscript is used, an array is known as the single-dimensional array.
  • If more than one subscript is used, an array is known as the multi-dimensional array.
3 single dimensional arrays
3.SINGLE-DIMENSIONAL ARRAYS
  • A single-dimensional array is defined as an array in which only one subscript value is used to access its elements. It is the simplest form of an array. Generally, a single-dimensional

array is denoted as

array_name [L:U]

where,

array_name = the name of the array

L = the lower bound of the array

U = the upper bound of the array

  • Before using an array in a program, it needs to be declared. The syntax of declaring a

Single-dimensional array in C is

data_type array_name[size];

where,

data_type = data type of elements to be stored in array

array_name = name of the array

s i z e = the size of the array indicating that the lower bound of the array is 0 and

the upper bound is s i z e -1. Hence, the value of the subscript ranges from 0 to

s i ze -1.

  • For example, in the statement intabc [ 5 ] , an integer array of five elements is declared and the array elements are indexed from 0 to 4. Once the compiler reads a single-dimensional array declaration, it allocates a specific amount of memory for the array. Memory is allocated to the array at the compile-time before the program is executed.
initializing and accessing single dimensional array
Initializing and Accessing Single-Dimensional Array
  • An array can be initialized in two ways: by declaring and initializing it simultaneously or by accepting elements for the already declared array from the user.
  • These elements can be accessed by using the combination of the array name and the subscript value.

Program 2.1: A program to illustrate initialization of two arrays and display their elements

#include<stdio.h>

#include<conio.h>

void main ()

{

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

int B[5l, i;

clrscr() ;

printf("Enter the elements of array B:\n");

for (i=O; i<5; i++)

printf("Enter the element:");

scanf("%d", &B[il);

printf("Elements of array A: \n");

for (i=O;i<5;i++)

printf("%d\t", A[il);

printf("\nElements of array B: \n");

for (i=O;i<5;i++)

printf("%d\t", B[il);

getch ();

}

initializing and accessing single dimensional array1
Initializing and Accessing Single-Dimensional Array

The output of the program is

Enter the elements of array B:

Enter the element: 6

Enter the element: 7

Enter the element: 8

Enter the element: 9

Enter the element: 10

Elements of array A:

1 2 3 4 5

Elements of array B:

6 7 8 9 10

  • Once an array is declared and initialized, various operations, such as traversing, searching,
  • insertion, deletion, sorting and merging can be performed on an array.
  • To perform any operation on an array, the elements of the array need to be accessed. The process of accessing each element of an array is known as traversal.
slide7

Program 2.2: A program to illustrate the traversal of an array

#include<stdio.h>

#include<conio.h>

void main ()

{

int ARR[5];

Inti,sum=0;

clrscr();

for (i=O;i<3;i++)

{

printf("Enter the elements of the array:\n");

scanf("%d", &ARR[i]);

}

for (i=O;i<3;i++)

{

printf("%d", ARR[i]);

}

for (i=O;i<3;i++)

{

Sum=sum + ARR[i];

}

Printf(“Sum=%d”,sum);

getch();}

slide8

The output of the program is

Enter the elements of the array:

12 23 34 45 56

The elements of the array are:

12 23 34 45 56

Sum of elements of an array: 170

4 two dimensional arrays
4.Two-Dimensional arrays
  • A two-dimensional array is defined as an array in which two subscript values are used to access an array element.
  • Two-dimensional arrays are useful when the elements being arranged in the form of rows and columns (matrix form).
  • Generally, a two-dimensional array is represented as

A[Lr : Ur, Lc : Uc]

where,

Lr and Lc = the lower bounds of the row and the column, respectively

Ur and Uc = the upper bounds of the row and the column, respectively

  • The syntax of declaring a two-dimensional array in C is
  • data type array_name [row_size] [column_size];
  • For example, in the statement int a [3] [3] an integer array of three rows and three columns is declared.
initializing and accessing two dimensional arrays
Initializing and Accessing Two-dimensional Arrays
  • Like a single-dimensional array, a two-dimensional array can also be initialized in two ways: by declaring and initializing the array simultaneously and by accepting array elements from the user.
  • Once a two-dimensional array is declared and initialized, the array elements can be accessed anytime.
  • two-dimensional array elements are also accessed by using the combination of the name of the array and subscript values.
slide11

Program 2.3: A program to illustrate traversal of a matrix (two-dimensional array) and finding sum of its elements

#include<stdio.h>

#include<conio.h>

void main ()

{

int ARR[3] [3], i, j;

clrscr();

printf("Enter the elements of matrix A: \n");

for(i=O;i<3;i++)

{

for(j=O;j<3;j++)

{

Printf(“Enter elements\n”);

scanf ("%d", &ARR[i] [j]);

}

}

for(i=O;i<3;i++)

{

for(j=O;j<3;j++)

{

printf ("%d", &ARR[i] [j]);

Sum=sum+ARR[i][j];

}

}

Printf(“sum=%d”,sum);

getch ();

}

slide12

The output of the program is

Enter the elements of matrix A:

1 2 3

4 5 6

7 8 9

Sum of elements of a matrix is: 45

5 three dimensional arrays
5.Three-Dimensional Arrays
  • A three-dimensional array is defined as an array in which three subscript values are used to access an individual array element.
  • The three-dimensional array can be declared as shown here.

int A[3] [3] [3];

program 2 4 a program to illustrate the traversal of a three dimensional array
Program 2.4: A program to illustrate the traversal of a three-dimensional array

#include<stdio.h>

#include<conio.h>

void main ()

{

int ARR[3] [3] [3], i, j, k;

clrscr();

for(i=O;i<3;i++)

{

for (j=O;j<3;i++)

{

for (k=O;k<3;k++)

{

printf("Enter the elements of an array A(3x3x3) :\n");

scanf("%d", &ARR[i] [j] [k]);

}

}

}

ad