-
Write a program with a function that will print a
line of n asterisks on a single line. You may only have a single
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. (So -1 is a ???.)
> ./a.out
How long would you like your line? 6
******
How long would you like your line? 3
***
How long would you like your line? -1
Goodbye!
>
Hint: first code the loop that will print the line in your
main(). Then take the code outside of main() and place it in a
definition. (What other two things will you need to add?) Once you
have a function that prints a line, then put a call to it in main(),
and put a loop around the call.
-
Write a program with an array of twenty integers. Use
a loop to set the values of the array to the even numbers
0,2,4,6,8,...38. Note that you do not need to use scanf to do this
(why not?). Use another loop to print the array on a single line
with a space between each number.
- Copy the program from problem 2. Make a program with an array of fifty integers. Use
a loop to set the values of the array to the multiples of three,
starting at zero. Use another loop to print the array, but with two
differences. Use a conditional inside the loop to insert a newline (backslash
followed by the letter n)
after every tenth number (this is similar to the kind of problem you
solved in lab02.10), and use formatting information (H&K 2.6) to
line up the numbers in columns as shown:
> a.out
0 3 6 9 12 15 18 21 24 27
30 33 36 39 42 45 48 51 54 57
60 63 66 69 72 75 78 81 84 87
90 93 96 99 102 105 108 111 114 117
120 123 126 129 132 135 138 141 144 147
- Copy the program you made for problem 1. Modify the function to
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. 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!
>
- Copy (again!) the program you made for problem 1. Using the same function
you wrote for that problem, you will now make a square of asterisks. Write
another function that will get called from main() with the size of
a square. If the size of the square is to be four, it will call the
asterisk line function from problem 1 four times.
> ./a.out
What size would you like your square? 4
****
****
****
****
What size would you like your square? -1
Goodbye!
>
-
Declare an array of 10 doubles. Write a while loop
that takes up to 10 numbers entered by the user (how will you get
the input?) until the user enters a negative number or the array is
filled. After the data is all entered, use a loop to sum the values
in the array. A negative number should not be entered in the
array or become part of the sum. Print the sum only once, after the
loop is finished. Demonstrate your program using the decimals
4.0, 4.1, 4.2, ... 4.9 inclusive.
- As you program, practice all of your Unix commands that have
been introduced in lab and class: ls, pwd, cd, cp, rm, mv, and mkdir
because they will be on the exams.