Object

Write a program that displays the contents of an array using pointers.

Algorithm

  1. Declare integer array that contains following 10 elements:
    {12, 14, 16, 18, 20, 22, 24, 26, 28, 30}
  2. Declare an integer variable i and an integer pointer j.
  3. Set j = &array[0].
  4. Repeat FOR i = 0 to 9 by 1
    • Write the value at address j (i.e., *j).
      • j++.
    • i++.
    • [End of FOR loop.]
  5. Exit.

Flowchart

C++ Source Code

// program 106
#include<iostream>
using namespace std;

int main()
{
  int array[] = {12, 14, 16, 18, 20, 22, 24, 26, 28, 30};
  int i, *j;

  j = &array[0];
  for (i = 0; i < 10; i++)
  {
    cout << "Element at position " << i << " is: " << *j << endl;
    j++;
  }
  return 0;
}

C Source Code

/*program 106*/
#include<stdio.h>

int main()
{
  int array[]={12, 14, 16, 18, 20, 22, 24, 26, 28, 30};
  int i, *j;

  j = &array[0];
  for(i=0; i<10; i++)
     {
     printf("\nElement at position %d is: %d", i, *j);
     j++;
     }
  return 0;
}/*program 106*/
#include<stdio.h>

int main()
{
  int array[]={12, 14, 16, 18, 20, 22, 24, 26, 28, 30};
  int i, *j;

  j = &array[0];
  for(i=0; i<10; i++)
     {
     printf("\nElement at position %d is: %d", i, *j);
     j++;
     }
  return 0;
}

Output

Element at position 0 is: 12
Element at position 1 is: 14
Element at position 2 is: 16
Element at position 3 is: 18
Element at position 4 is: 20
Element at position 5 is: 22
Element at position 6 is: 24
Element at position 7 is: 26
Element at position 8 is: 28
Element at position 9 is: 30

Design a site like this with WordPress.com
Get started