Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Wednesday, 27 March 2019

MaxElement Algorithm

ALGORITHM MaxElement(A[0..n 1])

//Determines the value of the largest element in a given array

//Input: An array A[0..n 1] of real numbers

//Output: The value of the largest element in A

maxval ← A[0]

for ← to do

if A[i> maxval

maxval ← A[i]

return maxval



C  program:


#include <stdio.h>

 int main(){ 

 int array[100],maximum=0, c, n; 

  printf("Enter number of elements in array\n");

  scanf("%d", &n);  

 printf("Enter %d integer(s)\n", n); 

  for (c = 0; c < n; c++)

    scanf("%d", &array[c]); 

  for (c = 0; c < n; c++)  {

    if (array[c]>=maximum)    /* If required element is found */  

  {

        maximum=array[c];    

 } 

 }

    printf("\n %d Maximum element present in the 

array.\n", maximum);   

return 0;

}


Output:

Enter number of elements in array

2

Enter 2 integer(s)

8

9 

9 Maximum element present in the array.

0 comments:

Post a Comment