Jump to content

Problem in program to find 3rd largest element of an array!(C programming)

Go to solution Solved by Alvin853,

Figure out what max, smax and tmax are after each iteration of the loop, you'll see your problem right away.

This is the logic that I'm using 

2GYnvmkXrFKGA8TPn51NiRdknqGoUQrcJKgRAOqgAkN2xnLcir0Q-QOTTiE3wwmpNs1FNblwJ59FSLN9fTaIugHcIWbFosLAZ7ZLvBiEvy5VwmHrgoAh272LVy2pzFjUD70ErKirnax_ze2I5wYdSdetCjWrzjXi1x2TEHG2D_5aksVfd7RVv6nUNZRQdw


 

#include <stdio.h>
#include <stdlib.h>
int main()
{
  system("cls");
  int arr[8] = {8, 1, 7, 5, 4};
  int max = INT_MIN;
  int smax = INT_MIN;
  int tmax = INT_MIN;
  for (int i = 0; i < 5; i++)
  {
    if (max < arr[i])
    {
      smax = max;
      max = arr[i];
      tmax = smax;
    }
    else if (arr[i] < max && arr[i] > smax)
    {
      smax = arr[i];
      tmax = smax;
    }
    else if (arr[i] < smax && arr[i] > tmax)
    {
      tmax = arr[i];
    }
  }
  printf("%d", tmax);
  return 0;
}

I'm currently getting output=7, instead I want output=5



I'm currently getting output=7, instead I want output=5
 

Link to post
Share on other sites

If you're allowed to modify the input array, you could just sort the array.

 

but basically  have a 3 element array which holds the 3 maximums , initialize the 3 elements with lowest possible value (int_max+1 or int_min, whatever constant)

for counter  = 1st element in array to the last   

  if value >  element 0 of max array , replace the element 0 with this value, sort the 3 element array (so that the smallest value will be element 0)

 

example in php

 

<?php

$max = [0,0,0];
$input = [8, 1, 7, 5, 4];

for ($i=0;$i<3;$i++) $max[$i]=PHP_INT_MIN;

for ($i=0;$i<count($input);$i++) {
	$value = $input[$i];
	if ($value>$max[0]) {
		$max[0]=$value;
		// sort the 3 element array
		/*
			if (a > c) swap(a, c);
			if (a > b) swap(a, b);
			//Now the smallest element is the 1st one. Just check the 2nd and 3rd
			if (b > c) swap(b, c);
		*/
		if ($max[0]>$max[2]) { $temp=$max[0];$max[0]=$max[2];$max[2]=$temp;}
		if ($max[0]>$max[1]) { $temp=$max[0];$max[0]=$max[1];$max[1]=$temp;}
		if ($max[1]>$max[2]) { $temp=$max[1];$max[1]=$max[2];$max[2]=$temp;}
	}
}
var_dump($max);

?>

 

 

or ... version without sorting, but doing 3 passes of the input array (and only works if highest number is less than INT_MAX) :

 

<?php

$max = [0,0,0,0];
$input = [8, 1, 7, 5, 4];

$max[0]=PHP_INT_MAX;
for ($j=1;$j<4;$j++) {
	$max[$j] = PHP_INT_MIN;
	for ($i=0;$i<count($input);$i++) {
		$value=$input[$i];
		if ($value>$max[$j]) {
			if ($value<$max[$j-1]) {
				$max[$j]=$value;
			}
		}
	}
}
var_dump($max);
?>

would output results in reverse order:

 

array(4) {
  [0]=>
  int(9223372036854775807)
  [1]=>
  int(8)
  [2]=>
  int(7)
  [3]=>
  int(5)
}

 

Link to post
Share on other sites

16 hours ago, mariushm said:

If you're allowed to modify the input array, you could just sort the array.

Sorting algos take O(nlogn) time, vs finding 3rd would still be O(n).  On large arrays it would not perform well.

3735928559 - Beware of the dead beef

Link to post
Share on other sites

1 hour ago, wanderingfool2 said:

Sorting algos take O(nlogn) time, vs finding 3rd would still be O(n).  On large arrays it would not perform well.

 

Never said/claimed it's optimal, it's just one of many solutions. also in case of sorting, you could add a break condition to stop after 3 passes or whatever amount is needed to be sure the 3 maximums were found.

4 hours ago, shivajikobardan said:

php is terrible language man. those dirty dollars everywhere.

It's better than lots of other languages, and in this case the fact it's an interpreted language (just refresh browser page or run script after change for instant feedback) and no strict data types is a plus, one of the reasons why I like to do small scripts/experiments in it. 

When it was designed, having a character like $ to signify variables made sense because it allowed to do stuff echo "sum of $a and $b is $c".  and echo would replace the variables with actual values. You can use single quotes to treat the string as literal, no parsing.

 

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×