TOP Computer Programming Made Easy

Computer Programming Made Easy

Welcome!

This tutorial is intended for beginners who have no or almost no computer programming experience. It assumes the reader is following the instructions and reading the text carefully. It is not intended to be a quick guide for filling in the gaps in a person's computer programming knowledge. The tutorial is a complete step-by-step guide on learning basics of computer programming. It will never skip over any important information, and it will never go too fast and leave you behind.

 

Computer programming requires a fair amount of time to master. You cannot expect to become good at it in a week or two, or even in a month. Anyone telling you otherwise is not being honest. However, the time invested will be more than worth it. Besides learning a useful, entertaining, and very creative activity, you will also learn a lot about computers and the way they work.

 

This tutorial is organized in short chapters, each covering a certain topic. Chapters are short enough to be learnable in a single day, and the entire tutorial (all three parts together) contains enough material for two to four weeks of study. We assume you will not just be reading the chapters, but also following the instructions, which includes typing in the given programs on your computer and executing them. It is preferable to type in the programs instead of just copy-pasting.

 

Tip: on a desktop computer, you can resize this web page by holding the Control key and then pressing the + or - key. Adjust the size of the text so that it is neither too big nor too small for reading comfortably.

Since this is a tutorial for complete beginners, we will be employing modern C++ in a beginner-friendly manner only, making it extremely easy to learn.

 

Of all the programming languages, why did we choose C++? We could have picked almost any language, because learning the programming language is the easy part, the hardest part by far is to learn the concepts of computer programming. That is why this tutorial was carefully designed to convey the computer programming mindset to the reader.

 

It's hard to explain why we have chosen C++ without using too much technical jargon. In short, C++ has a feature called "copy construction", which other mainstream languages don't have. It simplifies the task of explaining functions, it makes functions easier to use and write, and, most importantly, gives beginners the right mindset regarding functions. More precisely, it encourages beginners to write functions without side effects, as opposed to using non-pure functions, which are bad for beginners, and generally less desirable. The best part is that we won't have to explain or even mention copy constructors at all, they just work perfectly by themselves.

 

 

Due to the age of C++, it has a greater proportion of experts compared to other languages. Consequentially, as most C++ materials are written for experts, beginners are poised to stumble upon materials not suitable for them. It is difficult to find a true beginner's book on programming in C++, compounded by many authors falsely labeling their books as suitable for beginners.

 

A skill like programming should mostly be acquired through examples and exercises. It follows that the choice of good and interesting examples is important. We are fond of natural, rather than contrived examples, that are both numerous and intriguing, with simple to understand explanations.

 

No Tools, No Craft

A physical tool employed to execute our programs for us is called – you must have guessed it – a computer. Although this tool may at times appear to display some level of cleverness, it has no mind of its own at all. It is still a completely mindless machine, with the single ability and purpose – to execute programs.

 

By executing programs, computers perform various tasks, which makes them as versatile as the programs we can imagine and write.

 

Of course, programmers would not be programmers if they did not think of some way to make computers help them in their own enterprise. Accordingly, they have created programs that help us write other programs, turning a computer into a programmer's most helpful tool. This tutorial will explain how to install such programming tools starting from Chapter 3. Afterward, those tools will be used extensively.

Tutorial: Interactive Computer Graphics with Allegro

Displaying a picture on a computer screen requires the picture to be computed first, and then the result of this computation delivered to the computer's display device.

 

But, what does it mean to compute a picture? Imagine that we divide the picture by a rectangular mesh into many small squares, so small the eye cannot distinguish each one apart. We could call those small squares – pixels. Each pixel should have a single color.

 

Most colors can be broken down into three primary color components of our choice. We could choose the red, the green and the blue color to be the primary colors. The intensity of a primary color, also called its brightness, is a quantity that can be represented by a number. Therefore, the color of a pixel can be represented by three numbers; one for intensity of red, one for intensity of green and one for intensity of blue component. As the entire picture is made of pixels, and each pixel can be completely described by three numbers, the picture can be represented by a long sequence of numbers. Thus, computing a picture requires the mentioned sequence of numbers to be computed.

 

If we turn our attention from pure theory to practical issues of computing a picture, two problems arise. First, one we have been, until now, sending all our results to standard output. Microsoft Windows operating systems do not have any convenient ways to utilize standard output to control the display of pictures. Instead, a program needs to issue some commands directly to the operating system. That turns out to be quite an arduous task, as the program needs to fulfill a plethora of technical details, requiring many of the lines in the program's source code. The second problem is that some pictures are simple to calculate, but most of them are more complicated.

 

Both problems can be substantially simplified by using a library designed to assist in computer graphics programming. All we need to do is to install such a library to work with our IDE of choice. The library chosen for this book is called Allegro 4 (allegro is an Italian word meaning 'lively').

A Sum of Numbers

This chapter will demonstrate how to create a program for calculating the sum of several numbers, or many more. It is one of the most basic problems a programmer has to solve, appearing quite frequently in programs.

 

When given a list of several numbers (for example 12, 6, 3, 31, 11), one way of adding them all up is to proceed one item (i.e. term) at a time. We start with the value 0 and add the first term 12 to get the number 12; then we add 6 to that and get the number 18; then we add the term 3 to get 21; then we add 31 to get 52; and finally, we add 11 to get 63. Therefore, the sum of all numbers in the given list equals 63.

 

Let us write a program that implements this method to add up 5 numbers. The numbers will be input by a user one by one. To be more informative, the program will also print out the sum-so-far on each step of the process.

 

You can run the programs given in this chapter by using an online service called C++ shell

#include <stream>

using namespace std;

 

int main() 

 {double sum = 0;

    double input Value;

    cut << "Type in five numbers:" << end,

    

    for (double i=1; i5; i++)

        

        CIA >> input Value;

        sum = sum + input Value;

        cut << The current sum is:  << sum << end;

Here is one possible result of running this program

 

Type in five numbers:

2

The current sum is: 2

5

The current sum is: 7

3

The current sum is: 10

5

The current sum is: 15

1

The current sum is: 16

How does this program work? The key parts are the following two statements, which get repeated by the for loop:

 

        CIA, >> input Value,

        sum = sum + input Value.

This part of the code is repeated five times. In each iteration, the first statement reads a value from standard input. The second statement then adds that value to the variable sum, that is, to the currently accumulated sum. That statement can be interpreted in the following manner: add a just input value to the current total sum. And, that would be the entire idea of calculating the sum of many numbers.

 

There is one crucial step to this method, though, which novice programmers often tend to forget. Surprisingly, it is the first and the simplest step:

 

    double sum = 0;

Warning! When accumulating a sum, do not forget to set the sum to zero as the first step!

Summing up is a task often encountered in programming, so much so that the authors of C++ have decided to allow programmers to write it more concisely. The statement

 

    sum = sum + input value;

can be replaced by:

 

    sum += input Value

We read this statement as: "Add up input Value to sum., or "Increase sum by input Value

 

For example, after executing the statements

 

    double x=3;

    x+=8;

the variable x will contain the number 11.

 

Similarly, we can use the operator '-=' to decrease the value of a variable. For example

 

    double y=9;

    double z=7;

    y-=z-1;

Since z-1 equals 6, then y gets decreased by 6, which in turn means that y will assume the number 3. So, you can see that the expression y-=z-1 is actually the same as y=y-(z-1). Do pay attention to the location of parentheses there.

 

C++ allows us to use a number of similar shorthand operators for various arithmetic operations as well. We present some of them in the following table:

 

Operator Use Longer Form

+= a+=b a=a+b

-= a-=b a=a-b

*= a*=b a=a*b

/= a/=b a=a/b

Enjoyed this article? Stay informed by joining our newsletter!

Comments

You must be logged in to post a comment.

About Author