Arrays

Arrays:

the array, which stores a fixed-size sequential group of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Declaring Arrays:

For declaring array in c++,the programmer specifies the type of the elements and the number of elements required by an array as follows:
type array[arraySize];
Example:-
double balance[10];

Initializing Arrays:

Arrrays can be initialied by following ways:-
double balance[5]={1000.5,3.4,50.0,67.8,45.9};
or

double balance[]={1000.5,3.4,50.0,67.8,45.9};

representaion of the same array:

1array

Example

#include<iostream.h>

int foo [] = {16, 2, 77, 40, 12071};

int n, result=0;

void main ()

{

for ( n=0 ; n<5 ; ++n )
{
result += foo[n];
}
cout << result;

}

Output
12206

Two-Dimensional Arrays:

Two –dimensional arrays can declared as follows:-
Type arrayname[x][y];

Initializing Two-Dimensional Arrays:

int a[3][4]={
{0,1,2,3}, /*initialers for row indexed by 0*/

{4,5,6,7}, /*initialers for row indexed by 1*/

{8,9,10,11} /*initialers for row indexed by 2*/

};

Representaion of the array:

2array

 

Example

#include<iostream.h>

const int TOWN = 2;

const int WEEK = 7;

void main()

{

int temperature[TOWN][WEEK];

cout<<“Enter all temperature for a week of first city & then second city. \n”;

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

{

for(int j = 0; j < WEEK; ++j)

{

cout<<“Town “<<i+1<<“, Day “<<j+1<<” : “;

cin>>temperature[i][j];

}

}

cout<<“\n\nDisplaying Values:\n”;

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

{

for(int j = 0; j < WEEK; ++j)

{

cout<<“Town “<<i+1<<“, Day “<<j+1<<” = “<< temperature[i][j]<<endl;

}

}

}

Output

Enter all temperature for a week of first city and then second city Town 1 ,Day1 :27

Town 1 ,Day2 :29

Town 1 ,Day4 :28

Town 1 ,Day5 :30

Town 1 ,Day6 :32

Town 1 ,Day7:34

Town 2 ,Day1 :35

Town 2 ,Day2 :37

Town 2 ,Day4 :38

Town 2 ,Day5 :39

Town 2 ,Day6 :40

Town 2 ,Day7 :41

Displaying Values:

Town 1 ,Day1 :27

Town 1 ,Day2 :29

Town 1 ,Day4 :28

Town 1 ,Day5 :30

Town 1 ,Day6 :32

Town 1 ,Day7 :34

Town 2 ,Day1 :35

Town 2 ,Day2 :37

Town 2 ,Day4 :38

Town 2 ,Day5 :39

Town 2 ,Day6 :40

Town 2 ,Day7 :41

Write a C++ Programming Code to Multiply Two Matrices
#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int mat1_[3][3], mat_2[3][3], mat_3[3][3], sum=0, i, j, k;

cout<<“Enter first matrix element (3*3) : “;

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

{

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

{

cin>>mat_1[i][j];

}

}

cout<<“Enter second matrix element (3*3) : “;

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

{

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

{

cin>>mat_2[i][j];

}

}

cout<<“Multiplying two matrices…\n”;

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

{

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

{

sum=0;

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

{

sum = sum + mat_1[i][k] * mat_2[k][j];

}

Mat_3[i][j] = sum;

}

} cout<<“\nMultiplication of two Matrices are displayed as follows : \n”;

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

{

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

{

cout<<mat_3[i][j]<<” “;

}

cout<<“\n”;

}

getch();

}

Output

Enter first matrix element (3*3) :

8

6

6

6

8

3

5

2

4

Enter second matrix element (3*3) :

9

5

3

2

3

7

3

8

7

Multiplication of two Matrices :

102     106     108

79       78       95

61       63       57

Leave a Reply

Your email address will not be published. Required fields are marked *