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

Aaron Nam

In my code, I was able to find the largest integer within a set of numbers that ask to be inputted. I was not able to find the number of occurrences my largest integer was inputted. I feel like my problem is with the "if and else" statements. For example, when the first if statement is satisfied, I think it increments the "count" once and skips over all the other "if and else" statements and executes the last print function. So the count always ends up as 2.

What can I do to have the count count the number of occurrences of the largest integer?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

int main ()
{
    int count;
    int a,b,c,d,e;
    count = 1;

printf("Enter 5 integers within 1-10:\n");
scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);


if (e >= a 
 && e >= b 
 && e >= c 
 && e >= d){
    printf ("Largest integer is %d\n", e);
    count++;
    }

else if (d >= a 
      && d >= b 
      && d >= c 
      && d >= e){
    printf ("Largest integer is %d\n", d);
    count++;

    }

else if (c >= a 
      && c >= b 
      && c >= d 
      && c >= e){
    printf ("Largest integer is %d\n", c);
    count++;
    }

else if (b >= a 
      && b >= c 
      && b >= d 
      && b >= e){
    printf ("Largest integer is %d\n", b);
    count++;
    }

else {
    printf ("Largest is %d\n", a);
    count++;
    }       

printf ("Largest integer occurred %d times.\n", count);
system ("pause");
return 0;

}

Mureinik

I think you're over-complicating things. Instead of five variables, you could have just one, and input to it in a loop, saving the maximum and the count as you go:

#define NUMBER_OF_VARS 5

int i;
int input;
int curr_max = INT_MIN;
int count = 0;

for (i = 0; i < NUMBER_OF_VARS; ++i) {
    printf("Enter an integer: ");
    scanf("%d", &input);

    if (input > curr_max) {
        curr_max = input;
        count = 1;
    } else if (input == curr_max) {
        ++count;
    }
}

printf ("Largest integer is %d, appearing %d times\n", curr_max, count);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to find index of all occurrences of element in array?

From Dev

How do I find the largest integer less than x?

From Dev

How to find the Largest Difference in an Array

From Dev

SQL : How to find number of occurrences without using HAVING or COUNT?

From Dev

How to find array index of largest value?

From Dev

How to find largest value from array of NSDictionary objects?

From Dev

How to find the 100th largest number from an array

From Dev

Find Occurrences in Array

From Dev

How to sort string array in C with integer array in parallel? Without structs?

From Dev

How to Find all occurrences of a Substring in C

From Dev

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

From Dev

Split an integer and find the largest sum C++

From Dev

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

From Dev

How to return the largest integer in an Array that has 10 random integers in it?

From Dev

Find Two largest numbers in a list without using Array

From Dev

How do I find the largest number from a list and its occurrences using lambdas?

From Dev

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

From Dev

How to find the 100th largest number from an array

From Dev

Given an integer array of size n, find the k-th largest element without sorting the entire array

From Dev

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

From Dev

Java - Use binarySearch to find # of occurrences of certain element in array, without If

From Dev

Find Occurrences in Array

From Dev

How to Find all occurrences of a Substring in C

From Dev

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

From Dev

Find largest number of not sorted array without using linear and binary

From Dev

Find largest and smallest number in array using pointers in C

From Dev

Find how many times the largest digit appears in an array

From Dev

C program - find largest sequence in array

From Dev

How to return string attached to largest integer in an array (Javascript)

Related Related

  1. 1

    How to find index of all occurrences of element in array?

  2. 2

    How do I find the largest integer less than x?

  3. 3

    How to find the Largest Difference in an Array

  4. 4

    SQL : How to find number of occurrences without using HAVING or COUNT?

  5. 5

    How to find array index of largest value?

  6. 6

    How to find largest value from array of NSDictionary objects?

  7. 7

    How to find the 100th largest number from an array

  8. 8

    Find Occurrences in Array

  9. 9

    How to sort string array in C with integer array in parallel? Without structs?

  10. 10

    How to Find all occurrences of a Substring in C

  11. 11

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

  12. 12

    Split an integer and find the largest sum C++

  13. 13

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

  14. 14

    How to return the largest integer in an Array that has 10 random integers in it?

  15. 15

    Find Two largest numbers in a list without using Array

  16. 16

    How do I find the largest number from a list and its occurrences using lambdas?

  17. 17

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

  18. 18

    How to find the 100th largest number from an array

  19. 19

    Given an integer array of size n, find the k-th largest element without sorting the entire array

  20. 20

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

  21. 21

    Java - Use binarySearch to find # of occurrences of certain element in array, without If

  22. 22

    Find Occurrences in Array

  23. 23

    How to Find all occurrences of a Substring in C

  24. 24

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

  25. 25

    Find largest number of not sorted array without using linear and binary

  26. 26

    Find largest and smallest number in array using pointers in C

  27. 27

    Find how many times the largest digit appears in an array

  28. 28

    C program - find largest sequence in array

  29. 29

    How to return string attached to largest integer in an array (Javascript)

HotTag

Archive