1

Notice this is a homework problem I am not looking for the answer, but rather hints to help me solve my issue. The goal of the program is to list min and max numbers inputted without using an array. The input of 0 should be inputted to exit the program, but should not be recorded as the "low" value. I attempted to set low to not equal zero in the if statement, but it returns a 0 regardless.

#include <iostream>
using namespace std;
int main(){
    cout<<"Enter a Number: ";
    double input;
    int max = -100000000;
    int low = 100000000;
    cin>>input;
    for(int i = 0; i = input; i++){
    cout<<"Enter Another Number:";
    cin>>input;
    if(input > max != 0) { max = input; }
    else if (input < low != 0) { low = input; }
    }
    cout<<"MAX NUMBER WAS: "<< max << endl;
    cout<<"LOWEST NUMBER WAS: "<< low;
return 0;
}
  • 2
    What do you think `if(input > max != 0)` is checking? – GBlodgett Oct 31 '18 at 19:02
  • @GBlodgett I emailed my prof. and he said and I quote "set max to a small number means -10000000 input != 0 (and not low) should be in the "if" part and do it also for the "if" part of max" – Mason penguin Holder Oct 31 '18 at 19:04
  • The first part (`input > max`) will resolve to a bool (int value either 0 or 1), so you will be comparing whether `input` is more than `max`, which probably isnt what you want – GBlodgett Oct 31 '18 at 19:05
  • usually, max set to the first number you get. – kelalaka Oct 31 '18 at 19:06
  • Instead first check to see if `input` is zero (`if(input == 0)`) and act accordingly to it. Then check to see if `input > max` – GBlodgett Oct 31 '18 at 19:06
  • 1
    Instead of using *magic* numbers (e.g. `-100000000, 100000000`) use the correct min and max for type, e.g. `int min = std::numeric_limits::max(), max = std::numeric_limits::min()` – David C. Rankin Oct 31 '18 at 19:18
  • Why not control your program with a `while ((cin >> input) && input != 0) { /* do your checks here */ }` loop? Further, you need only `if (input > max) max = input;` and `if (input < min) min = input;`. – David C. Rankin Oct 31 '18 at 19:23

4 Answers4

1

Because I dont have points to give you comments for hints, here i will add hints: So, the easiest way to get the right solution of any problem is to visualize it or write simple example on paper for example: user inputs:

1,4,6,10,0

Think to your self how do you find the smallest and the largest number if someone shows you this numbers in the same order? what will you do? So, at start when you seen the first number 1 for you is the smallest and the largest because you don't have more inputs right?

min = 1 and max = 1

That someone who is showing you the numbers in the given order now shows you the second number 4 What you will do at this moment ? For sure you are going to compare the if the input is smaller than the min(or 1) in your current stage, and if the input is smaller than the min you save the input value to the min or same thing for max: if the input is greater than max you save input value to the max(or in this case because our second number is 4 and the value of input is greater than max)so

max will take 4 and min will be 1

. You repeat same thing for all the inputed numbers, or you can make while loop if the input is -1 the loop will end. I think I give you the idea of what you need to do and help you with my hints. And sorry because I gave you the source code in my previous answer which I edited it now. :)

RistoS
  • 64
  • 2
  • What is the reason you added a system call at the bottom, seems unnecessary for the situation. Also, the question clearly stated they did not want the solution, so perhaps it would be beneficial to edit your answer to be more "hint-like"? – Carl Oct 31 '18 at 19:12
  • Or at least wrap it in preprocessor conditionals so it is only executed on windows, e.g. `#if defined (_WIN32) || defined (_WIN64) system ("PAUSE"); #endif` (note: that is 3 separate lines) – David C. Rankin Oct 31 '18 at 19:20
0

C++ offers this mechanism to find out machine specific min and max values, so you don't put values like 100000 to your variables:

std::numeric_limits<int>::min();
std::numeric_limits<int>::max();

You could use them for your min and max variables. As an alternative, you could use the first number given and assign it to both min and max.

So, in the end,

int number = 0, min = 0, max = 0;
std::cin >> number;
min = max = number;
while (number != 0) {
    if (number < min)
        min = number;
    else if (number > max)
        max = number;
    std::cin >> number;
}

Something like this would be enough. Then, you could use the variables however you want. This way, you didn't have to use an array for storing all the variables. Also, I suggest you to have a look at The Definitive C++ Book Guide and List if you are a beginner.

D. Jones
  • 461
  • 2
  • 16
0

1) Use a while(1) instead of for(int i = 0; i = input; ++i). This will be an infinite loop. You can add a break statement to come out of the loop using if condition. With i = input condition loop may never execute.

2) Break condition in your case is when input = 0. So add an if (input == 0) break; statement in the loop.

3) Modify the matching conditions to

if(input > max) { max = input; }
if (input < low) { low = input; }

4) Read the new input after matching conditions.

while(1){
    if (input == 0) { break; }
    if(input > max) { max = input; }
    if (input < low) { low = input; }
    cout<<"Enter Another Number:";
    cin>>input;
}
vinayawsm
  • 845
  • 9
  • 28
0

It is better to use while here rather than for loop that you have used. Try this :

    int n, max, min;
    std::cout << "Enter a No. : ";
    std::cin >> n;
    //Assign 1st input to max and min 
    max = min = n;
    while(1){
        std::cout << "Enter a No. :";
        std::cin >> n;
        if(0 == n) //Terminating condition for loop
            break;
        if(n > max)
            max = n;
        else if(n < mi)
            min = n;
    }
Card1nal
  • 1
  • 2