#include <stdio.h>
int main()
{
int ele;
printf("Enter the required number of elements:");
scanf("%d", &ele);
int arr[ele];
for (int i = 0; i < ele; i++)
{
printf("Enter value for the array:n");
scanf("%d", &arr[i]);
}
// printing the array
int len;
int larger;
len = sizeof(arr) / sizeof(arr[0]);
for (int z = 0; z < len; z++)
{
printf("Array elements is %dt", arr[z]);
}
// finding the largest element in the array
for (int y = 0; y < len; y++)
{
larger = arr[y];
if (larger > 0)
{
if (arr[y] > larger)
{
larger = y;
}
}
else
{
if (arr[y] < larger)
{
larger = y;
}
}
}
printf("The largerst element in the array is:%d", arr[larger]);
}
I’m creating this C programme to find the highest element in the array , whether it be -ve or +ve.
I’m getting correct answer for positive elements but not for negative ones.
For e.g we know that -10 is greater than -40 , but my program shows -40 as the larger element.
New contributor
Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.