Q:

C program to find the largest number in an array

0

C program to find the largest number in an array

C program to find the largest number in an array , We also print the index at which it's present.

How to find max value in an array?

Algorithm to get max value: we assume that it's present at the beginning of the array. Then compare it with the second element. If the second element is greater than the first, the index is updated. Repeat it till the last index of the array. Similarly,

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>
int main()
{
  int array[100], size, c, location = 0;

  printf("Enter the number of elements in array\n");
  scanf("%d", &size);

  printf("Enter %d integers\n", size);

  for (c = 0; c < size; c++)
    scanf("%d", &array[c]);

  for (c = 1; c < size; c++)
    if (array[c] > array[location])
      location = c;

  printf("Maximum element is present at location %d and its value is %d.\n", location+1, array[location]);
  return 0;
}

output:

Enter the number of elements in array

3

Enter 3 integers

3

4

5

Maximum element is present at location 3 and its value is 5

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now