• Home
  • Textbooks
  • You Can Do It!: A Beginners Introduction to Computer Programming
  • You Can Write a Function

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

Francis Glassborow

Chapter 3

You Can Write a Function - all with Video Answers

Educators


Chapter Questions

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 horizonta1_1ine $\mathrm{O}$ and vertical_1ine $\mathrm{O}$. You should now press $\mathrm{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
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

I want you to implement the following function declaration (put the declaration 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 from 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 carefully 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!
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
02:39

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$-by-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 "periect".

Mrinal Rana
Mrinal Rana
Numerade Educator