Dr. A.P.J. Abdul Kalam Technical University, Lucknow
KCS151 / KCS251 Programming for Problem Solving - Using C Language
Lab Exercises
42. Write a program to find the largest no among 20 integers array using dynamic memory allocation.
/*
File: Prgrm42.c
Author: Aditya Saini
Date: Jan 20, 2021
Description: Program to find the largest no among 20 integers array using dynamic memory allocation.
*/
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int *ar, *max;
int i;
//Create array
ar = (int *) calloc (20, sizeof (int));
if (ar == NULL)
{
printf ("Error! Memory not allocated.");
return 0;
}
//Input elements
printf ("Input elements: ");
for (i = 0; i <= 19; i++)
scanf ("%d", ar + i);
//Find largest
printf ("Largest: ");
max = ar;
for (i = 1; i <= 19; i++)
if (*(ar + i) > *max)
max = ar + i;
printf ("%d", *max);
};
Output
Input elements: 5 20 2 17 4 10 11 3 8 13 9 14 6 1 16 7 19 15 12 18
Largest: 20
No comments:
Post a Comment
Please do not post spam links.