C Whileloop controlled by a sentinel value Write a program t

C++

(While-loop controlled by a sentinel value) Write a program that reads an unspecified number of integers. Your program ends with the input 0. Determine and display how many positive and negative values have been read, the maximum and minimum values, and the total and average of the input values (not counting the final 0).

Solution

int main()

{

    int sum=0;

    int pos=0;

    int neg=0;

    double ave=0;

    char arr[100] = {\'\\0\',};

    std::cout << \"Enter an integer, the input ends if it is 0: \" ;

    gets(arr);

    int index = 0;

    char ch[1];

    bool negativeNumber = false;

    while(true)

    {              

        ch[0] = arr[index++];

        if(ch[0] == \' \') // Check space and continue;

        {

            continue;

        }

        else if(ch[0] == \'0\' || ch[0] == \'\\0\') // check for 0 or NULL and break;

        {

            break;

        }

        if(ch[0] == \'-\') // Set flag if \"-ve\"

        {          

            negativeNumber = true;

            continue;

        }

        int digit = atoi(ch);

        if(negativeNumber)

        {

            digit *= -1;

            negativeNumber = false;

        }

        if(digit > 0)

        {

            pos++;

        }

        else if(digit < 0)

        {

            neg++;

        }

        sum += digit;

    }

    ave= (double)sum/(pos+neg);

    cout <<\"The number of positives are \" << pos <<endl;

    cout <<\"The number of negatives are \" << neg <<endl;

    cout <<\"The total is \" << sum << endl;

    cout <<\"The sverage is \"<< ave << endl;

    return 0;

}

C++ (While-loop controlled by a sentinel value) Write a program that reads an unspecified number of integers. Your program ends with the input 0. Determine and
C++ (While-loop controlled by a sentinel value) Write a program that reads an unspecified number of integers. Your program ends with the input 0. Determine and

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site