• Home
  • Textbooks
  • You Can Do It!: A Beginners Introduction to Computer Programming
  • You Can Loop

You Can Do It!: A Beginners Introduction to Computer Programming

Francis Glassborow

Chapter 2

You Can Loop - all with Video Answers

Educators


Chapter Questions

00:53

Problem 1

Write a program that displays the three times table in the form: 3, 6, 9, ... (finish with 36 ) as a single row of output. Your program should be a simple modification of the program above that outputs the numbers from 0 to 9 .

Willis James
Willis James
Numerade Educator
01:51

Problem 1

Go back to the draw_functions.cpp file and look at the definition of draw_a_cross that should still be there. Now remove those two for-loops and replace them with suitable calls to horizontal_1ine $\mathrm{O}$ and vertical_1ine $\mathrm{O}$. You should now press F9 and get the same result that you did before. What we have done is demonstrate one of the major advantages of using functions: you can change an implementation of a function and link it with already existing code. That means that if you discover a better way of doing something. you only have to make the change in one place, the implementation of the function.

Mrinal Rana
Mrinal Rana
Numerade Educator
00:53

Problem 2

Repeat Exercise 1 but display the answers in a vertical column in the form:
$$
\begin{gathered}
1 \times 3=3 \\
2 \times 3=6 \\
\ldots
\end{gathered}
$$

Finish at $12 \times 3=36$. This is an exercise in combining output of numerical values with symbols and newline characters (the ' $\backslash n$ ' mentioned above).

Willis James
Willis James
Numerade Educator
01:51

Problem 2

There is another way that we could define a cross; we could give the point of intersection of the horizontal and the vertical together with the lengths of each of the four "arms". The declaration of such a function would be:
void draw_another_cross(fgw::playpen &,
int intersection_x, int intersection_y,
int left_arm, int right_arm,
int lower_arm, int upper_arm, fgw::hue);
Here is a test program for this function:
int main(){
playpen paper;
paper.scale(3);
draw_another_cross(paper, 5, 5, 10, 10, 20, 5, blue4 + red4);
paper.display();
cout << "Press return to end";
cin.get();
}
Implement draw_another_cross and compile and execute this program so that you test your implementation. (You should get a khaki cross with a long tail.)

Mrinal Rana
Mrinal Rana
Numerade Educator

Problem 3

Write a program that outputs a number, its square and its cube for all numbers from 1 to 20. Place the results for each number on a line; i.e. your output should start:
$$
\begin{array}{ccc}
1 & 1 & 1 \\
2 & 4 & 8 \\
3 & 9 & 27
\end{array}
$$
You will not be able to keep the output in neat columns with what you currently know about output in $\mathrm{C}++$ so do not spend time trying to do so. The solution to this exercise is a program that is similar to that for Exercise 2.

Check back soon!

Problem 3

I want you to implement the following function declaration (put the deciaration in drawing_functions.h and the definition in drawing_functions.cpp) and then test it. If you haven't realized, when I ask you to test something I mean write a program that uses it and tests that it works even when used in ways that you did not think about initially (like drawing lines from right to left as well as trom left to right).
void verticals (fgw: :playpen $\&$, int begin_x, int begin_y, int end $y$, int interval, int count, fgw: :hue);
Let me make certain that you understand the problem. I want a function that will produce a number (given by count) of vertical lines separated by the given interval. The first line must start at (begin_x, begin_y) and end at (begin_x, end_y). Each subsequent line should be an interval to the right of the previous one. For now you can assume that you will not be given a negative count (we are not quite ready to deal with impossible requests), but your solution should be able to handle a negative interval (which should draw successive lines to the left instead of right).

If you think carelully you will find that you can recycle the mechanisms you used in drawing a line and the functions that draw lines (or at least one of them). Good programming builds on what has gone before both by reusing code and reusing ideas.

Check back soon!
00:53

Problem 4

Write a program that outputs the values for multiplication tables from 1 to 12 . The results for each table should be on a single line (i.e. the way the results were displayed in Exertise 1). There are many ways to achieve the required result but you should be looking for one that only uses two for-statements.

Willis James
Willis James
Numerade Educator
01:00

Problem 4

This one should be easy. Repeat Exercise 3 but develop a function called horizontals, which draws a column of horizontal lines from similar data.

Emily Schilz
Emily Schilz
Numerade Educator

Problem 5

Write a program that displays all 256 colors provided for your playpen objects. The result should exactly fill the Playpen. That window is 512 pixels by 512 pixels. If you display 16 colors on each of 16 rows using a scale of 32 the results fit exactly. You will need to experiment to get the right starting points.

There is quite a lot of similarity between this program and the one for Exercise 4. One good solution uses two for-statements. However, remember that at this stage in your programming any program that produces the required output is satistactory.

Check back soon!
03:31

Problem 5

Use the functions you have developed for Exercises 3 and 4 to write (and test) a function called square grid that draws a grid of $n-b y-n$ squares on the screen.

This exercise has a little kicker in the tail. Unless you are abnormally insightful your first almost successtul attempt will, on close inspection, be missing a single pixel. Remember how our line drawing for-loops stop when they reach the end without actually plotting that end pixel? Now look at your solution to this exercise and add that final tiny piece that completes it.

This is also a common programming experience where we have to deal with a boundary case. As you gain experience you will get increasingly used to checking these tiny details that make the difference between "almost right" and "perfect".

Banhishikha Sinha
Banhishikha Sinha
Numerade Educator