'Calculating minimum finds second lowest instead?
Why is the output of this C++ code to find the min and max between inputted elements not showing the lowest number but rather the 2nd to the lowest? Idk why the min is not working when it is the exact opposite code of the max which is perfectly showing the highest number.
#include <iostream>
using namespace std;
int main()
{
int array[50], *max, *min, size, i;
cout<<"Enter the number of elements in array\n";
cin>>size;
cout<<"Enter array elements\n";
for ( i = 0 ; i < size ; i++ )
cin>>array[i];
max = array;
min = array;
for (i = 0; i < size; i++)
{
if (*(array+i) > *max)
*max = *(array+i);
}
cout<<"Maximum element in the array is "<< *max << "\n" ;
for (i = 0; i < size; i++)
{
if (*(array+i) < *min)
*min = *(array+i);
}
cout<<"Minimum element in the array is "<< *min <<"\n";
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
