Skip to main content

Arrays and Pointers in C++


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.
Types of Array:
  • 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.



























Write a program to accept marks of 5 subjects and calculate the total marks of student and average using loop























Write a Program to reverse the Array.

























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 row: defines the size of row. Where indexing starts from 0.
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 = &num;
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

Program

Comments

Popular posts from this blog

Programming Basics

Logic Building Data_Types Operators Algorithms Pseudo_Code Hello Guys, Let's Play with Logic.........but wait, before that you must know that how to build Logic in Programming as you all know that programming is all about Logic..... So Let's start the game... If you are new Here that means you are absolutely right Place, Or If you already know, than you will learn new things here! If we Talk about the Programming we should be familiar with  these terms..... 1. Variable 2. Data Types 3.Keywords Variable: Variable is something which holds the value.                  Like:      a=10;                 Here,                 a---------------------->  Variable                  10 ---------------------> Value * The Value of Variable can be changed , *  " Variable is a named memory Location which holds a value." Data Types:   Data Type is what which tells that which kind of data we are using. In programming we have different Dat

FAQs

Basic Questions on Programming Languages 1. What is the meaning of Programming? Ans: Programming is the process of Scheduling the task to perform step by step to Execute a specific Task. 2. Types of Programming Languages? Ans: Types:             Procedural Programming Language            Object Oriented Programming Language            Object Based Language            Scripting Language Procedural Programming Language   C Programming  Object Oriented Programming Language     C++     Java     C#     VB.Net Object Based Language     JavaScript Scripting Language     Python     VBScript     PHP     Perl     Ruby     R 3. Types Of Operators in Programming Languages. Ans:      Operators Symbols Type Example Assignment = Unary a=10 Arithmetic +,-,/,*,% Binary a+b  a-b  a*b  a/b  a%b Relational >, <, >=, <=, ==, !=(Not Equal) Binary It gives Result in Boolen value True/False a>b  a<b  a>=b  a<=b  a==b  a!=b Logical && (AND), ||(OR), !(Not) Binary It gives Resul