-->

Facebook

Jasper Roberts - Blog

Saturday, April 4, 2015

Two Dimensional Arrays

Two Dimensional Arrays

-         Sometimes we need to store the data where more than one dimensions are involved, like sales information of a company, or for mathematical calculations we need to use matrix.

-         To declare a two-dimensional integer array of size i,j,  you would write something as follows:

-         Syntax:
Data-Type Variable-Name[row-size][col-size];

Where , Data-Type may be int, char or float,
Variable-Name represents name of the array,
Row-size indicate the number of row in table,
Col-size indicate the number of columns in an array.
-         Example:
                     int matrix[3][3];

-         In above example, int is a datatype and matrix is an array name. Here, the size of row is 3 and size of column is 3.






In above figure, the cell a[i][j] refers to the cell in ith row jth column.
So,
matrix[0][0]=10,
matrix[1][0]=20,
matrix[2][0]=30,
matrix[0][1]=40,
matrix[1][1]=50,
matrix[2][1]=60,
matrix[0][2]=90,
matrix[1][2]=80,
matrix[2][2]=70,
and like that.
So , solving mathematical problems involving matrices, we can use two dimensional array.



Write a C++ Program to demostrate character data type.

#include<iostream.h>
//using namespace std;
class Person
{
char name[30];
int age;
public:
void get_data(void);
void display();
};
void Person::get_data(void)
{
cout<<"Enter the name : ";
cin>>name;
cout<<"\nEnter the age : ";
cin>>age;
};
void Person::display(void)
{
cout<<"\nName : "<<name;
cout<<"\nAge : "<<age;
};
void main()
{
Person p;
p.get_data();
p.display();
}

Write a C++ Program to calculate average and sum.

#include<iostream.h>
//using namespace std;
void main()
{
float x,y,s,a;
cout<<"Enter the number : "<<"\n";
cin>>x>>y;
s=x+y;
a=s/2;
cout<<"\nSum = "<<s;
cout<<"\nAvg = "<<a;
}

OUTPUT:
Enter the number: 10
10
Sum = 20
Avg = 10

Basic Concepts of Array

Array:
One Dimensional Array - CLICK HERE