Home > Blockchain >  find the smallest of a set of integers c
find the smallest of a set of integers c

Time:01-29

#include <stdio.h>
int main () {
    int n, smNum = 1;
    printf("Enter a number: ");
    scanf("%d", &n);
    while (n != 0 || smNum != 0){
        printf("Enter a number: ");
        scanf("%d", &smNum);
            if (smNum< n)   smNum = n;
    }
    printf("The smallest number is: %d", n);
}

This program must determine the smallest prompted number and print it out. The program must stop when the user enters the number "0". That's when an error occurs. The program keeps demanding an entry.

CodePudding user response:

There is a little bit of confusion in the question in my opinion. I will try to understand what you wanted to do and maybe solve some of your doubts.

First of all, it looks a little odd to use the stdio.h functions in C . Even though it can be used and will definitely work, I would suggest using C, or use C functions for IO.

I guess you wanted to implement something like: "Create a C/C program that asks the user to enter some integers, and when the user enters a 0, the program stops and prints the smallest integer the user has entered (excluding the 0)"

I am going to maintain the general structure of your program so you might find it easier to read it:

#include <stdio.h>
int main() {
    int n;    // It will hold the user's input
    int min;  // It will hold the smallest number entered so far
    printf("Enter a number: ");
    scanf("%d", &n);
    min = n;          // This is the first number, so we save in the min variable so that we can compare it to the other values later
    while (n != 0) {  // As we said, we stop when n is equal to 0
        printf("Enter a number: ");
        scanf("%d", &n);
        if (n < min && n != 0) min = n;  // If n is smaller than min, it has to become the new min.
        // But this is not true when it is equal to 0, we just have to go out of the loop in that case
    }
    printf("The smallest number is: %d", min);
}

CodePudding user response:

First off, the code looks more C than C . You should be using <iostream> classes in C , instead of <stdio.h> functions. Either way, you should be validating the user actually enters valid numbers before acting on them.

That being said...

int n, smNum = 1;
printf("Enter a number: ");
scanf("%d", &n);
while (n != 0 || smNum != 0){

Regardless of what the user enters as their 1st input, the loop will always be entered at least 1 time, because the expression smNum != 0 will always be true on the 1st iteration due to smNum being initialized to 1 instead of 0.

printf("Enter a number: ");
scanf("%d", &smNum);
if (smNum< n)   smNum = n;

Inside the loop, regardless of what the user enters on subsequent inputs, n will always be the value of the 1st input, so the expression smNum < n will be true only for new inputs that are lower than the 1st input, but in that case smNum will be overwritten with the value of the 1st input.

If the user enters 0 for the 1st input, the loop will continue until the user enters 0 again. n will always be 0, and smNum will always be either 0 or positive because negative inputs are being discarded.

If the user does not enter 0 for the 1st input, the loop will continue forever, because n will never be 0, so the expression n != 0 will always be true.

printf("The smallest number is: %d", n);

You should be printing out smNum, not n.

That being said, try something more like this instead:

#include <stdio.h>

int askForNumber(const char *msg)
{
    int n, ret;
    printf("%s: ", msg);
    do {
        ret = scanf("%d", &n);
        if (ret == 1) break;
        if (ret == 0){
            while ((ret = getchar()) != EOF && ret != '\n');
        }
        if (ret == EOF) exit(0);
        printf("Invalid input, try again: ");
    }
    while (true);
    return n;
}

int main () {
    int n = askForNumber("Enter a number");
    if (n != 0){
        int smNum = n;
        while ((n = askForNumber("Enter another number")) != 0) {
            if (n < smNum) smNum = n;
        }
        printf("The smallest number is: %d", smNum);
    }
    return 0;
}

Or, using <iostream>:

#include <iostream>
#include <limits>
#include <cstdlib>
using namespace std;

int askForNumber(const char *msg)
{
    int n;
    cout << msg << ": ";
    while (!(cin >> n)) {
        if (cin.eof()) exit(0);
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Invalid input, try again: ";
    }
    return n;
}

int main () {
    int n = askForNumber("Enter a number");
    if (n != 0){
        int smNum = n;
        while ((n = askForNumber("Enter another number")) != 0) {
            if (n < smNum) smNum = n;
        }
        cout << "The smallest number is: " << smNum;
    }
    return 0;
}
  •  Tags:  
  • Related