Arrays and Pointers
Arrays
Arrays are used to store data into a Single variable.Arrays stores the data in contiguous memory location. In Array data is used to access by using the index value.
Array stores similar kinds of data. Array can store data of primitive data types like int, char, float,double as well as Non-primitive Data types like Structure, pointers , string etc.
Advantages
- We can Randomly access the data
- Retrival is easy here.
- Traversing is Easy
- By using array the Sorting becomes easy.
Disadvantages
- We can only store limited data in array.
- Insertion and deletion is difficult.
- Single Dimensional Array or 1-D Array
- Multi Dimensional Array or 2-D Array
Single Dimensional Array
Single Dimensional or 1-D Array is used to store data in a single variable.It stores data either in row or in column.
It stores same kinds of data using single variable.
Data can be accessed using index number or Array.
Indexing of Array starts from 0.
Syntax of declaring Array.
data type arrName[size];Initialization of Array
Initialization can be in two ways:1. int arr[]= {3,4,5,6,3,3,8};
2. int arr[5]; //declaration
arr[0] = 12;
arr[1] = 23;
arr[2] = 45;
arr[3] = 56;
arr[4] = 90;
Program
Write a program to store marks of 5 subjects and print the numbers without using loop.Multi Dimensional Array or 2-D
2-D or two Dimensional array is used to store data into Table form.In this Data is Stored in rows and columns.
Or we can say data is stored in matrix form.
Syntax of Declaring 2-D array:
data type arrName[row][column]
Here column: defines the size of column. Where indexing starts from 0.
Full length of matrix : (size of row) * (size of column).
Ex: int arr[3][3];
It will store 3 * 3 = 9 Elements into matrix.
Initialization of Matrix:
1. int mat[3][3]={1,2,3,4,5,6,7,8,9};
2. int mat[3][3]={ {1,2,3}, {4,5,6}, {7,8,9}};
Accessing elements of MAtrix:
First Row
mat[0][0] = 1
mat[0][1] = 2
mat[0][2] = 3
Second Row
mat[1][0] = 4
mat[1][1] = 5
mat[1][2] = 6
Third Row
mat[2][0] = 7
mat[2][1] = 8
mat[2][2] = 9
Program
Write a Program to display 3X3 Matrix.Write a Program to Add Two Matrix
#include <iostream>
using namespace std;
int main()
{
int mat1[3][3],mat2[3][3],sum[3][3]={0};
int r,c;
cout<<"Enter Elements of Matrix 1\n";
for(r=0; r<3; r++)
{
for(c=0; c<3; c++)
{
cout<<"\nmat["<<r<<"] ["<<c<<"] :";
cin>>mat1[r][c];
}
}
cout<<"\nEnter Elements of Matrix 2\n";
for(r=0; r<3; r++)
{
for(c=0; c<3; c++)
{
cout<<"\nmat2["<<r<<"] ["<<c<<"] :";
cin>>mat2[r][c];
}
}
cout<<"\nElemnents of Matrix1 are as Follows:\n";
for(r=0; r<3; r++)
{
for(c=0; c<3; c++)
{
cout<<mat1[r][c]<<"\t";
}
cout<<"\n";
}
cout<<"\nElemnents of Matrix2 are as Follows:\n";
for(r=0; r<3; r++)
{
for(c=0; c<3; c++)
{
cout<<mat2[r][c]<<"\t";
}
cout<<"\n";
}
for(r=0; r<3; r++)
{
for(c=0; c<3; c++)
{
sum[r][c]= mat1[r][c]+mat2[r][c];
}
}
cout<<"\n Sum of Matrix1 and Matric 2 :\n";
for(r=0; r<3; r++)
{
for(c=0; c<3; c++)
{
cout<<sum[r][c]<<"\t";
}
cout<<"\n";
}
return 0;
}
Pointer
Pointer is a variable which holds the address of another variable.Pointer mostly used for allocating memory and for accessing the address of the variables.
The pointer variable and normal variable whose address is stored by the pointer variable both have the same memory location.
Syntax:
data type varName,*ptrvariable;
Ex: int num, *ptr;
ptr = #
Here ptr will store the Address of num.
*ptr will hold the same value as num.
If we change the value of num it will automatically changes the value of *ptr
& if we change the value of *ptr it will automatically changes the value of num
Why?
because both are allocated to the same memory location.
Program:
Write a Program to show the use of the Pointer Variable.
Array of Pointers
Program
An array of pointer used with character array used to store a string into Array
Comments
Post a Comment
If you have any doubts, Please let me Know