Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

***IN C LANGAUGE Write a documented function called Largest that finds and retur

ID: 3855483 • Letter: #

Question

***IN C LANGAUGE

Write a documented function called Largest that finds and returns the address of the largest element in the array passed to it. (Assume an integer array of size 10) You must use pointer arithmetic on the array name instead of the array subscript notation to implement the function. int *Largest(int *array, int size): Write a documented function called Swap that takes two integer pointers and exchanges the values of each. It returns void. Example: given two integers 'a = 2' and 'b = 4', after swap (&a;, &b;) is called, 'a' will be 4 and 'b' will be 2. void Swap(int *x, int *y): Write a complete, well documented C language program (called Lab4.c) to test both the functions Largest and Swap. In order to test your functions you will need to define and populate an array, within main, that you can pass to Largest, and also two initialized int variables to pass into Swap: do not forget to write printf() statements to show Before/After views of any data that will undergo changes. Ensure that your testing clearly demonstrate each function and its required result. There is no need for a menu, nor for the program logic to repeat itself (unless you choose to implement such logic).

Explanation / Answer

#include<stdio.h>
#include<stdlib.h>

/**
*   Largest function takes an array of integers and size of the array as parameters
*   and returns the address of the largest element in the array
*/
int *Largest(int *array, int size)
{
   /**
   *   initially i is declared as an integer and ptr is pointer to an int holds the base address of the array
   *   and initially, it is assumed that first element of the array is the largest element and then in the
   *   for loop all the array from second to last is compared with the integer value pointed by ptr and if
   *   value is larger than the value pointed by the ptr pointer then it is updated such that it points to
   *   the new larger element than the previous one and at the end of the for loop address of the largest element
   *   in the array is returned
   */
   int i, *ptr = array;
   for(i = 1; i < size; i++)
   {
       if(*ptr < *(++array))
       {
           ptr = array;
       }
   }
   return ptr;
}

/**
*   Swap function takes two pointer to int as parameters
*/
void swap(int *x, int *y)
{
   /**
   *   initially temp initialized with the value pointed by the pointer x,
   *   then the value pointed by the pointer x is changed with the value pointed by the pointer y,
   *   and at the end the value pointed by the pointer y is changed with the value stored at temp variable
   */
   int temp = *x;
   *x = *y;
   *y = temp;
}

int main()
{
   int arr[] = {91, 2, 1, 5, 2, 6, 2, 0, 9};
   printf(" Largest : %d ", *Largest(arr, 9));
   int a = 4, b = 10;
   printf(" Before a : %d, b : %d ", a, b);
   swap(&a, &b);
   printf(" After a : %d, b : %d ", a, b);
   return 0;
}