CS 010 - Introduction to Computer Science I
Lab 4. Loops, constants, predefined functions

Points(10 overall)

Collaboration policy:

Collaboration on this lab exercise is strongly ENCOURAGED. The exercise is intended for practice, not assessment -- most people who try should get all participation points. Feel free to ask for help from, and provide help to, others. You shouldn't blindly copy solutions from one another (or from anywhere else) nor simply write code for someone else (even if you explain it as you write), but you can certainly help each other debug, give plenty of suggestions and hints, **explain** why things work or don't work, etc. If you get done early, feel free to walk around and help others.


Part 1:

Write a program to count down from a number input by the user to -5.
So if the user inputs 3, the output will be:

3, 2, 1, 0, -1, -2, -3, -4, -5


Part 2:

Write a program that continues to ask the user to input a number, until the user enters a number larger than 100.


Part 3:

Write a program that reads in ten integer numbers and that outputs the sum of all the numbers greater than zero, the sum of all the numbers less than zero, and the sum of all the numbers, whether positive, negative, or zero. The user enters them just once each and can enter them in any order. Your program should not ask them to enter the positive and negative numbers separately. Make the number of integers to be entered, in this case 10, a constant using the const modifier.

Here is a sample run of the program: (user input in bold)

        Enter 10 integers (positive and negative):
        1 4 -5 90 -45 -32 23 100 -8 -49

        The sum of all positive numbers is 218
        The sum of all negative numbers is -139
        The sum of all numbers (positive and negative) is 79

Hints:

You will need to use a while loop along with an if-else structure. Each iteration of the while loop should take care of one user input. Inside this while loop you will either add the user input to the sum of positives or to the sum of negatives. Parts 3 and 4 will be very difficult and/or tedious if you don't follow this hint.


Part 4:

Change the program in Part 3 to have the user type in 20 integers instead of 10. If you did part 3 correctly, you should only have to change one line of code, the const declaration.


Part 5:

Design a quadratic equation calculator. Recall that the quadratic formula

solves for the roots of the quadratic equation,

You should have the user input values for a, b, and c and then output the 2 values for x. You will need to use the predefined function, double sqrt(double), found in the cmath library. Also, if (b^2 - 4ac) produces a negative value, then the roots are complex (square root of a negative number). You can check for this and just output a message stating the roots are complex in this case.

Your program should allow the user to continue using the calculator until they are done.

Sample run of the program (user input in bold):

        Enter the values a, b, and c for the quadratic equation, ax^2 + bx + c = 0:
        2 5 3

        The roots for 2x^2 + 5x + 3 = 0 are (x = -1) and (x = -1.5).

        Would you like to go again? (y or n) y

        Enter the values a, b, and c for the quadratic equation, ax^2 + bx + c = 0:
        1 2 5

        The roots for 1x^2 + 2x + 5 = 0 are complex.

        Would you like to go again? (y or n) n


Part 6:

Write a program that allows the user to enter as many positive integers as they want. They should type in a -1 when they are done entering numbers. Your program should then output the maximum and minimum of the numbers just entered.

Sample run of the program (user input in bold):

        Enter as many positive integers as you want: (type -1 when you are done):
        45
        55
        20
        39
        2
        99
        35
        -1

        Maximum = 99
        Minimum = 2


Part 7:

Modify your program from Part 6 to also output the average value of all entries (except the -1).


Challenge Exercises:

Please feel free to help your fellow labmates reach this point!!

The following exercises are for your own benefit. You are not required to do these. In fact, they may contain material not covered in class or lab yet. As such, the TAs will give priority to helping students on the previous sections.


Challenge 1:

Write a program that prints out a box with the size given by the user. So, if the user wants a square of size 5, you should print the following:

*****
*****
*****
*****
*****

You can use the '*' character or allow the user to choose which character gets printed out.


Challenge 2:

Enhance the program from Challenge 1 to allow the user to print out a box OR a right triangle with a size of their choice. So, if the user chooses a right triangle of size 4, it should look similar to the following:

*
**
***
****