0% found this document useful (0 votes)
16 views6 pages

UU-COM-2013 Week 1 - Building Blocks

Uploaded by

glenn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

UU-COM-2013 Week 1 - Building Blocks

Uploaded by

glenn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

UU-COM-2013

Computer Programming

Building Blocks of program

what we are going to learn this week:

• Problem Solving
• Flow Charts
• Program Structure and basic programming concepts

Problem solving

Quite often, the programmer will be presented with (what seems to be a huge) problem that requires a solution. The problem
can usually be the requirement for some new software, the extension of functionalities of existing software, the redesign of
software or databases or something else. The solution will often be the program that you (often within a team of other
programmers) will create. The problem can appear to be complex and you, especially if you have limited experience, can
feel overwhelmed. However, there is a process that you can follow called Problem decomposition. This is the problem
solving strategy of breaking a (big) problem up into a set of (smaller and easier to tackle) subproblems, solving each of the
subproblems individually and then composing a solution to the original problem from the solutions to the subproblems. Each
of the subproblems can be broken down again into other subproblems. Eventually, the subproblems will become simpler and
approachable.

The above can be demonstrated diagrammatically in Figure 1 below where you’d be asked to design a bicycle:

Figure 1 - Problem decomposition

(Deitel, H. M., & Deitel, P. J. (2003). C How to Program (International ed.). Upper saddle River, NJ: Pearson.)

While at first appearing a daunting task, by breaking down the problem (or object) “bicycle” into smaller problems/units (or
sub objects, such as brakes or gears), you end up with a set of smaller and easier to manage tasks. Notice that the item
“wheels” is further broken down.

Objects are mentioned here for the first time. You will see this word more often as you progress through the Computer
Science courses (especially when you do your first Object Oriented programming course). For now, it’s enough to say that
we use this problem decomposition and the different objects to create “families” of objects (or even source code libraries
filled with objects that have sub objects or child objects etc) and that the objects can be reused or modified later on amongst
other functionalities.

UU-COM – 2013 Computer Programming Page 1


UU-COM-2013
Computer Programming

This decomposition process is used not only in computer programming but also in a variety of other areas
including medicine, engineering, design, architecture and construction amongst others.

Flow charts

A flowchart is a type of diagram that represents an algorithm, workflow or process, showing the steps as boxes of various
kinds, and their order by connecting them with arrows. This diagrammatic representation illustrates a solution model to a
given problem. Flow charts are closely related to the problem decomposition process mentioned earlier. They aid us in the
task of making a problem easier to comprehend, explain and tackle. They do this by showing the flow of data through a
system or process, while at the same time showing the sequence of steps needed for the said process to be completed. For
example, imagine that you have been assigned the task of designing a simple program for a point of sale (P.O.S.) machine
for a restaurant. The program should allow the customer to make an order of a burger, with the options of adding fries and
a drink. It should then calculate the price and accept the payment from the customer. The following information and the
sequence of steps needed are demonstrated in Figure 2 below:

Figure 2 - Flowchart of restaurant cashier software / P.O.S. machine

(Glassborow, F. (2006). You can program in C++: a programmer's introduction. John Wiley.)

Program Structure and basic programming concepts

Before we can talk about structure, let’s first see what we need in order to create a computer program. Besides the obvious
items (a working computer, loaded with an Operating System), you also need some sort of software that can edit, understand
and run computer code (also known as source code). For most programming languages, dedicated software exists, that can
do all the above, plus much more (such as automatically finding errors in the code and offering suggestions on possible
fixes). These programs are called Integrated Development Environments or IDE’s.

There are many IDE’s available, some are open source so you can use them for free (such as Netbeans, Eclipse or Dev-
C++), others are licensed software (such as Microsoft’s Visual Studio). The choice usually depends on the programmer.
What’s important is to use an IDE that supports your programming language of choice (and not all of them support all
languages). In this course, the language used will be C++. “The C++ programming language has a history going back to
1979, when Bjarne Stroustrup was doing work for his Ph.D. thesis. One of the languages Stroustrup had the opportunity
to work with was a language called Simula, which as the name implies is a language primarily designed for simulations.

UU-COM – 2013 Computer Programming Page 2


UU-COM-2013
Computer Programming

Now it’s time to talk about the program structure. Generally, when writing source code, most programs have a certain structure,
depending on the programming language used. For programs written in C++, this structure is (usually) as follows:

1. Importing certain libraries, headers, packages or files.

2. Writing the main function (also called main method)

3. Exiting the program

In addition to the above structure, other functions could exist before or after the main one. Furthermore, code comments
(these don’t have any function other than informing/reminding the programmer) could and should appear in the code,
unless it’s entirely trivial.

1. It is essential for a program to run.

2. It’s the starting point of execution of a program.

Figure 3 - Hello World program in C++

This figure shows the “Hello World” (the simplest and usual introductory program in any programming language)
program in C++. This is also a program that:

1. Imports a header (the “include” line). The header is called iostream and it’s the Standard Input / Output Streams
Library (used for cout on line 6).
2. The main function where in this case it is void (which means it needs no parameters/arguments to run). Notice the
keyword “int” in front of main. This means that this main, expects an integer variable to be returned (more on this later
on). The main has the cout line, which tells the program to print the message “Hello World” to the console.
3. The exit line (“return 0”). 0 is the integer which was expected to be returned to the main. In some IDE’s, you
might also have to enter the code: system("pause"); before return 0; to force the IDE to pause so that you can look
at the output.

Figure 4 below depicts a simple program that has two methods instead of one (the main, and another called addition). As
you can see in this case, the addition method comes before main. We could however place it under main (but in that case it
would require a prototype method, again before the main method. You will learn more about prototyping in the lessons to
come).

UU-COM – 2013 Computer Programming Page 3


UU-COM-2013
Computer Programming

Figure 4 - A program with two functions

Now, before we proceed any further, let’s see how we can type, compile and run a program such as Hello World (from
Figure 3). I’m assuming that you already downloaded and installed a C++ compatible IDE. I’m personally using Dev-
C++, but the process is similar in all IDE’s.

1. First you need to start your IDE.


2. Next you have to start a new project. We usually do this by going to File, New Project (depending on IDE).
3. You then choose the language (C++), console application and give a name for your Project.
4. After you press ok, you need to choose a location for the generation of the project file. The location is important
as all of your subsequent project files will be created in this location. I suggest making a folder beforehand and
creating the project in that folder.
5. Then you hit save, and you are presented with a new project and a ready made (but not very useful)
sample program. You can now edit the sample code and recreate the program from Figure 3.
6. Once you have done that, the program should be ready to compile. By compiling it, you (eventually) turn your
C++ source code into executable code that can run on a computer and at the same time the program also runs. To
compile and run it you go to Execute, compile and run and you will get the following screen (Figure 5).

Figure 5 - Hello World In C++

By pressing any key you are returned back to the code view of the IDE. If you want to save the project , you can go to:
File, Save all. For some more information on compiling, look at Figure 6 below, which shows the journey from source
code to executable program (you should also read various definitions that are available online).

UU-COM – 2013 Computer Programming Page 4


UU-COM-2013
Computer Programming

Figure 6 - Journey from Source code to Executable program

(Glassborow, F. (2006). You can program in C++: a programmer's introduction. John Wiley.)

UU-COM – 2013 Computer Programming Page 5


UU-COM-2013
Computer Programming

References

1. Deitel, H. M., & Deitel, P. J. (2003). C How to Program (International ed.). Upper saddle River, NJ: Pearson.
2. Stephen R. Davis (1994). C++ for dummies. San Mateo CA:IDG Books
3. Glassborow, F. (2006). You can program in C++: a programmer's introduction. John Wiley.
4. Schildt, H. (2003). C/C++ programmerʼs reference. McGraw-Hill/Osborne.
5. Davis, S. R. (2014). C for dummies. John Wiley & Sons.
6. Ashraf, Z. (2016). How to program in C++: with 100 examples. LAP Lambert Academic Pub.
7. Deitel, P. J., & Deitel, H. M. (2017). C++ how to program. Pearson.
8. LOSPINOSO, J. O. S. H. U. A. (2021). C++ Crash Course. O'REILLY MEDIA.
9. Forouzan, B. A., & Gilberg, R. F. (2020). C++ programming: an object-oriented approach. McGraw-
Hill Education.
10. Gregoire, M. (2021). Professional C++. Wrox, a Wiley brand.

UU-COM – 2013 Computer Programming Page 6

You might also like