C find the 2 largest numbers and 2 smallest numbers in float array

user3819295

I am really frustrated , I tried all day to make it works but never figured it out. I need to find 2 largest and 2 smalllest numbers from arr[11].

Here is what I have been done so far :

int main(){

float arr[11] ,max = 0, max2, min, min2;
int i = 0;

for (i = 0; i < 11; i++){
    scanf("%f", &arr[i]);
}

max = arr[0];
max2 = arr[0];
min = arr[0];
min2 = arr[0];

for (i = 1; i < 11; i++){
    if (arr[i] > max)
        max = arr[i];
    if (arr[i] < min)
        min = arr[i];
}

while (min == min2){
    min2 = arr[1 + i];
    i++;
}


for (i = 10; i > 0; i--){
    if (arr[i] > max2 && arr[i] < max)
        max2 = arr[i];
    if (arr[i] < min2 && arr[i] > min)
        min2 = arr[i];
}

printf("Max = %.2f\nMax2 = %.2f\nMin = %.2f\nMin2 = %.2f", max, max2, min, min2);
getch();
return 0;

}
chiastic-security

You could sort it, but that will be O(n log n) if the array gets any larger. It's pretty irrelevant for 11 elements, though, and if you're certain it'll always be 11, then sorting would be the easiest method.

Failing that, the simplest way, from the perspective of understanding the code, would be:

  1. Make one pass through the array to identify the min and the max. Record not just the min and max, but their locations too.
  2. Make another pass through the array, and find the min and max, but ignoring the elements you found in the first pass.

In terms of the code you have, the problem is here:

while (min == min2){
    min2 = arr[1 + i];
    i++;
}

This is after your first loop, where i ran from 1 to 10. At the end of the loop, the value of i would have been 11. But now you're keeping scanning, and i is still increasing... you've run past the end of the array!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find largest and smallest numbers in an array

From Dev

Sorting-(smallest 2 numbers in an array)

From Dev

Java - Finding Largest and Smallest Numbers using an Array

From Dev

Java - Finding Largest and Smallest Numbers using an Array

From Dev

How to find smallest numbers in an array

From Dev

How to find 3 largest even numbers in an array with C?

From Dev

R largest/smallest representable numbers

From Dev

R largest/smallest representable numbers

From Dev

Algorithm to find indexes of k smallest numbers in an array

From Dev

Find the largest sequence in a given array of numbers

From Dev

find the largest three Numbers on the array of int swift

From Dev

Find largest consecutive numbers in array and output numbers and how many there is

From Dev

Find the largest subset of a set of numbers such that the sum of any 2 numbers is not divisible by a given number

From Dev

Find location of numbers in 2d array

From Dev

print 2 numbers and skip 2 numbers in an array in C

From Dev

print 2 numbers and skip 2 numbers in an array in C

From Dev

Returning array of largest numbers

From Dev

Issue in displaying correct largest and smallest numbers

From Dev

Find largest and smallest number in array using pointers in C

From Dev

The 2 lowest numbers of an array

From Dev

How do I correctly get the largest and smallest numbers in a sequence of numbers?

From Dev

how to find indices of k smallest numbers in a multidimentional array?

From Dev

how to find indices of k smallest numbers in a multidimentional array?

From Dev

How to find Largest Prime Number, Smallest Factor except 1, Sum of Numbers

From Dev

How to find Largest Prime Number, Smallest Factor except 1, Sum of Numbers

From Dev

Fastest algorithm to find the largest palindrome that is the product of 2 numbers with the same number of digits

From Dev

Given a String of numbers separated by one or more spaces, find the 2nd largest number

From Dev

Find Two largest numbers in a list without using Array

From Dev

Find the order of numbers which will give the largest number in an array

Related Related

  1. 1

    Find largest and smallest numbers in an array

  2. 2

    Sorting-(smallest 2 numbers in an array)

  3. 3

    Java - Finding Largest and Smallest Numbers using an Array

  4. 4

    Java - Finding Largest and Smallest Numbers using an Array

  5. 5

    How to find smallest numbers in an array

  6. 6

    How to find 3 largest even numbers in an array with C?

  7. 7

    R largest/smallest representable numbers

  8. 8

    R largest/smallest representable numbers

  9. 9

    Algorithm to find indexes of k smallest numbers in an array

  10. 10

    Find the largest sequence in a given array of numbers

  11. 11

    find the largest three Numbers on the array of int swift

  12. 12

    Find largest consecutive numbers in array and output numbers and how many there is

  13. 13

    Find the largest subset of a set of numbers such that the sum of any 2 numbers is not divisible by a given number

  14. 14

    Find location of numbers in 2d array

  15. 15

    print 2 numbers and skip 2 numbers in an array in C

  16. 16

    print 2 numbers and skip 2 numbers in an array in C

  17. 17

    Returning array of largest numbers

  18. 18

    Issue in displaying correct largest and smallest numbers

  19. 19

    Find largest and smallest number in array using pointers in C

  20. 20

    The 2 lowest numbers of an array

  21. 21

    How do I correctly get the largest and smallest numbers in a sequence of numbers?

  22. 22

    how to find indices of k smallest numbers in a multidimentional array?

  23. 23

    how to find indices of k smallest numbers in a multidimentional array?

  24. 24

    How to find Largest Prime Number, Smallest Factor except 1, Sum of Numbers

  25. 25

    How to find Largest Prime Number, Smallest Factor except 1, Sum of Numbers

  26. 26

    Fastest algorithm to find the largest palindrome that is the product of 2 numbers with the same number of digits

  27. 27

    Given a String of numbers separated by one or more spaces, find the 2nd largest number

  28. 28

    Find Two largest numbers in a list without using Array

  29. 29

    Find the order of numbers which will give the largest number in an array

HotTag

Archive