07 June, 2013

C++ Program To Print An Array Using Pointer.

This Post Contains A C++ Program To Print An Array Using Pointer With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At Turbo C/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Arrays' & 'Pointers' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.
  


  • A linear array is a list of finite number 'n' of homogeneous data elements.
  • The elements of array are referenced by index set consisting of 'n' consecutive numbers.
  • The elements of array are stored respectively in successive memory locations.
  • The number 'n' of elements is called as 'length' or 'size' of the array.
  • Length Of Array = UB - LB + 1.
          Where UB = largest index i.e. upper bound.
                     LB = smallest index i.e. lower bound.
  • The elements of array A may be denoted by subscript notation.
          A1A2A3,  .......... An
  • But in languages generally bracket notation is used.
          A[0], A[1], A[2], .......... A[n]

   
C++ Program To Print An Array Using Pointer.

/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>

/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int i, a[10], *ptr;
clrscr();

/* Asking For The Input From User */
cout<<" \n Please Enter 10 Numbers For An Array 'A' : ";
for(i=0; i<10; i++)
{
   cin>>a[i];
}

/* Printing The Output Onto The Screen/Console */
ptr=&a;                                 // Or You Can Also Denote ptr=&a[0];
cout<<" \n The Entered Numbers Are : \n ";
for(i=0; i<10; i++)
{
   cout<<*ptr<<"\t";
   ptr++;
}
getch();
}
/* End Of Main Program */


Output :

Please Enter 10 Numbers For An Array 'A' :
1  2  3  4  5  6  7  8  9  10

The Entered Numbers Are :
1  2  3  4  5  6  7  8  9  10

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

C++ Program To Print An Array Onto Screen/Console.

/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>

/* Start Of Main Program */
void main()
{

/* Declaration Of Variables */
int i, a[10];
clrscr();

/* Asking For The Input From User */
cout<<" \n Please Enter 10 Numbers For An Array 'A' : ";
for(i=0; i<10; i++)
{
   cin>>a[i];
}

/* Printing The Output Onto The Screen/Console */

cout<<" \n The Entered Numbers Are : \n ";
for(i=0; i<10; i++)
{
   cout<<a[i]<<"\t";
}
getch();
}
/* End Of Main Program */


Output :

Please Enter 10 Numbers For An Array 'A' :
1  2  3  4  5  6  7  8  9  10

The Entered Numbers Are :
1  2  3  4  5  6  7  8  9  10




No comments:

Post a Comment

Subscribe To:

Most Commonly Asked Programs In 'C' & 'C++' Language.

Blog Archive