bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_1 / practice-c / pc-ip-18.c

pc-ip-18.c (1436B)


      1 /*
      2  * Question 18:
      3  * Write a C program that includes a user-defined function named findLargest with the signature int findLargest(int arr[], int size);.
      4  */
      5 
      6 #include <stdio.h>
      7 #include <stdlib.h>
      8 
      9 int findLargest(int[], int);
     10 void inputArray(int[], int);
     11 void printArray(int[], int);
     12 
     13 int main()
     14 {
     15     int n, *arr = NULL;
     16     printf("Enter the number of element: ");
     17     scanf("%d", &n);
     18     if (n < 2)
     19     {
     20         printf("\nTo get highest element, there must be atleast 2 element.");
     21         return 1;
     22     }
     23     arr = (int *)malloc(n * sizeof(int));
     24     if (arr == NULL)
     25     {
     26         printf("\nMemory allocation failed.");
     27         return 1;
     28     }
     29     inputArray(arr, n);
     30     printf("\nGiven Array: ");
     31     printArray(arr, n);
     32     printf("\nLargest element of the array: %d", findLargest(arr, n));
     33     free(arr);
     34     return 0;
     35 }
     36 
     37 void inputArray(int arr[], int n)
     38 {
     39     int i;
     40     for (i = 0; i < n; i++)
     41     {
     42         printf("Enter element %d: ", i + 1);
     43         scanf("%d", &arr[i]);
     44     }
     45 }
     46 
     47 void printArray(int arr[], int n)
     48 {
     49     int i;
     50     printf("[");
     51     for (i = 0; i < n; i++)
     52     {
     53         printf("%d", arr[i]);
     54         if (i < n - 1)
     55         {
     56             printf(", ");
     57         }
     58     }
     59     printf("]");
     60 }
     61 
     62 int findLargest(int arr[], int size)
     63 {
     64     int i, largest = arr[0];
     65     for (i = 1; i < size; i++)
     66     {
     67         if (arr[i] > largest)
     68         {
     69             largest = arr[i];
     70         }
     71     }
     72     return largest;
     73 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror