multiple float literals?

For my CS class I am supposed to design a program that says:

Write a program that asks the user how many integers they would like to enter. You can assume they will enter a number >= 1. The program will then prompt the user to enter that many integers. After all the numbers have been entered, the program should display the largest and smallest of those numbers (no, you cannot use arrays, or any other material we haven't covered). When you run your program it should match the following format:

How many integers would you like to enter?
4
Please enter 4 integers.
-4
105
2
-7
min: -7
max: 105

I dont know how to assign the different number of user input variables their own values.
The assignment is due on the 21st of january 2018.

any help would be appreciated.

edit: I am not allowed to use array or the conditional operator (?)
Last edited on
You need one variable for the min(with the max int value), one for the max(with the min int value) value and one for the current input.
After you got the number of inputs you can use a for loop to read all the numbers.
For each number you read you need to check if it is greater than the max value or smaller than the min value. If yes assign the new values.
he can't 'check' directly because he can't use a conditional operator.

will have to use std::max() and std::min() instead.

if that isnt allowed all I can think of to do is to subtract the current and the old min or max value and use bit logic to extract the sign bit then use implicit logic (0 is false) with no conditional operator to decide.


jonnin, personally I think that's misinterpreting it. By "conditional operator", I am 99% sure OP is talking about the ternary operator "? :" https://msdn.microsoft.com/en-us/library/e4213hs1.aspx

I think they are still allowed to use basic things like if statements. In fact, I'd assume the instructor would prefer the use of if statements before using built-ins like max (since it's a learning exercise).

@lostBytes
In pseudo code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
get number of items to process from user
get first input, n, from user
minimum number = n
maximum number = n
loop (number of times to get input - 1)
{
    getting input from user, n

    if n is greater than current maximum number
       current maximum number = n
    if n is less than current minimum number
        current minimum number = n
}
display minimum number
dispaly maximum number


You should also validate that the user entered a number > 0, and don't loop or display numbers if that is not the case.
Last edited on
> I am not allowed to use array or the conditional operator (?)

You do not have to use the conditional operator (?:).
Simple if statements with relational operators ( < and > ) is all that is required.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

int main()
{
    std::cout << "How many integers would you like to enter? " ;
    int n ; // #integers to be entered
    std::cin >> n ;
    // we assume that n >= 1

    std::cout << "Please enter " << n << " integers.\n" ;

    int current_value ;
    std::cin >> current_value ; // read in the first value (since n >= 1 )

    int max_value_so_far = current_value ;
    int min_value_so_far = current_value ;

    for( int i = 1 ; i < n ; ++i ) // for the remining n-1 values
    {
        std::cin >> current_value ; // read in the value

        // if this value is greater than max_value_so_far, this value becomes max_value_so_far
        if( current_value > max_value_so_far ) max_value_so_far = current_value ;

        // otherwise, if this value is smaller than min_value_so_far, this value becomes min_value_so_far
        else if( current_value < min_value_so_far ) min_value_so_far = current_value ;
    }

    // after the loop has executed (all numbers have been read in),
    // max_value_so_far contains the largest value entered by the user,
    // min_value_so_far contains the smallest value entered by the user,
    // print them out
    std::cout << "max: " << max_value_so_far << '\n'
              << "min: " << min_value_so_far << '\n' ;
}
Thanks for the help all. It worked!
I had to drop the class though cuz I need to learn the material better. I will pick up a more dumbed down version of intro to CS at a community college. Having an instructor for this class is a must for me. This was an Ecampus class through Oregon state
Topic archived. No new replies allowed.