简体   繁体   中英

How do you get the largest number and smallest number in a For-Loop function, The problems asks for a 10 number input and count specific type numbers

Hey guys I'm trying to figure this problem which counts as specific type of numbers yet I cant seem to get the largest and smallest number as stated on the problem, ps we are not allowed to use lists or what so ever that would make the code easier, we're on controlled structures and if statements atm. Here's the problem "Input 10 integers. Display the total count of positive, negative, odd numbers, even numbers, largest and smallest numbers"

#include <stdio.h>
int main()
{
int numInput;
int count;
int result = 0;
int postiveNum = 0, negativeNum  = 0, oddNum  = 0, evenNum = 0, largestNum = 0, smallestNum = 0;
printf("Please input a number: \n");
for(count = 1; count <= 10; count++)
{
scanf("%d", &numInput);
if (postiveNum >= postiveNum)
{
    largestNum = postiveNum;
}
if(postiveNum <= postiveNum)
{
    smallestNum = postiveNum;
}
if(numInput > 0)
{
postiveNum++;
}
 if(numInput < 0)
{
negativeNum++;
}
 if(numInput % 2 != 0)
{
    oddNum++;
}
 if(numInput % 2 == 0)
{
    evenNum++;
}
};

printf("Positive Numbers: %d\n", postiveNum);
printf("Negative Numbers: %d\n", negativeNum);
printf("Odd Numbers: %d\n", oddNum);
printf("Even Numbers: %d\n", evenNum);
printf("Largest Number: %d\n", largestNum);
printf("Smallest Number: %d\n", smallestNum);

}

You could do something like this

#include <stdio.h>

int main() {
    int numInput;
    int count;
    // initialize largest and lowest num with NULL
    int positiveNum = 0, negativeNum = 0, oddNum = 0, evenNum = 0, largestNum = NULL, smallestNum = NULL;

    // get 10 numbers as input
    for (count = 1; count <= 10; count++) {
        printf("Please input a number: \n");
        scanf("%d", &numInput);

        // if its the first iteration, initialize largest and lowest num with first input value
        // then compare the values from next iteration onwards
        if (count == 1) {
            smallestNum = numInput;
            largestNum = numInput;
        } else {
            // if input num is less than smallest num, rewrite the value
            if (numInput < smallestNum)
                smallestNum = numInput;

            // if input num is greater than the largest num, rewrite the value
            if (numInput > largestNum)
                largestNum = numInput;
        }

        // a number can't be positive and negative at the same time so use if else
        if (numInput < 0)
            negativeNum++;
        else
            positiveNum++;

        // a number can't be even and odd at the same time so use if else
        if (numInput % 2 == 0)
            evenNum++;
        else
            oddNum++;
    };

    printf("Positive Numbers: %d\n", positiveNum);
    printf("Negative Numbers: %d\n", negativeNum);
    printf("Odd Numbers: %d\n", oddNum);
    printf("Even Numbers: %d\n", evenNum);
    printf("Largest Number: %d\n", largestNum);
    printf("Smallest Number: %d\n", smallestNum);
}

You need to compare different variables in the if statement, change

if (postiveNum >= postiveNum){
    largestNum = postiveNum;

for

if (numInput > largestNum){ 
// you should compare to the variable you want to change inside the if statement
    largestNum = numInput;
}

the same for the smallest

if (numInput < smallestNum){
    smallestNum = numInput;
}

Also the largestNum should be initialized as the minimum posible. The same goes for smallestNum , but with the maximum posible. This considering that the user could input just negative numbers and none of them will be > 0 and your program will print 0 as the minimum number, even tho you never input that number.

Way to find the largest number amongst given input number

I assume there's an input range.

Initialize the variable largestNum with the min number of that range.

Then for each input compare largestNum with that input and update the value of largestNum accordingly.

Way to find the smallest number amongst given input number

Apply the same procedure for finding the smallest number.

The difference is, here you've to initialize the variable smallestNum with the max number of the input range.

Note : I've considered max input range as -10^6 >= input <= 10^6 . Change it according to your problem requirement.

Here's a possible sample code -

#include <stdio.h>
#define MIN_NUMBER -1000000
#define MAX_NUMBER 1000000
int main()
{
    int numInput;
    int count;
    int result = 0;
    int postiveNum = 0, negativeNum  = 0, oddNum  = 0, evenNum = 0, largestNum = MIN_NUMBER, smallestNum = MAX_NUMBER;

    for(count = 1; count <= 10; count++)
    {
        printf("Please input a number: \n");    // print it before taking every input
        scanf("%d", &numInput);

        if (numInput > largestNum)
        {
            largestNum = numInput;
        }
        if(numInput < smallestNum)
        {
            smallestNum = numInput;
        }
        if(numInput > 0)
        {
            postiveNum++;
        }
        if(numInput < 0)
        {
            negativeNum++;
        }
        if(numInput % 2 != 0)
        {
            oddNum++;
        }
        if(numInput % 2 == 0)
        {
            evenNum++;
        }
    }

    printf("Positive Numbers: %d\n", postiveNum);
    printf("Negative Numbers: %d\n", negativeNum);
    printf("Odd Numbers: %d\n", oddNum);
    printf("Even Numbers: %d\n", evenNum);
    printf("Largest Number: %d\n", largestNum);
    printf("Smallest Number: %d\n", smallestNum);
}

我注意到一个错误,positiveNum 变量是 inputNum,我应该比较 userInput

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM