Home > OS >  Find the smallest number from a string of n numbers where atleast one is even
Find the smallest number from a string of n numbers where atleast one is even

Time:12-12

it keeps saying i didnt insert an even number even if i did so,and if i get it to pass that state,it wont display the smallest number

#include <iostream>

using namespace std;

int x, y, k, k1, n, k2;

void nrmin() {
    cout << "n=";
    cin >> n;
    y = 99999999999;
    for (k = 0; k <= n - 1; k  ) {
        k1 = 0;
        cout << "x=";
        cin >> x;
        if (x % 2 == 0) {
            k1  ;
        }
        if (x < y) {
            y = x;
        }
    }
}

int main() {
    nrmin();
    k2  ;
    while (true) {
        if (k1 > 0 and k2 > 1) {
            cout << "min=";
            cout << y;
        } else {
            cout << "you didnt insert an even number" << endl;
            nrmin();
        }
    }
}

CodePudding user response:

OK, then let us analyse your source code and I will explain you the errors.

  1. using namespace std; should not be used in C . You should not open the complete std namespace. Always use fully qualified names
  2. int x, y, k, k1, n, k2;. Do never, under no circumstances, use global variables. Don't do that. Additionally: Variables shall have a meaningful name. Do not use 1 letter variabled without any meaning. And: Do always, without any exception intialize variables while defining them.
  3. The function "nrmin" has a no-meaning name. It is of type void and returns nothing. It tries to communicate via global variables. That is strictly forbidden.
  4. cin >> n. You must always check the return state of any IO-operation. Try foryourselv and enter "abc" instead of a number. What shall you program do?
  5. YOu variable "y" is of type integer. You assign a magic number "99999999999" to it. This number is to big and cannot fit into an integer data type. Do never use magix numbers or numbers that will not fit into an assigned data type. There are functions in header " that shall be used.
  6. In your for loop for (k = 0; k <= n - 1; k ) Define variable k in the initializer section. For the end condition. Always use k < n;: Never use k <= n - 1. Do not use post increment in the for loop. ALways use pre-increment. So, always use k instead of k
  7. The split of functionalities between main and nrmin is bad. Please use a more meaningful approach.
  8. In funcktion main you increment "k2" to "1". So, k2 will always be 1. It will never receive a different value. You if statement will always be false. Always. You will always get a the "you didnt insert an even number"- message
  9. You are using a endless loop while (true). This loop will rund endless and never stop. Your program will never stop
  10. endlis never necessary for std::cout. It will print a '\n' and flush the buffer. But this is not necessary. So, no need to do.
  11. Write comments. The more the better

Please see the below example program. One of many possible solution.

Please digest that and think fpor yourself, what is better to understand and why.

#include <iostream>
#include <limits>

// This function will show the minimum number of a row of numbers.
// The condition that at leaset one is even.
bool showMinimumNumber() {

    // We assume an error as default result
    bool resultIsOK_OneNumberWasEven = false;

    // Here we will store the smallest number. 
    // We initialize this with the biggest possible number, so that any comparison
    // will vreate a new samllest number
    int smallestNumber = std::numeric_limits<int>::max();

    // We want to know, how many numbers shall be checked.
    unsigned int countOfNumberToCheck = 0;

    // Tell user what to do. He shall give a count of numbers to be checked
    std::cout << "\nPlease enter, how many number shall be checked: \t";

    // Now read anumber and check, if that worked and if the number is acceptable
    if ((std::cin >> countOfNumberToCheck) and (countOfNumberToCheck > 1)) {

        // Read values in a loop and check them
        for (unsigned int k = 0; k < countOfNumberToCheck;   k) {

            // Here we will store the user input. the number to be checked
            int numberToBeChecked = std::numeric_limits<int>::max();

            // Read the number and check if that worked
            if (std::cin >> numberToBeChecked) {

                // Ok, we read a valid number
                // First check, if it is even
                if ((numberToBeChecked % 2) == 0) {

                    // Yes, at least one number was even. Remember that
                    resultIsOK_OneNumberWasEven = true;
                }

                // Now check, if we have a new smallest number
                if (numberToBeChecked < smallestNumber) {

                    // New smallest number found. Remeber that
                    smallestNumber = numberToBeChecked;
                }
            }
            else { // Problems while reading the number to be checked. Show error message
                std::cerr << "\nError while reading number for checking\n";
            }
        }
    }
    else { // SHow error message for the problem of reading the count of numbers to check
        std::cerr << "\nError while reading the count of numbers to check\n";
    }
    // Now, we were checking all the numbers. If there was at least onre even number, then show result
    if (resultIsOK_OneNumberWasEven) {
        // Show result
        std::cout << "\n\nYou entered at least one even number. The smallest number was: " << smallestNumber << "\n\n";
    }

    // Return the result of this function
    return resultIsOK_OneNumberWasEven;
}

int main() {
    // Inform user about the program
    std::cout << "\nThis program will read a given number of values and\n"
        "check, if at least one of the numbers was even.\n"
        "Additionally, it will show then the smalles number.\n\n";

    // Call subroutine, until we had at least one ven number
    while (not showMinimumNumber()) {

        std::cout << "\n\nYou did not enter at leaset one even number. Please try again!\n\n";

    }
    return 0;
}

And for answering that question, I would be happy about > 100 upvotes and an accepted anser :-)

CodePudding user response:

I'm still new here, but maybe it is because you never increased k2 to more than one so the if condition is never activated?

  •  Tags:  
  • c
  • Related