C program to find maximum element in array

Q. C program to find maximum element in array.

Q. Write a C program using array to find largest and smallest number from a list of 100 given numbers.

SOLUTION :

#include <stdio.h>
int main()
{
  int array[100], maximum, list, c, position = 1;

  printf("\n Enter the number of elements in array : ");
  scanf("%d", &list);
  printf(" Enter value for %d integers. \n\n", list);

  for (c = 0; c < list; c++){
    printf(" Enter value for %d place : ", c+1);
    scanf("%d", &array[c]);
    }

  maximum = array[0];

  for (c = 1; c < list; c++){
    if (array[c] > maximum){
       maximum  = array[c];
       position = c+1;
    }
  }

  printf("\n RESULT : \n Maximum element is present at,\n Position number = %d and it's Value is = %d.\n", position, maximum);
  return 0;
}

By using Pointers :

#include <stdio.h>

int main()
{
  long array[100], *maximum, list, c, position = 1;

  printf("\n Enter the number of elements in array : ");
  scanf("%ld", &list);

  printf(" Enter %ld integers\n\n", list);

  for ( c = 0 ; c < list ; c++ ){
    printf(" Enter value for %d place : ", c+1);
    scanf("%ld", &array[c]);
  }

  maximum  = array;
  *maximum = *array;

  for (c = 1; c < list; c++)
  {
    if (*(array+c) > *maximum)
    {
       *maximum = *(array+c);
       position = c+1;
    }
  }

  printf("\n RESULT : \n Maximum element is present at,\n Position number = %ld  and it's value = %ld.\n", position, *maximum);
  return 0;
}

OUTPUT OF SOLUTION :

find maximum element in array

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s