0
votes

I must write a program, which reads in loop a list of numbers, until user give number 0. Number 0 is not included in calculations, is only signal for end data. Next program write min and max element from string of numbers, and how many times it was entered. For example for (2,3,1,5,4,2,3,1,5,1)

Min 1 3 times

Max 5 2 times

I have problem with number, cause when I have max and enter lower numbers my lmax is getting higher. Second problem is program don't respond on '0'. What am I doing wrong?

int number;
int min = 0;
int max = 0;
int lmin = 1;
int lmax = 1;
do {
    cout << "Number: ";
    cin >> number;

    if (min == 0 && max == 0) {
        min = number;
        max =  number;
    }

    else if (number > max && number !=0) {
        max = number;
        lmax = 1;
    }
    else if (number < min && number != 0) {
        min = number;
        lmin = 1;
    }
    else if (number = max) {
        lmax++;
    }
    else if (number = min) {
        lmin++;
    }


    } while (number != 0);

    cout << "Min " << min << " " << lmin << " times" <<endl;
    cout << "Max " << max << " " << lmax << " times" << endl;

}

1

1 Answers

2
votes

You're using = rather than == for portions of your solution. Try changing

else if (number = max) {

to something like

else if (number == max) {