I'm trying to find the 2nd max number from an array. For some reason, I keep getting output zero.
int main()
{
int i,j;
int arr[5],max=0,max2=0;
int *(ptr)=arr;
printf("Enter Array Elements: ");
for(i=0;i<5;i )
{
scanf("%d",ptr i);
}
for(j=0;j<5;j ){
if(max<*(ptr j)&&max>max2)
{
max=*(ptr) j;
}
else if(max2<*(ptr j)&&max2<max)
{
max2=*(ptr) j;
}
}
printf("\n2nd Maximum: %d",max2);
}
CodePudding user response:
both your conditions compare max
to max2
. Since they are both initialized to 0
, neither of these conditions is ever met.
Instead you need to compare the current value (*(ptr j)
) to the maximum number first, and if that condition isn't met, to the second maximum:
for (j = 0; j < 5; j ) {
int curr = *(ptr j);
if (curr >= max)
{
max2 = max;
max = curr;
}
else if(curr > max2)
{
max2 = curr;
}
}
CodePudding user response:
You are over complicating it. It probably would help to write it first without using pointers that way you can visualize what is going on then rewrite it.
If you don't want values that can be equal remove the equal sign in the if statement.
Secondly, this still has potential issues. If you have all negative numbers and the first one is the highest. The max2 will never get set. To avoid that issue you would need to either sort all the value low to high or run two loops one for max and one for max2. It gets a bit more complex than that even. The best option would be sort then run the loops to avoid the issue.
If you do the sort well your work is done for the most part. Second value from the top would be max 2 if you allow equal values. If not you just have to go down until you find the next value that is different.
int main()
{
int arr[5],max=0,max2=0;
int *(ptr) = arr;
printf("Enter Array Elements: ");
for (int i=0;i<5;i ){
scanf("%d",ptr i);
}
/*
max = arr[0];
for (int j=1;j<5;j ){
if(arr[j]>=max){
max2 = max;
max = arr[j];
}
}*/
max = *(ptr);
for(int j = 1;j<5;j ){
if(*(ptr j)>=max){
max2 = max;
max = *(ptr j);
}
}
printf("\n2nd Maximum: %d",max2);
return 0;
}
CodePudding user response:
Initially max
and max2
are equal each other due to setting them to 0
.
int arr[5],max=0,max2=0;
So neither of these two if statements evaluate their conditions to true
if(max<*(ptr j)&&max>max2)
^^^^^^^^
//...
else if(max2<*(ptr j)&&max2<max)
^^^^^^^^
because max
is not greater than max2
.
Moreover setting initially max
and max2
to zero is a wrong approach because the user can enter only negative values to elements of the array. In this case you will get wrong maximums.
If you want to find two maximum values that are not equal to each other except the case when the array contains all elements equal each other then the approach is not trivial as you think.
At first you need to find two unequal elements in the array. For example
int max = arr[0];
int max2 = arr[0];
i = 1;
while ( i < 5 && arr[i-1] == arr[i] ) i;
if ( i != 5 )
{
if ( max < arr[i] )
{
max = arr[i];
}
else
{
max2 = arr[i];
}
for ( ; i < 5; i )
{
if ( max < arr[i] )
{
max2 = max;
max = arr[i];
}
else if ( max2 < arr[i] )
{
max2 = arr[i];
}
}
}
if ( max != max2 )
{
printf("\n2nd Maximum: %d\n", max2 );
}
else
{
printf("\nAll elements are equal to %d\n", max2 );
}