Maximum element in an array in C
Q. Write a C program to find maximum element in an array.
#include <stdio.h>
#include <conio.h>
void main()
{
int a[100], size, i, max;
printf("Enter the number of elements in array\n");
scanf("%d", &size);
printf("Enter the integers: ");
for (i = 0; i < size; i++)
{
scanf("%d", &a[i]);
}
max = a[0];
for (i = 1; i < size; i++)
{
if (a[i] > max)
{
max = a[i];
}
}
printf("Maximum element in an array is %d", max);
getch();
}
Q. Write an algorithm to find maximum element in an array.
1. Start 2. Declare an array `a` to hold integers and variables `size`, `i`, and `max`. 3. Prompt the user to enter the number of elements `size`. 4. Read the value of `size`. 5. Prompt the user to enter `size` integers. 6. Use a loop to read and store each integer into the array `a`. - For `i` from 0 to `size - 1` - Read the `i`th element into `a[i]` 7. Initialize `max` with the first element of the array `a[0]`. 8. Use a loop to traverse the array from the second element to the last. - For `i` from 1 to `size - 1` - If `a[i]` is greater than `max` - Update `max` with `a[i]` 9. After the loop ends, `max` holds the maximum element of the array. 10. Print the maximum element. 11. End
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com