need help here

i was able to create a program that displays the highest and lowest value of the inputted number
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
#include <iostream>
using namespace std;

int main()
{
    int first;
    int second;
    int amount;
    int hold=99999999;

    cout<<"How many numbers will you enter?";
    cin>>amount;

    cout<<"Enter number: ";
    cin>>first;
    for (int x=1;x<=amount-1;x++)
    {
        cout<<"Enter number:";
        cin>>second;

        if (first<second)
        {
            first=second;

        }
        if (first&&second<hold)
            hold=first&&second;


    }
        cout<<"highest score: "<<first<<endl;
        cout<<"Lowest score: "<<hold;
}


can you show me other ways of doing this without using the variable hold=99999999 ?
closed account (48T7M4Gy)
Try it this way:

Start with both the highest and lowest being set as the first number.

For every subsequent number, if it is higher than the highest, reset the highest to that number.

And, if it lower than the lowest, set the lowest accordingly.

Input the next number ...

BTW you won't need your 'second' int.
Hmm... I think this is the exact same code, but I'm going to mess around with it after I post it lol
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
40
41
42
43
44
45
46
47
48
#include <iostream>

using namespace std;

void getNum(int *);
void testNum(int *, int *, int *);

int main()
{
    int low, high, num;
    int amount;

    cout << "How many numbers will you enter? ";
        cin >> amount; cout << endl;

    // Get the first number in order to set the initial low and high value
    getNum(&num);
    low = num;
    high = num;

    while(amount-1 > 0) // Used amount-1 because we've already entered 1 number
    {
        testNum(&low, &high, &num);        

        amount--;
    }

    cout << "Lowest: " << low << endl;
    cout << "Highest: " << high << endl;

    return 0;
}


void getNum(int *num)
{
    cout << "Enter number: ";
        cin >> *num; cout << endl;

}

void testNum(int *low, int *high, int *num)
{
    getNum(num);

    if(*num < *low) *low = *num;
    else if(*num > *high) *high = *num;
}
Last edited on
closed account (48T7M4Gy)
@Sherre02

Yeah, looks to be doing the same as my pseudocode.

A very comprhensive use of reference passing.
Topic archived. No new replies allowed.