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

Arrays: Designed By: Harendra Singh Dhaila, Jaycees Public School, Rudrapur, Uttrakhand

This document defines and describes arrays, including one-dimensional and multi-dimensional arrays. It discusses declaring, initializing, and accessing array elements. Sample programs are provided to demonstrate one-dimensional and two-dimensional arrays. The document also covers addressing calculations in two-dimensional arrays using row-major and column-major ordering and different sorting algorithms like selection, bubble, and insertion sort.

Uploaded by

nishii29
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Arrays: Designed By: Harendra Singh Dhaila, Jaycees Public School, Rudrapur, Uttrakhand

This document defines and describes arrays, including one-dimensional and multi-dimensional arrays. It discusses declaring, initializing, and accessing array elements. Sample programs are provided to demonstrate one-dimensional and two-dimensional arrays. The document also covers addressing calculations in two-dimensional arrays using row-major and column-major ordering and different sorting algorithms like selection, bubble, and insertion sort.

Uploaded by

nishii29
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

Arrays

Designed By: Harendra Singh Dhaila, Jaycees Public School, Rudrapur, Uttrakhand

Definition
Array is a collection of variables of same data type with a common name.

Types of Arrays
One Dimensional Arrays

A[0] A[1] A[2] A[3] A[4] A[5]


Two Dimensional Arrays
A[0][0] A[0][1] A[0][2] A[0][3] A[0][4] A[1][0] A[1][1] A[1][2] A[1][3] A[1][4
A[2][1] A[2][2] A[2][3] A[2][3] A[2][4]

Multi Dimensional Arrays 3D Array, 4D Arrays etc.

Declaration of Array
One Dimensional Arrays
int A[10]; float B[20]; char C[20];

Two Dimensional Arrays


int X[5][5]; char Y[5][25];

Initialization of the Array


int A[5]= {1,2,3,4,5}; int ARR[ ] = {1,2,3,4,5,6}; char B[20]=Rudrapur; float C[5]={2.4,3.5,1.5}; int D[2][3]={{1,2}, {3,4}, {5,6}};

Accessing Array Elements


In C++ first element of the array is always at zero position.

A[0] A[1] A[2] A[3] A[4] A[5]

Sample Program on 1-D Array


#include<iostream.h> void main() { int A[10],i; for(i=0;i<10;i++) { cout<<Enter a number: ; cin>A[i]; } cout<<\n Array Contents\n; for(i=0;i<10;i++) { cout<<A[i]<< ; } }

Sample Program on 2-D Array


#include<iostream.h> void main() { int A[3][4],i,j; for(i=0;i<20;i++) for(j=0j<4;j++) { cout<<Enter a number: ; cin>A[i]; } cout<<\n Array Contents\n; for(i=0;i<10;i++) { for(j=0j<4;j++) cout<<A[i]<< ; cout<<endl; } }

Address Calculation in 2D Arrays


Row Major Column Major
Base Address : 1000

Size of Each Element : 2 Bytes 0 0 1 2 3 1 2 3 4 5

Address of Location X In Row Major : 1032 In Column Major : 1016

Y X

Address of Location Y
In Row Major : 1018

In Column Major : 1032

Address Calculation in Row Major


A[i][j]= B + [i*n + j]

Where B=Base address of the array n = Total column of the array i = Row number of desired row j = Column number of desired row

Address in Column Major


A[i][j]= B + [i + j*m]

Where B=Base address of the array m = Total rows of the array i = Row number of desired row j = Column number of desired row

Sorting
Arranging number in order (either ascending or descending) is called sorting.
TYPES OF SORTING Selection Sort Bubble Sort Insertion Sort

You might also like