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

cheese425

I need to find 3 largest numbers in an array and then add them together.

For example: Input: 3 4 7 10 11 16 16 23 26 Output: The sum of the 3 largest even numbers are: 16, 16, 26. The sum is 58

In my code, I'm getting weird outputs like "16, 1245782582792, 1".

Note: I can only use ifs/else, for/while loops, and arrays for this.

#include <stdio.h>
Tom Karzes

There are a few problems here:

  1. You should only examine the array entries that are defined. Instead, you are looking at the entire array, including the undefined portion from nNumbers through MAX_NUMBERS-1. You will likely pick up garbage values there. Change your for loops to:

    for (i = 0; i < nNumbers; i++)
    
  2. You are initializing greatest1, etc. to the first number in the array. That doesn't work if the number is odd and happens to be large enough to block the even number you're looking for.

  3. If one of the largest even numbers occurs more than once, you will ignore the duplicates. For instance, if the largest number is 1000, and it occurs three times, you probably want to add all three and return 3000. You can fix this by keeping track of the indices you have chosen, and only rejecting a duplicate if the index matches, rather than the value.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

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

From Dev

Find largest and smallest numbers in an array

From Dev

Javascript how to find largest numbers in array, and record positions

From Dev

C largest sum of 3 consecutive numbers from array

From Dev

find even numbers in given 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

How to find the Largest Difference in an Array

From Dev

How to find occurrences of the largest integer in C without array?

From Dev

How to find largest group of numbers in a string in bash

From Dev

C# : Find the largest palindromic number made from product of 3-digit numbers

From Dev

Finding the 3 largest numbers in an array/list of n numbers without the sorting

From Dev

Counting the largest sequence of even numbers

From Dev

C program - find largest sequence in array

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

From Dev

Returning array of largest numbers

From Dev

Return smallest even number from 3 arguments or largest uneven number if there are no even numbers

From Dev

Return smallest even number from 3 arguments or largest uneven number if there are no even numbers

From Dev

How to find the largest element of an array of known size?

From Dev

How to find array index of largest value?

From Dev

find the largest palindrome from the product of 3 numbers in python

From Dev

AS3 - How to find TWO biggest numbers in Array

From Dev

AS3 - How to find TWO biggest numbers in Array

From Dev

Find numbers with largest sum (Javascript)

From Dev

Find the largest three numbers in a row

From Dev

How to pull even numbers for an Array in MongoDB?

From Dev

How to calculate the percentage of even numbers in an array?

Related Related

  1. 1

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

  2. 2

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

  3. 3

    Find largest and smallest numbers in an array

  4. 4

    Javascript how to find largest numbers in array, and record positions

  5. 5

    C largest sum of 3 consecutive numbers from array

  6. 6

    find even numbers in given array

  7. 7

    Find the largest sequence in a given array of numbers

  8. 8

    find the largest three Numbers on the array of int swift

  9. 9

    How to find the Largest Difference in an Array

  10. 10

    How to find occurrences of the largest integer in C without array?

  11. 11

    How to find largest group of numbers in a string in bash

  12. 12

    C# : Find the largest palindromic number made from product of 3-digit numbers

  13. 13

    Finding the 3 largest numbers in an array/list of n numbers without the sorting

  14. 14

    Counting the largest sequence of even numbers

  15. 15

    C program - find largest sequence in array

  16. 16

    Find Two largest numbers in a list without using Array

  17. 17

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

  18. 18

    Returning array of largest numbers

  19. 19

    Return smallest even number from 3 arguments or largest uneven number if there are no even numbers

  20. 20

    Return smallest even number from 3 arguments or largest uneven number if there are no even numbers

  21. 21

    How to find the largest element of an array of known size?

  22. 22

    How to find array index of largest value?

  23. 23

    find the largest palindrome from the product of 3 numbers in python

  24. 24

    AS3 - How to find TWO biggest numbers in Array

  25. 25

    AS3 - How to find TWO biggest numbers in Array

  26. 26

    Find numbers with largest sum (Javascript)

  27. 27

    Find the largest three numbers in a row

  28. 28

    How to pull even numbers for an Array in MongoDB?

  29. 29

    How to calculate the percentage of even numbers in an array?

HotTag

Archive