book cover
book cover

You Can Do It!

A Beginner's Introduction to Computer Programming

by Francis Glassborow with Roberta Allen

Home      Contact Us      News & Updates

Contents

Chapter 1: You Can Program

In this chapter I will introduce you to the essential programming tools and show you how to use them to write your first program. You will find that this chapter is packed with screen images. In so far as is possible these are exactly what you should see though I cannot promise that future versions of Windows will not introduce minor variations. I place immense value on ensuring that if you do what I say you will see what I see.

I will also try to give you a sense of what programming really is and show you that you can already program though not program computers. By the end of this chapter you should be able to write a very simple program for yourself that will build more complicated images from an instruction to place a colored square in a window at a place of your choosing.

Chapter 2: You Can Loop

One of the most fundamental guidelines for good programming is to avoid writing something more than once. Programming languages provide a variety of mechanisms to help you to avoid repeating code. The most basic of these is the concept of looping. In this chapter, I will introduce you to one of the ways that C++ supports looping.

Chapter 3: You Can Write a Function

A function is another tool for avoiding writing things twice. In this chapter I will introduce you to the design, implementation and use of functions in your programming. A second benefit of using functions is that they provide a mechanism for naming what you are doing.

Chapter 4: You Can Communicate

In this chapter I will be covering various aspects of programming that can be loosely placed under the heading of communication. Good use of names and the C++ namespace facility makes source code more readable. Getting information into and out of a program is important and you need to learn more about the C++ streams mechanism for doing those things effectively. Finally you need to know a little about how a program can communicate internally when it meets unexpected problems such as a disk being full, or being given unusable data. On the way I will introduce you to a couple more of the types provided by C++.

Chapter 5: You Can Create a Type

This chapter and the next were originally one but it grew to be an unreasonable size. This chapter now focuses on creating user-defined types. The major objective is to help you understand more about what a type is by following through the process of creating one. I will also introduce the basic floating-point type (double) used in C++.

The type I have chosen (one that represents a point on a two-dimensional plane) requires some mathematical expertise. This may make it harder for some readers to follow the fine detail but it makes it a good example of encapsulating specialist knowledge. That encapsulation of knowledge empowers people who lack it because they can use the type without having to know how it works; they only have to know what it does.

Chapter 6: You Can Use point2d

In the last chapter you accompanied me as I designed a new type to represent the concept of a point on a plane. In this chapter I will show you how that simple type greatly extends what you can do. The main focus of this chapter is developing and using geometric shapes. We will discover that coupling the concept of a container (for our purposes, std::vector) with our newly defined point2d provides a great deal of power. At the end of this chapter I add some detail about handling the origin used by Playpen objects.

Chapter 7: You Can Have Fun

Programming isn't all about learning new things; a big part is using what you know. The first six chapters have been packed with new ideas and facts. The purpose of this chapter is to give you a break from learning new things and a chance to use what you have learned. I also hope you will take some time to plan for the future.

Chapter 8: You Can Write a Menu

In this chapter I will show you the basics of writing a menu-driven task. Modern operating systems and applications make extensive use of menus of all kinds. They both show the user (of the program) what can be done at this point and provide a mechanism for choosing which of those things to do. At this stage in your programming we will be writing simple text and keyboard driven menus. However the principles are the same as those used for graphical and mouse driven (sometimes called ''graphical user interface'', or GUI -- pronounced gooey to rhyme with gluey -- for short) programs.

In addition to the specific objective (menus) you will also see how a program develops by dealing with one problem at a time rather than trying to do everything at once. On the way you will learn about another form of user-defined type and I will introduce you to two functions from my library that fill areas of the Playpen with a single color.

Chapter 9: You Can Keep Data

Persistence has a special meaning in programming; it refers to the property of data that survives from one execution of a program to another. In this chapter you will learn how to develop the sample menu program of the last chapter so that you can save and restore the images you create. You will learn about a mechanism for saving actions that a program user takes during an execution of the program rather than just the end result. A consequence of that will be the ability to provide editing and undoing of actions. To do this you will learn how to use std::stringstream objects as temporary data stores. You will also learn about the important programming concept of an iterator and how that concept is supported by C++.

Chapter 10: Lotteries, Ciphers and Random Choices

The main focus of this chapter is on the concept of random numbers. In computing this takes two forms: genuinely random values determined by some unpredictable external event and pseudo-random values generated by carefully designed functions.

In this chapter we will provide genuinely random values by using a feature of my library that allows a program direct access to a computer's keyboard. Pseudo-random numbers will be provided by two functions from the C++ Standard Library. Both kinds of random value are useful in programming, as I will demonstrate by developing programs that use one or other in an appropriate fashion.

Chapter 11: Keyboards and Mice

In this chapter you will learn about the final pieces of hardware support provided by my library. I will provide the details of the fgw::keyboard type that you used in the last chapter and introduce the fgw::mouse type, which provides minimal support for a mouse.

This chapter focuses mainly on the technology rather than on programs. When you understand what fgw::keyboard and fgw::mouse provide you will quickly see how they can enrich your programming. You should want to revisit some of your earlier programs and rework them to incorporate these new facilities. By doing this you will learn more about the process of software maintenance. Programmers working on commercial software have the same problem when new hardware becomes available. Those whose source code was well structured and designed for change will find their task relatively easy; those who took the view of getting their code to work by trial and error will find the cost of change is high.

Chapter 12: A Pot Pourri Spiced with Bitset

In this chapter I introduce the special container bitset that is provided by the C++ Standard Library. This is a fixed size container of n bits (binary digits). The size, n, is chosen by the programmer and the value must be provided in the source code.

As well as introducing you to the bitset container, this chapter is designed to show you how a simple programming tool can be useful in a variety of different ways. You will see bitset being used for three very different purposes. The possibilities are only limited by your imagination and interests. As you gain understanding of more components of C++ you will have to spend time choosing the right ones for the job you want to do.

Each of the three example programs in this chapter relies on some domain knowledge, which I will provide. Even if the subject does not inspire you, working through the design and development of the programs will improve your programming.

Chapter 13:How Many. . .? in Which Set and Map Lend a Hand

In this chapter, you will learn what an associative container is. I will introduce two of C++'s associative containers, std::set and std::map, and show you ways they can be used. We will use these two types to handle a number of typical small problems that can prove quite demanding without the appropriate tools.

Chapter 14: Getting, Storing and Restoring Graphical Items

In this chapter I will introduce you to further details of implementing and using persistence. I will show you how to write code to get and save a rectangular part of a Playpen. You will also learn how to write code to restore a saved area to a location of your choice. I will tell you something about fonts and how to add text to the Playpen. During the process you will learn about two-dimensional vectors and make further use of std::map.

This chapter gives further examples of developing new types (called user-defined types to distinguish them from the fundamental types provided by the C++ language). I will walk you through the thinking processes I went through while achieving my objectives. This is deliberate. You need to learn by example rather than just see the end product.

Chapter 15: Functions as Objects and Simple Animation

In this chapter I will introduce you to the idea that a function is just another kind of object. Just like other objects, they have types, references to them can be stored in variables and they can be passed as arguments to functions with suitable parameters. Some people think that treating functions as objects is very difficult and cannot be of much use. They are wrong on both counts.

You will find that handling functions as objects is actually straightforward. I will also show you one way of using them that by the end of the chapter will allow you to produce animated images in the Playpen. These will include both animation on the spot and objects that move around the Playpen.

Chapter 16: Turtles and Eating Your Own Tail

In this chapter I introduce you to an entirely different way of viewing graphics. I will tell you about Seymour Papert's Turtle Geometry, which formed a part of the programming language LOGO. He designed LOGO for teaching children to program. As a side effect of learning about Turtle Graphics you will learn about recursion, another way to repeat some action.

Chapter 17: You Can Program

If you have got here by studying the previous 16 chapters you have discovered that you can program and that somewhere inside you there is a programmer. I hope you have also experienced the high spots of a tremendous kick when it all goes together and works. Unless you are unusually talented you will certainly have experienced low moments when the code just refuses to work. I hope the high points have more than compensated for the low ones. I know that has been true for me. There have been times while writing this book when I have been hitting my desk and shouting at my computer when source code simply would not behave itself. However there have also been plenty of times when I have felt overjoyed and even surprised at how well some code has worked.

If you have the patience it is probably worth going back and reworking the early chapters. I think you will be amazed at how much easier they are second time round. Much more important is that I think you will find that you are writing more elegant code second time around. The first time, your priority was to get the code working and to try to understand what you were doing. The sample solutions are not intended to be definitive, just examples of a way of solving the problems you have been given. Again you probably had to work quite hard at understanding the solutions Roberta and I provided. I know she had to work very hard understanding some of mine, but you should also know that sometimes she produced essentially better solutions than mine even if her code sometimes lacked polish.


Buy the book from Amazon.co.uk

 

© F. Glassborow & R. Allen 2003