-
Write a program that sets a variable result
to the product of doubles x (value 33.5) and y (value
-141). (What type should result be?) Print result. Now change the
values of x and y (how? you know two ways to do this),
but do not reset result. Print result to see if it has
changed. Explain this behavior in terms of memory and the assignment
operator (in your comments).
- Write a program that asks for user input (an integer), and uses
a switch statement to print "emerald" if the user enters 8 and
"laura" if the user enters 14. For any other number the program
should print "spam". You may only use three cases.
- Use a shell command to copy the previous program. Place a loop
around the appropriate code so that the user is asked for data
repeatedly. You may add some code to make it work properly. The
program should stop when the user enters -1 (what is that number
called? How should it appear in your program?).
- Write a program that asks for user input (a positive integer), and uses
a switch statement to print "red" if the number is 0-5; "white"
if the number is 6-11; and "chartreuse" if the number is
12-17. Any other number should print "mauve". You may only use
four cases.
- Write a program that computes the average of a series of five
numbers entered by the user. The program should use a loop to read
one number at a time (How do we read numbers from the user? How
should five be represented in your program?). If you can, do it
without reading the hint at the bottom of the page1.
-
Write a for loop that prints n asterisks in a
line. You may only have a print statement that prints a single
asterisk! Ask the user how long the line should be, and print
it. Repeat until the user enters a length of -1.
> ./a.out
How long would you like your line? 7
*******
How long would you like your line? 3
***
How long would you like your line? -1
Goodbye!
>
- Copy the program you made for 6. Using the same single
for loop, make a square of asterisks. This time, instead of looping
up to the user input number, loop until the input number squared
(i.e. print enough asterisks to fill a square of size
"input"). Once that is working, put a statement inside the loop
that uses a conditional to print a single newline (backslash
followed by the letter n). The condition should be true when the
loop reaches the end of one line of a square. For example, if the
user enters "6", then the program will print 36 separate asterisks
with a newline after every sixth asterisk.
> ./a.out
How big would you like your square? 3
***
***
***
How big would you like your square? -1
Goodbye!
>
-
Here is a different way to solve the same
problem. Copy the program you made for 6. Add another for
loop so that the previous for loop is inside the new one. The two
for loops will need different variables (why?). Both for loops will
stop on the same number from the user, so that the program prints
squares of asterisks instead of just one line:
> ./a.out
How big would you like your square? 3
***
***
***
How big would you like your square? -1
Goodbye!
>