samedi 4 juillet 2015

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

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;

}

Aucun commentaire:

Enregistrer un commentaire