Write a program to find the largest number among 20 integers array using dynamic memory allocation.
C Program
#include <stdio.h>#include <stdlib.h>int main(){int num = 20;int *dataMemory;// memory allocationdataMemory = (int *)calloc(num, sizeof(int));if (dataMemory == NULL){printf("Error!!! memory not allocated.");exit(0);}printf("Enter 20 numbers:\n");for (int i = 0; i < num; ++i){scanf("%d", dataMemory + i);}// Finding the largest numberfor (int i = 1; i < num; ++i){if (*dataMemory < *(dataMemory + i)){*dataMemory = *(dataMemory + i);}}printf("Largest number is %d", *dataMemory);return 0;}
Input:
Enter 20 numbers:
2 3 4 5 1 3 8 9 1 3 4 10 11 23 24 12 34 5 6 7
Output:
Largest number is 34
No comments:
Post a Comment