123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- /*
- * Copyright (C) 2020, 2019, 2018, 2017 Girish M
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- * MA 02110-1301, USA.
- *
- */
- /*-----------------------------------
- Name: Girish M
- Roll number: cs1713
- Date: 25 July 2017
- Program description: Write a program to find the highest and second highest value in an array of n integers using less than 2n − 3 comparisons.
- Acknowledgements:
- ------------------------------------*/
- #include <stdio.h>
- int main()
- {
- int a[]={3, 2, 56, 2, 8, 5, 3, 2, 0, 4, 1, 43, 21};
- int i=0, max, loc;
- int secondMax;
- /* typedef struct loserList{
- } *l;*/
- max = a[0];
- while( i != 13 )
- {
- if(a[i] >= max)
- {
- max = a[i];
- loc = i;
- }
- i++;
- }
- printf("\nMax element: %d\n", max);
- a[loc] = -1;
- i = 0;
- secondMax = a[0];
- while( i != 12 )
- {
- if(a[i] != -1 && a[i] >= secondMax)
- {
- secondMax = a[i];
- }
- i++;
- }
- printf("\n2nd Max element: %d\n", secondMax);
- return 0;
- }
|