Object
Write a program that passes an array to a function using pointers.
Algorithm
- Declare a function void display(int *, int) that accepts an integer pointer and an integer as arguments and returns no value.
The Function main - Declare integer array array that contains 5 elements.
- Declare integer variable i and integer pointer j.
- Set j = &array[0].
- Repeat FOR i = 0 to 4 by 1
- Read array[i]. (i.e., store input value in array at location i)
- [End of FOR loop.]
- Call the function display(j, i) considering j and i as actual arguments.
- Exit.
Defining the Function void display(int *j, int len)
where *j and len are the formal arguments - Declare integer variable i.
- Repeat FOR i = 0 to (len-1) by 1
- Write the value at address j (i.e., *j).
- j++.
- i++.
- [End of FOR loop.]
- Write the value at address j (i.e., *j).
- Exit.
Flowchart

C++ Source Code
// program 107
#include<iostream>
using namespace std;
void display(int *, int);
int main()
{
int array[5];
int i, *j;
j = &array[0];
for (i = 0; i < 5; i++)
{
cout << "Enter any number for position " << i << ": ";
cin >> array[i];
}
display(j, i);
return 0;
}
void display(int *j, int len)
{
system("cls");
int i;
for (i = 0; i < len; i++)
{
cout << "\nElement at position " << i << " is: " << *j;
j++;
}
}
C Source Code
/*program 107*/
#include<stdio.h>
#include<stdlib.h>
void display(int *, int);
int main()
{
int array[5];
int i, *j;
j = &array[0];
for(i=0; i<5; i++)
{
printf("Enter any number for position %d: ", i);
scanf("%d", &array[i]);
}
display(j, i);
return 0;
}
void display(int *j, int len)
{
system("cls");
int i;
for(i=0; i<len; i++)
{
printf("\nElement at position %d is: %d", i, *j);
j++;
}
}
Output
Element at position 0 is: 24
Element at position 1 is: 67
Element at position 2 is: 93
Element at position 3 is: 23
Element at position 4 is: 76
