pc-ip-18.c (1627B)
1 /* 2 * Author : Amit Dutta <amitdutta4255@gmail.com> 3 * Date : 05 Jan 2026 4 * Repo : https://github.com/notamitgamer/bsc 5 * License : MIT License (See the LICENSE file for details) 6 */ 7 8 /* 9 * Question 18: 10 * Write a C program that includes a user-defined function named findLargest with the signature int findLargest(int arr[], int size);. 11 */ 12 13 #include <stdio.h> 14 #include <stdlib.h> 15 16 int findLargest(int[], int); 17 void inputArray(int[], int); 18 void printArray(int[], int); 19 20 int main() 21 { 22 int n, *arr = NULL; 23 printf("Enter the number of element: "); 24 scanf("%d", &n); 25 if (n < 2) 26 { 27 printf("\nTo get highest element, there must be atleast 2 element."); 28 return 1; 29 } 30 arr = (int *)malloc(n * sizeof(int)); 31 if (arr == NULL) 32 { 33 printf("\nMemory allocation failed."); 34 return 1; 35 } 36 inputArray(arr, n); 37 printf("\nGiven Array: "); 38 printArray(arr, n); 39 printf("\nLargest element of the array: %d", findLargest(arr, n)); 40 free(arr); 41 return 0; 42 } 43 44 void inputArray(int arr[], int n) 45 { 46 int i; 47 for (i = 0; i < n; i++) 48 { 49 printf("Enter element %d: ", i + 1); 50 scanf("%d", &arr[i]); 51 } 52 } 53 54 void printArray(int arr[], int n) 55 { 56 int i; 57 printf("["); 58 for (i = 0; i < n; i++) 59 { 60 printf("%d", arr[i]); 61 if (i < n - 1) 62 { 63 printf(", "); 64 } 65 } 66 printf("]"); 67 } 68 69 int findLargest(int arr[], int size) 70 { 71 int i, largest = arr[0]; 72 for (i = 1; i < size; i++) 73 { 74 if (arr[i] > largest) 75 { 76 largest = arr[i]; 77 } 78 } 79 return largest; 80 }