zueducator

Online education for all on health, science, technology, business and management...

ad3

Thursday, June 9, 2022

12 Ways to Apply Arrays in C++

 12 Ways to Apply Arrays in C++

Introduction

This article is basically designed to discuss some useful ways to write C++ programs using an array. The paper basically deals with twelve different ways of writing C++ programs using arrays which are considered the most effective tool for a programmer to solve desired problems. So, let us define, understand and discuss different methods and approaches to an array using C++.

Array

·   An array is a collection of similar elements.

·   An array refers to a collection of variables of the same data type that are referenced by a common name.

·   In the C/C++ programming language, an array can be defined as a number of memory locations, each of which can store the same data type and which can be referenced through the same variable name.

·   An array is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key. An array is stored so that the position of each element can be computed from its index tuple or vector by a mathematical formula.

·   An array is a linear data structure whose elements form a sequence and whose elements/members are represented in memory by means of sequential (contiguous) memory locations.

·   Arrays can have data items/elements/members of fundamental data types like char, int, float, and double and user-defined data types like structures and objects. For example, marks obtained by 10 students in an exam, salaries of 10 employees and number of chairs kept in a hall, etc.

Importance of an array in C++

An array is one of the oldest and most important data structures and is used by almost every programmer. Arrays are very much useful in a situation where many elements of the same data types need to be stored and processed. Arrays are often used to implement tables, especially look-up tables (synonym of the array).  Arrays are used to implement data structures like linked lists, hash tables, search trees, and strings. Using array (linear data structure) a programmer can easily insert, delete, merge, sort, search and traverse data elements/members. Arrays effectively exploit the addressing logic of a computer. In most modern computers and many external storage devices, the memory is a one-dimensional array of words whose indices are their addresses. Arrays are useful mostly because the element indices (denoted by n) can be computed at runtime.

Types of array

Generally, there are two types of array namely 1) one-dimensional array and 2) multi-dimensional array. Now let us discuss each one separately.

 One dimensional array

One dimensional array refers to a type of linear array comprised of finite homogeneous elements.

 The basic syntax to declare a one-dimensional array is given by

Data type Array-name [size of array];

For example, int roll[8];

In this case, int specifies the data type of the variable,” roll” specifies the name of the variable or array name, the bracket([]) tells the compiler that it is an array and the number indicated in the bracket specifies the total number of data items/elements/members that this array can hold or store.

Fig1: Illustration of a one-dimensional array

From the above discussion, we can easily conclude that we have declared an array of integer data types, whose name is “roll “ and it can hold or store the roll number of 8 students.

 

An array can be initialized in the following manner:

Basic syntax to initialize an array is:

Data-type array-name [size] = {list of values};

For example, int roll[8]={12,45,32,23,17,49,5,11}; 

OR

int roll[8]; // Declaration of an array

roll[0]=12;

roll[1]=45;

roll[2]=32;

roll[3]=23;            //Assigning values to an array roll[8]

roll[4]=17;

roll[5]=49;

roll[6]=5;

roll[7]=11;

OR

int roll[ ]={12,45,32,23,17,49,5,11};

In this case, the compiler automatically assumes its size from given element values.

 

Some useful ways to use an array in C++are:

Numbers as an array

A program to demonstrate the 3 different ways of array declaration and then accessing array elements.

#include<iostream.h>//Function prototype

#include<conio.h>

void main()

{

int ar1[3]={5,10,15};  // array declaration and initialization

int ar2[ ]={3,4,5};

int ar3[3];

ar3[0]=1;     //array initialization statement        

ar3[1]=2;

ar3[2]=3;

cout<<”\n”<<”Accessing members/elements of first array:”;

cout<<”\n”<<ar1[0]<<”\n”<<ar1[1]<<”\n”<<ar1[2];

cout<<”\n”<<”Accessing members/elements of second array:”;

cout<<”\n”<<ar2[0]<<”\n”<<ar2[1]<<”\n”<<ar2[2];

cout<<”\n”<<”Accessing members/elements of third array:”;

cout<<”\n”<<ar3[0]<<”\n”<<ar3[1]<<”\n”<<ar3[2];

getch();

}

Output:

Accessing members/elements of first array:

5

10

15

Accessing members/elements of second array:

3

4

5

Accessing members/elements of third array:

1

2

3

 

Numbers as an array and for loop

For loop

A for loop refers to a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

The basic syntax of a for loop in C++ is −

for ( initialization; condition; increment )

{

   statement(s);

}

 

A program to demonstrate the different ways of assigning values to an array and accessing array elements using for loop.

#include<iostream.h>

#include<conio.h>//Function prototype

void main()

{

int a;

int ar1[3]={5,10,15};  // array declaration and initialization

int ar2[ ]={3,4,5};

int ar3[3];

int ar4[3];

ar4[0]=1;

ar4[1]=2;                  //array initialization statement

ar4[2]=3;

cout<<”\n”<<”Assigning values to an array:”<<endl;

for(a=0;a<3;a++)

{

cin>>ar3[a];

}

cout<<”\n”<<”Accessing members/elements of first array:”;

for(a=0;a<3;a++)

{

cout<<”\n”<<ar1[a];

}

cout<<”\n”<<”Accessing members/elements of second array:”;

for(a=0;a<3;a++)

{

cout<<”\n”<<ar2[a];

}

 

cout<<”\n”<<”Accessing members/elements of third array:”;

for(a=0;a<3;a++)

{

cout<<”\n”<<ar3[a];

}

cout<<”\n”<<”Accessing members/elements of fourth array:”;

for(a=0;a<3;a++)

{

cout<<”\n”<<ar4[a];

}

getch();

}

Output:

Assigning values to an array:

10

20

30

Accessing members/elements of first array:

5

10

15

Accessing members/elements of second array:

3

4

5

Accessing members/elements of third array:

10

20

30

Accessing members/elements of fourth array:

1

2

3

 

Strings as an array

String

A string refers to an ordered sequence of characters.

·   A string is a variable that stores a sequence of letters or other characters, such as "Welcome" or "Welcome to India".

·   In C++ the enclosing delimiters are double-quotes.

A program to demonstrate the different ways of assigning string values to an array and accessing array elements using for loop.

#include<iostream.h>

#include<conio.h>//Function prototype

#include<string.h>

#include<stdio.h>

void main()

{

int a;

char str1[3]={‘D’,’a’,’D’};             // array declaration and initialization

int ar2[ ]={3,4,5};

char str2[6]=”Ramesh”;

char str3[16]=”welcome to India”;

char str4[5];

char str5[16];

cout<<”\n”<<”Assigning values to fourth array:”<<endl;

for(a=0;a<5;a++)

{

cin>>str5[a];

}

cout<<”\n”<<”Assigning values to fifth array:”<<endl;

gets(str5);

cout<<”\n”<<”Accessing members/elements of first array:”<<endl;

for(a=0;a<3;a++)

{

cout<<”\t”<<str1[a];

}

cout<<”\n”<<”Accessing members/elements of second array:”<<endl;

for(a=0;a<3;a++)

{

cout<<str2[a];

}

cout<<”\n”<<”Accessing members/elements of third array:”<<endl;

for(a=0;a<16;a++)

{

cout<<str3[a];

}

cout<<”\n”<<”Accessing members/elements of fourth array:”<<endl;

for(a=0;a<5;a++)

{

cout<<str4[a];

}

cout<<”\n”<<”Accessing members/elements of fifth array:”<<endl;

for(a=0;a<16;a++)

{

cout<<str5[a];

}

getch();

}

Output:

Assigning values to fourth array:

Patna

Assigning values to fifth array:

Welcome to Patna

Accessing members/elements of first array:

       D   a   D

Accessing members/elements of second array:

Ramesh

Accessing members/elements of third array:

Welcome to India

Accessing members/elements of fourth array:

Patna

Accessing members/elements of fifth array:

Welcome to Patna

 

A program to demonstrate the different ways of assigning string value of any  length to an array and accessing array elements using for loop and puts() function.

#include<iostream.h>

#include<conio.h>

#include<string.h>//Function prototype

#include<stdio.h>

void main()

{

int a; //local variable declaration statement

char str1[ ]=”welcome to India”;

char str2[ ]=”welcome to Jharkhand”;

clrscr();

cout<<”\n”<<”First string is:”;

for(a=0;a<16;a++)

{

cout<<str1[a];

}

cout<<”\n”<<”String length1:”<<strlen(str1);

cout<<”\n”<<”Second string is:”;

puts(str2);

cout<<”\n”<<”String length2:”<<strlen(str2);

getch();

}

Output:

First string is: welcome to India

String length1: 16

Second string is: welcome to Jharkhand

String length2: 20

 

 

An array of strings using a pointer

Pointer

A pointer refers to a variable whose value is the address of another variable. 

The general form of a pointer variable declaration is –

Data type *variable-name;

A program to display strings using array and pointer.

#include<iostream.h>

#include<conio.h>

#include<string.h>

#include<stdio.h>

void main()

{

char *ar[3];

ar[0]=”welcome”;

ar[1]=”to”;

ar[2]=”India”;

cout<<”\n”<<”String is:”<<endl;

cout<<”  ”<<ar[0]<<”  ”<<ar[1]<<”   ”<<ar[2];

getch();

}

Output:

String is:

Welcome to India

 

Array and function

Function

A function refers to a group of statements that together perform a task.

·       Every C++ program has at least one function, which is main().

·       A function declaration tells the compiler about a function's name, return type, parameters, and how to call the function.

·       A function definition provides the actual body of the function.

A program to demonstrate the different ways of declaring and assigning integer values to an array and accessing array elements using for loop and function without parameter or argument.

#include<iostream.h>//Function prototype

#include<conio.h>

void a1( )              //function declaration statement

{

int a;

int ar1[3]={5,10,15};

cout<<”\n”<<”Accessing members/elements of the array:”<<endl;

for(a=0;a<3;a++)

{

cout<<”\n”<<ar1[a];

}

}

void a2( )           //function declaration statement

{

int a;

int ar2[3];

cout<<”\n”<<”Assigning members/elements of the array:”<<endl;

for(a=0;a<3;a++)

{

cin>>ar2[a];

}

cout<<”\n”<<”Accessing members/elements of the array:”<<endl;

for(a=0;a<3;a++)

{

cout<<”\n”<<ar2[a];

}

}

void main()

{

clrscr();

a1( );                        //calling function

a2( );

getch();

}

Output:

Accessing members/elements of the array:

5

10

15

Assigning members/elements of the array:

4

5

6

Accessing members/elements of the array:

4

5

6

 

A program to demonstrate the different ways of declaring and assigning integer values to an array and accessing array elements using for loop, pointer and function with parameters or arguments.

#include<iostream.h> //Function prototype

#include<conio.h>

void num1(int ar1[3]) //parameters are declared as an array

{

int a;

cout<<”\n”<<”Accessing members/elements of the array1:”<<endl;

for(a=0;a<3;a++)

{

cout<<”\n”<<ar1[a];

}

}

void num2(int ar2[ ])//parameters are declared as an array of arbitrary length

{

int a;

cout<<”\n”<<”Accessing members/elements of the array2:”<<endl;

for(a=0;a<3;a++)

{

cout<<”\n”<<ar2[a];

}

}

void num3(int *ar3) //parameters or arguments are declared as pointer

{

int a;

cout<<”\n”<<”Accessing members/elements of the array3:”<<endl;

for(a=0;a<3;a++)

{

cout<<”\n”<<ar3[a];

}

}

void main()

{

int a;

int n1[3];

int n2[3];//array declaration statement

int n3[3];

clrscr();

cout<<”\n”<<”Assigning members/elements of the array1:”<<endl;

for(a=0;a<3;a++)

{

cin>>n1[a];

}

cout<<”\n”<<”Assigning members/elements of the array2:”<<endl;

for(a=0;a<3;a++)

{

cin>>n2[a];

}

cout<<”\n”<<”Assigning members/elements of the array3:”<<endl;

for(a=0;a<3;a++)

{

cin>>n3[a];

}

num1(n1 );       //calling function with argument

num2(n2 );

num3(n3 );

getch();

}

Output:

Assigning members/elements of the array1:

1

2

3

Assigning members/elements of the array2:

4

5

6

Assigning members/elements of the array3:

7

8

9

Accessing members/elements of the array1:

1

2

3

Accessing members/elements of the array2:

4

5

6

Accessing members/elements of the array3:

7

8

9

 

Structure and array

Structure

·   A Structure refers to a user-defined datatype in C/C++language which allows us to combine data of different types together. 

·   A structure helps to construct a complex data type that is more meaningful.

 

A program to demonstrate the different ways of declaring and assigning a string, integer and floating values to an array and accessing array elements using for loop and structure.

#include<iostream.h>

#include<conio.h>

#include<string.h>//Function prototype

#include<stdio.h>

struct experiment

{

int no1[3];

char str[10];                //Array declaration statement

float no2[3];

} exp;

void main()

{

int a;

cout<<”\n”<<”Assigning a string:”;

gets(exp.str);

cout<<”\n”<<”Assigning integers:”<<endl;

for(a=0;a<3;a++)

{

cin>>exp.no1[a];

}

cout<<”\n”<<”Assigning floating numbers:”<<endl;

for(a=0;a<3;a++)

{

cin>>exp.no2[a];

}

cout<<”\n”<<”Displaying result:”<<endl;

cout<<”\n”<<”Accessing string:”;

cout.write(exp.str,10);

cout<<”\n”<<”Accessing integers:”<<endl;

for(a=0;a<3;a++)

{

cout<<”\n”<<exp.no1[a];

}

cout<<”\n”<<”Accessing floating numbers:”<<endl;

for(a=0;a<3;a++)

{

cout<<”\n”<<exp.no2[a];

}

getch();

}

Output:

Assigning a string: welcome

Assigning integers:

1

2

3

Assigning floating numbers:

2.5

3.5

4.5

Displaying result:

Accessing string: welcome

Accessing integers:

1

2

3

Accessing floating numbers:

2.5

3.5

4.5

 

 

A program to find the largest and the smallest number among numbers.

#include<iostream.h>

#include<conio.h>//Function prototype

void main()

{

int a,max,min;         //local variable declaration

clrscr();

int ar[5]={5,7,3,6,9};                 //array declaration and initialization

cout<<”\n”<<”Numbers are:”<<endl;

for(a=0;a<5;a++)

{

cout<<”\t”<<ar[a];

}

for(a=1;a<5;a++)

{

If(ar[a]<min)

{

min=ar[a];

}

else if(ar[a]>max)

{

max=ar[a];

}

}

cout<<”\n”<<”Maximum number:”<<max;

cout<<”\n”<<”Minimum number:”<<min;

getch();

}

Output:

Numbers are:

7      3 9

Maximum number: 9

Minimum number: 3

 

Array and bubble sort

Bubble sort

Bubble sort refers to a sorting technique that is typically used for sequencing small lists. It starts by comparing the first item to the second, the second to the 3rd, and so on until it finds one item out of order. It then swaps the two items and starts over.

Figure 2: Shows the working of bubble sort

 

5.1 Salient features

·   Bubble sort is a very popular sorting algorithm.

·   Bubble sort is an internal exchange sort.

·       Bubble sort works by repeatedly swapping adjacent elements that are out of order.

·       The sort may alternate from the top of the list to the bottom and then from the bottom to the top.

·       Bubble sort is also known as a sinking sort.

·       Bubble sort is intuitive, easy to write and debug.

·       Bubble sort consumes little memory.

 The process of bubble sort algorithm is:

·   For i =1 to A.length -1

·   For j= A.lengthdownto i+1

·   If A[j]<A[j-1]

·   Exchange A[j] with A[j-1]

                                             OR

For sorting an integer array using bubble sort.

for (int i=0; i<n; i++)

{

for (int b=0; b<n-1;b++)

{

if(array[b]>array[b+1])

{

int temp=array[b+1];

array[b+1]=array[b];

array[b]=temp;

}

}

}

                                                      OR

·   In each pass, the first two items in a list are compared and placed in the correct order.

·   Items two and three are then compared and re-ordered, followed by items three and four, and then four and five, and so on.

·   The sort continues until a pass with no swap occurs.

·   As a result, high-value items near the beginning of a list move to their correct position rapidly and are called turtles, because they move only one position with each pass.

 

The following program illustrates the implementation of the bubble sort.

A program to sort array elements in ascending order using bubble sort method.

#include<iostream.h>                 //Function prototype

#include<conio.h>

void main()

{

int a,b,flag,c;

int ar[5]={3,5,2,1,4};            //Array declaration

cout<<”\n”<<”Array is:”<<endl;

for(a=0;a<5;a++)

{

cout<<ar[a]<<”  ”;

}

for(a=0;a<5-1;a++)    //sorting process begins

{

flag=1;

for(b=0;b<(5-1-a);b++)

{

If(ar[b]>ar[b+1])

{

flag=0;

c=ar[b];

ar[b]=ar[b+1];

ar[b+1]=c;

}

}

If(flag)

break;

}

cout<<”\n”<<”sorted list is:”<<endl; //Accessing sorted array members

for(a=0;a<5;a++)

{

cout<<” ”<<ar[a];

}

getch();//freeze the screen until any key is pressed

}

Output:

Array is:

3 5    2 1 4

Sorted list:

1 2    3 4 5

OR

#include<iostream.h>         //Function prototype

#include<conio.h>

void main()

{

int a,b,c,n;

n=5;

int ar[5]={10,5,15,25,20};                        //Array declaration statement

cout<<”\n”<<”Array elements are:”<<endl;

for(a=0;a<n;a++)

{

cout<<”   ”<<ar[a];

}

for(a=0;a<(n-1);a++)      //sorting begins

{

for(b=0;b<(n-a-1);b++)

{

If(ar[b]>ar[b+1])

{

c=ar[b];

ar[b]=ar[b+1];

ar[b+1]=c;

}

}

}

cout<<”\n”<<”Sorted list is:”<<endl;

for(a=0;a<n;a++)

{

cout<<”   ”<<ar[a];

}

getch();                      //freeze the screen until any key is pressed

}

Output:

Array elements are:

10    5 15 25     20

Sorted list is:

5     10 15 20    25

 

A program to sort numbers using array and bubble sort.

#include<iostream.h>

#include<conio.h>//Function prototype

void main()

{

int a,b,flag,c;

int ar[5]={3,5,2,1,4};      //Array declaration

cout<<”\n”<<”Array is:”<<endl;

for(a=0;a<5;a++)

{

cout<<ar[a]<<”  ”;

}

for(a=0;a<5-1;a++)//sorting process begins

{

flag=1;

for(b=0;b<(5-1-a);b++)

{

If(ar[b]>ar[b+1])

{

flag=0;

c=ar[b];

ar[b]=ar[b+1];

ar[b+1]=c;

}

}

If(flag)

break;

}

cout<<”\n”<<”sorted list is:”<<endl;

for(a=0;a<5;a++)

{

cout<<” ”<<ar[a];

}

getch();

}

Output:

Array is:

3 5    2 1 4

Sorted list:

1 2    3 4 5

 

A program to reverse array elements by copying elements to second array.

#include<iostream.h>

#include<conio.h> //Function prototype

void main()

{

int ar1[5]={5,10,15,20,25};

int a,b, ar2[5];

cout<<”\n”<<”The array elements are:”<<endl;

for(a=0;a<5;a++)

{

cout<<” ”<<ar1[a];

}

// copying elements into array2 starting from end of array.

for(a=5-1,b=0;a>=0;a--,b++)

{

ar2[b]=ar1[a];

}

//copying reverse array into original

for(a=0;a<5;a++)

{

ar1[a]=ar2[a];

}

cout<<”\n”<<”Reversed array is: ”<<”\n”;

for(a=0;a<5;a++)

{

cout<<”\t”<<ar1[a];

}

getch();

}

Output:

The array elements are:

5 10    15   20 25

Reversed array is:

25 20    15 10 5

 

Array and swapping

Swapping

Swapping refers to mutually exchanging the values of two variables.

 

A program to reverse an array elements using swapping method.

#include<iostream.h>

#include<conio.h>//Function prototype

void main()

{

int ar[5]={5,10,15,20,25};

int a,b,n,end;

n=5;

end=n-1;

cout<<”\n”<<”The array elements are:”<<endl;

for(a=0;a<n;a++)

{

cout<<” ”<<ar[a];

}

for(a=0;a<n/2;a++) //swapping begins

{

b=ar[a];

ar[a]=ar[end];

ar[end]=b;

end--;

}

cout<<”\n”<<”Reversed array is: ”<<”\n”;

for(a=0;a<n;a++)

{

cout<<” ”<<ar[a];

}

getch();//freeze the screen until any key is pressed

}

Output:

The array elements are:

5 10     15 20  25

Reversed array is:

25 20      15 10 5

 

Array of objects

Objects

·   An object is an instance of a class.

·   To use the data and access functions defined in the class, objects are created.

Syntax to define objects

Class-name object-name;

 

A program to demonstrate the declaration of an array member as an object.

#include<iostream.h>

#include<conio.h>//Function prototype

class number             //class declaration statement

{

private:

int no;

public:

void getdata(int a)        //function declaration statement

{

no=a;

}

void display()                //function declaration statement

{

cout<<”\n”<<no;

}

};

void main()

{

int i, no,count;

char check;

number ob[10];  //array of 10 objects declared

clrscr();

count=0;

cout<<”\n”<<”Getting numbers:”<<endl;

for(i=0;i<10;i++)

{

cout<<”\n”<<”Do you want to continue(y/n)?”;

cin>>check;

if(check==’Y’||check==’y’)

{

cout<<”\n”<<”Enter an integer:”;

cin>>no;

ob[i].getdata(no);

count++;

}

else

break;

}

cout<<”\n”<<”Displaying numbers:”<<endl;

for(i=0;i<count;i++)

{

ob[i].display();   //calling object

}

getch();//freeze the screen until any key is pressed

}

Output:

Getting numbers:

Do you want to continue(y/n)? y

Enter an integer:2

Do you want to continue(y/n)? y

Enter an integer:4

Do you want to continue(y/n)? y

Enter an integer: 6

Do you want to continue(y/n)? n

Displaying numbers:

2

4

6

An array of pointers to objects

A program to demonstratethe declaration of an array of pointers to objects.

#include<iostream.h>

#include<conio.h>//Function prototype

class number          //class declaration statement

{

private:

int no;

public:

void getdata(int num)          //function declaration statement

{

no=num;

}

void display()             //function declaration statement

{

cout<<”\n”<<no;

}

};

void main()

{

int a,no,count;

char check;

number *ob[10];       //array of pointers to objects

clrscr();

count=0;

for(a=0;a<10;a++)

{

cout<<”\n”<<”Do you want to enter an integer(y/n)?”;

cin>>check;

if(check==’Y’||check==’y’)

{

cout<<”\n”<<”Assign an integer:”;

cin>>no;

ob[a]=new number;   // dynamically creating objects

ob[a]àgetdata(no);

count++;

}

else

break;

}

cout<<”\n”<<”Displaying numbers:”<<endl;

for(a=0;a<count;a++)

{

ob[a]àdisplay();

}

for(a=0;a<count;a++)

{

delete ob[a];

}

getch();

}

Output:

Do you want to enter an integer(y/n)? y

Assign an integer: 2

Do you want to enter an integer(y/n)? y

Assign an integer: 3

Do you want to enter an integer(y/n)? 4

Assign an integer: 4

Do you want to enter an integer(y/n)? n

Displaying numbers:

2

3

4

Array and pointers

A program to demonstrate an array name as a pointer.

#include<iostream.h>

#include<conio.h>

void main()

{

int num[5]={5,10,15,20,25};

cout<<”\n”<<”Third member :”<<*(num+2);//The pointer num points to getch();                                                         the beginning   location of array num[ ].

}

Output:

Third member : 15

A program to access array members using pointer variable.

#include<iostream.h>

#include<conio.h>//Function prototype

void main()

{

int *a;

int num[5]={5,10,15,20,25};

a=num;

cout<<”\n”<<” a points to first member:”<<*a;

cout<<”\n”<<” a points to second member:”<<*(a+1);

getch();

}

Output:

a points to first member:5

a points to second member: 10

 

A program to access array members using pointer and for loop.

#include<iostream.h>

#include<conio.h>

void main()

{

int a;

int num[5]={5,10,15,20,25};

cout<<”\n”<<”Accessing array members :”;

 for(a=0;a<5;a++)

{

cout<<”\n”<<*(num+a);

}

getch();                                                         

}

Output:

Accessing array members :

5

10

15

20

25

 

A program to access string members of an array using pointer and for loop.

#include<iostream.h>

#include<conio.h>//Function prototype

#include<string.h>

void main()

{

int a;

char *str[3]={“apple”,”orange”,”guava”};

cout<<”\n”<<”Accessing array members :”;

 for(a=0;a<3;a++)

{

cout<<”\n”<<*(str+a);

}

getch();                                                        

}

Output:

Accessing array members :

apple

orange

guava

 

Multi-dimensional array

Multi-dimensional array refers to a rectangular grid of elements having rows and columns. The idea of a two-dimensional array has been illustrated giving a suitable example.

 

A program to demonstrate the declaration of a 2-D array.

#include<iostream.h>

#include<conio.h>//Function prototype

void main()

{

int a,b;

int ar[4][2]={1,1,2,8,3,27,4,64};  //Declaring and assigning values to 2-D array

cout<<”\n”<<”Accessing array members :”<<endl;

for(a=0;a<4;a++)

{

for(b=0;b<2;b++)

{

cout<<ar[a][b]<<”\t”;

}

cout<<”\n”;

}

getch();

}

Output:

Accessing array members :

1                  1

2                   8

3                  27

4                  64 

OR

#include<iostream.h>

#include<conio.h>//Function prototype

void main()

{

int a,b;

int ar[2][2];

cout<<”\n”<<”Entering array members:”<<endl;

for(a=0;a<2;a++)

{

for(b=0;b<2;b++)

{

cin>>ar[a][b];

}

}

cout<<”\n”<<”Accessing array members :”<<endl;

for(a=0;a<2;a++)

{

for(b=0;b<2;b++)

{

cout<<ar[a][b]<<”\t”;

}

cout<<”\n”;

}

getch();

}

Output:

Entering array members:

1

2

2

4

Accessing array members :

1                    2

2                    4

 

A program to assign row elements in a 2-D array.

#include<iostream.h>

#include<conio.h>//Function prototype

void main()

{

int a,b;

int ar[4][4]={{0,1,2,3},{1,2,3,0},{2,3,0,1},{3,0,1,2}}; //Assigning rows elements

clrscr();

cout<<”\n”<<”Accessing array members :”<<endl;

for(a=0;a<4;a++)

{

for(b=0;b<4;b++)

{

cout<<ar[a][b]<<” ”;

}

cout<<”\n”;

}

getch();

}

Output:

Accessing array members :

0 1    2 3

1 2    3 0

2 3    01

3 0     1   2

OR

#include<iostream.h>

#include<conio.h>//Function prototype

void main()

{

int a,b;

int ar[4][2]={{1},{1,2},{3},{3,0}};          //Assigning array elements

clrscr();

cout<<”\n”<<”Accessing array members :”<<endl;

for(a=0;a<4;a++)           //for loop declaration

{

for(b=0;b<2;b++)

{

cout<<ar[a][b]<<”\t”;

}

cout<<”\n”;

}

getch();   //freeze the screen until any key is pressed

}

Output:

Accessing array members :

0

2

0

0

 

A program to declare and access string members of a 2-D array.

#include<iostream.h>

#include<conio.h>//Function prototype

void main()

{

int a,b;

char*str[7][1]={“Monday”,”Tuesday”,”Wednesday”,”Thursday”,”Friday”,”Saturday”,”Sunday”};

cout<<”\n”<<”Accessing array members :”<<endl;

for(a=0;a<7;a++)

{

for(b=0;b<1;b++)             //for loop declaration  

{

cout<<str[a][b];

}

cout<<”\t”;

}

getch();           //freeze the screen until any key is pressed

}   

Output:

Accessing array members :

Monday Tuesday     Wednesday Thursday Friday      Saturday Sunday

 


No comments:

Post a Comment