Home / Programs / Write a program where getdata() and show() can be used to read objects into any integer array and to print element values of any integer array, respectively.
Programming Example

Write a program where getdata() and show() can be used to read objects into any integer array and to print element values of any integer array, respectively.

👁 1,657 Views
💻 Practical Program
📘 Step by Step Learning
Write a program where getdata() and show() can be used to read objects into any integer array and to print element values of any integer array, respectively.

Program Code

#include <stdio.h>
#define MAX 50
int main()
{
	int arr[MAX],n;
	int getdata(int *, int);
	void show(int *, int);
	n = getdata(arr, MAX);
	show(arr, n);
	return 0;
}
/* Function reads scores in an array. */
int getdata(int *a, int n)
{
	int x, i = 0;
	printf("\n Enter the array size\n");
	scanf("%d",&n);
	printf("\n Enter the array elements one by one\n");
	while(i < n)
	{
		scanf("%d",&x);
		*(a + i)=x;
		i++;
	}
	return i;
}
void show(int *a, int n)
{
	int i;
	for(i=0;i<n;++i)
	printf("\n %d", *(a+i));
}

Output

 Enter the array size
5

 Enter the array elements one by one
10
11
12
13
14

 10
 11
 12
 13
 14

Explanation

None

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.