This document discusses declaring and using multidimensional arrays, 2D dynamic arrays, and structures in C++. It explains that multidimensional arrays can be declared to have multiple indices, like int arr[rows][columns]. 2D dynamic arrays can be declared using pointers, by allocating memory for an array of pointers and each pointer. Structures allow grouping of different data types and functions together under one name. Members are accessed using the dot operator.
2. ◦ To learn how to declare multidimensional array
◦ To learn how to declare 2D dynamic array using pointers
◦ To learn the use and syntax of the structures
3. Arrays can have two or more dimensions. The two
dimensional array is called a matrix. It can be declared
as
int std[no. of rows][no. of columns];
Two loops are used to input values to the array
elements. E.g.
for(i=0; i<no. of rows; i++)
for(j=0; j<no. of columns; j++)
{
cin>>std[i][j];
}
4. Like input, two loops are used to display values of the
array elements. E.g.
for(i=0; i<no. of rows; i++)
for(j=0; j<no. of columns; j++)
{
cout<<std[i][j];
}
Two dimensional arrays can be initialized as;
int std[3][2]={{1234,56}, {2356, 99}, {1586, 90}};
int std[][2]={{1234,56}, {2356, 99}, {1586, 90}};
5. Pointers in C++ can also be used to declare a 2D dynamic
array in which you can specify the number of rows and
columns at the runtime.
First you have to declare a double pointer mean a pointer
that point to a variable pointer then we allocate memory for
the variable pointers of any size we want and at last we will
go to each location of the pointers and allocate memory for
variables of any size. This can be done as
int **m;
m = new int* [size]; //array of pointers
for ( int i=0; i<size; i++)
m[i] = new int [size];
6.
7. #include<iostream>
using namespace std;
void main()
{
int size;
cin>>size;
int **num;
num= new int*[size];
for(int i=0;i<size;i++)
num[i]=new int[size];
for(int i=0;i<size;i++)
for(int j=0;j<size;j++)
cin>>num[i][j];
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
cout<<num[i][j]<<'t';
}
cout<<endl;
}
}
8. A structure is a collection of simple variables that can be of different types and functions.
The data items in a structure are called members of a structure. Structures are defined
using ‘struct’ keyword followed by the user defined name for the structure e.g.
struct name
{
int a; //data member
float b; //data member
string c; //data member
void function(); //member function
}; //definition of structure ends with a semi-colon
After the definition of the structure you can declare objects of the structure just like you
declare a variable. To access the data and function member of a structure dot operator is
used e.g.
void main()
{
name n1;
n1.a = 10;
n1.b = 2.3;
n1.c = “Hello “; //storing value in a data member
n1.function(); //calling member function
}
9. struct name
{
int a; //data member
float b; //data member
string c; //data member
void function()
{
cout<<"HELLO WORLD";
}//member function
}n;
struct name
{
int a; //data member
float b; //data member
string c; //data member
void function()
{
cout<<"HELLO WORLD";
}//member function
};
struct name n;
10. struct name
{
int a; //data member
float b; //data member
string c; //data member
void function()
{
cout<<"HELLO WORLD";
}//member function
};
struct name n={10,20.0,"Hello"};
11. struct book
{
string name;
float price;
int pages;
};
struct book b[100];
void main()
{
int i;
for(i=0;i<100;i++)
{
cin>>b[i].name>>b[i].price>>b[i].pages;
}
for(i=0;i<100;i++)
{
cout<<b[i].name<<b[i].price<<b[i].pages<<endl;
}
}