who can help me

It is my final assignment, but i dont know how to do....can someone help me?

Write a program that reads integers, and finds the smallest and the largest. The input ends with number 0. Suppose that you entered 3 5 2 7 -1 5 0; the program finds that the smallest is -1 and largest is 7. You have to create functions smallestNumber and largestNumber, and store the entered numbers in an array.

Sample 1:

Enter an int value, the program exits if the input is 0:: 3 5 2 7 -1 5 0
User entered: 3 5 2 7 -1 5
The smallest number is -1
The largest number is 7


Sample 2:

Enter an int value, the program exits if the input is 0: 0
There is no list of numbers.
Sample 2:

Enter an int value, the program exits if the input is 0: 0
There is no list of numbers.


1
2
#include <iostream>
int main() { for ( int i = 1; i; std::cin >> i ); }


Have to admit ... it's not the most useful program on earth.
Last edited on
How much of it can you do?

If you've written some code, it can be improved. But we can't improve nothing.
Have to admit ... it's not the most useful program on earth.

I'd say the samples are expected output from the same program with different inputs, not two different programs.

If the user inputs zero as the first value to be sorted the program is going to end before it does anything.
Ah, I see! Thank-you @Furry Guy! I completely misread that question.

But now I'm lost as to why exactly one has to store the numbers in an array, given that you just have to find the smallest and the largest and do nothing else with them.
Last edited on
I'd conjecture the requirement of using an array is an instructor/course mandate.

Printing out again what the user entered does need some form of a container.

Most "Teaching C++" guidelines IMO suck donkey balls.
There are zillions of ways of doing this. Here's a start with one of them:

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
36
37
38
39
#include <iostream>

const int limit = 5;

int main()
{
    int array[limit]{0};
    
    int smallest = 1000;
    int largest = -1000;
    
    int counter{0};
    
    while( counter < limit )
    {
        std::cout << "Please enter a number: ";
        std::cin >> array[counter];
        
        if( array[counter] == 0 )
        {
            break;
        }
        else
        {
            if( array[counter] < smallest )
                smallest = array[counter];
            
            if( array[counter] > largest )
                largest = array[counter];
            
            std::cout << array[counter] << '\t' << smallest << '\t' << largest << '\n';
            counter++;
        }
    }
    
    std::cout << "The end ...\n";
    
    return 0;
}
thank you, againtry
you are nice guy
1
2
thank you, againtry
you are nice guy 


My pleasure, and I try to be - at least some of the time.

Hopefully it is useful as a learning exercise rather than an a panic copying fix. Your call of course. :)
Topic archived. No new replies allowed.