-
Copy the program from lab three that had a function that printed
a line of asterisks. Modify the function to
make a square of asterisks as follows. 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!
>
-
Here is a different way to 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!
>
-
Copy the program you made for problem 2. Using the same
function you wrote for that problem, you will now make a triangle of
asterisks. If the size of the square is to be four, it
will call the asterisk line function four times.
> ./a.out
What size would you like your triangle? 3
*
**
***
What size would you like your triangle? 4
*
**
***
****
What size would you like your triangle? -1
Goodbye!
>
-
Place the functions you have written for 2 and 3
into a new program, and give the user a menu that allows the user to
select an action or exit. Use a switch statement in main() to
implement the menu. The sentinel loop should run until the user
enters -1, but replace the -1 in your program with a defined
constant called STOP by adding a preprocessor directive (see section
2.1 of H&K).
> ./a.out
Type -1 to exit, 1 to print a square, or 2 to print a triangle: 2
What size would you like your triangle? 3
*
**
***
Type -1 to exit, 1 to print a square, or 2 to print a triangle: 1
What size would you like your square? 2
**
**
Type -1 to exit, 1 to print a square, or 2 to print a triangle: -1
Goodbye!
>
- Write a function called indent. It will be a void
function and takes a single integer parameter called
length. If the number is negative, the function prints
ërror" and returns. Otherwise, it prints length spaces
(nothing else) and then returns.
- Write a program that uses indent to create
the indents for a diagonal line. Your program will use a
for loop that calls indent to print the following
(based on user input):
% ./a.out
Enter height: 5
*
*
*
*
*
%
- Declare an integer variable sum. Using a while loop, take a series of
five integers from the user, and add each one to sum. After the loop
is over, print the sum:
> a.out
Please enter an integer: 2
Please enter an integer: 4
Please enter an integer: 3
Please enter an integer: 1
Please enter an integer: 2
The sum is 12
>
- Write a program that takes positive integers from the user until the user
enters a sentinel -1, and then prints the average of the integers
entered. Use a unary type cast so that the correct kind of division
is performed. The sentinel should not be considered data.
- Declare an array of ten integers, and use a for loop to
initialize the array to the integers 10-19. Print the array using a
different for loop.
-
Declare an array of ten integers, and use a for loop to
initialize the array ten numbers entered by the user. Print the
array using a different for loop.