In this post, we will write a C program to find the largest value in an array using pointers.
C Program to Find the Largest Value in an Array Using Pointers
In this program, all the elements of the array will be scanned using pointers.
Let's create a file named largestinarray.c and add the following source code to it:
#include <stdio.h>
#define max 100
void main()
{
int p[max], i,n, *ptr, *mx;
printf("How many elements are there? ");
scanf("%d", &n);
printf("Enter %d elements \n", n);
for(i=0;i<n;i++)
scanf("%d",&p[i]);
mx=p;
ptr=p;
for(i=0;i<n;i++)
{
if (*mx < *ptr)
{
mx=ptr;
}
ptr++;
}
printf("Largest value is %d\n", *mx);
}
To compile and run the above C program, you can use C Programs Compiler Online tool.
Output:
How many elements are there? 5
Enter 5 elements
10
30
50
20
80
Largest value is 80
Comments
Post a Comment