You are on page 1of 5

LAB 1

INTRODUCTION TO PROGRAMING
1.1 OBJECTIVES

- To know how to solve a computational problem


- To understand basics of program design and algorithm development
- To understand translation process
- To get familiar with IDE by writing your first C++ program
- To get familiar with the overall lab environment and routines

1.2 PRE-LAB READING

1.2.1 PROBLEM SOLVING AND ALGORITHMS

In theoretical computer science, a computational problem is a mathematical object representing a


collection of questions that computers might be able to solve. Similarly the process of finding solutions to
difficult or complex issues is known as problem solving.

While solving a given problem we always need to discover a method of its solution first, which in technical
terms called as Algorithm. After knowing the method of solution, you can give commands to the computer
accordingly to produce the desired results.
Algorithm is a sequence of precise instructions which leads to a solution.
For example the algorithm to get a sum of two numbers could be as follows:
 Get 1st number
 Store the number in variable x
 Get 2nd number
 Store the number in variable y
 Add x and y
 Store the result of addition in variable z
 Show the sum

FLOW CHARTS
It is often helpful to represent something in a pictorial way. 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. They help to visualize what is going on and to better understand a process.

Shape Description

Flow line (Arrowhead)


Terminal (Start/End)

Process

Decision

Input/output

Figure 1 Components of a Flow Chart

Activity 1:
Make a flow chart of the algorithm given above, by selecting appropriate symbols/shape.

1.2.2 PROGRAM & PROGRAM DESIGNING PROCESS

A computer program is a list of instructions that tell a computer what to do. Everything a computer does
is done by using a computer program. A computer program is written in a programming language.

Program design is a creative process. There is no complete set of rules, no algorithm to tell you how to
write programs. Still there are some steps you should follow to design your program. The entire
program design process can be divided into two phases, the problem-solving phase and the
implementation phase.
 Problem solving phase includes 1st of all problem identification, and then writing a method of its
solution in plain English
 Implementation phase produces final program translated from algorithm into a specific
language

1.2.3 TRANSLATION PROCESS OF A PROGRAM

Since the computer only understands low level binary code (sequence of 1’s and 0’s), there must be a
translation process to convert high level languages (e.g C++) to binary code. This is often done by a
compiler, which is a software package that translates high level languages into low level code. Without it
we can not run our programs. The figure below illustrates the role of the compiler:
The compiler translates source code into object code. The type of code is often reflected in the
extension name of the file where it is located. For example we will write source (high level language)
code in C++ and all our file names will end with .cpp, such as:

firstprogram.cpp
myprogram.cpp

When those programs are compiled, a new file (object file) will be created that ends with .obj, such as:

firstprogram.obj
myprogram.obj

After the compile process is completed, the computer must do one more thing before we have a copy of
the machine code (1’s and 0’s). Most programs are not entirely complete to be executed. They need
other modules previously written that perform certain operations such as data input and output. Our
programs need these attachments in order to run. This is done by linker which links our code with the
codes already written in software libraries. This produces what is called the executable code, generated
in a file that often ends with .exe such as:

firstprogram.exe
myprogram.exe

Figure 2. (next page) summarizes the translation process.


Figure 2 Program Translation Process

Once we have the executable code, the program is ready to be run.

1.2.4 WRITING YOUR 1 S T PROGRAM


An integrated development environment (IDE) is a software package that bundles an editor (used to
write programs), a compiler (that translates programs) and a run time component into one system.
Microsoft visual studio, NetBeans, Dev and CodeBlocks are some examples of IDE. They all facilitate us in
a same way to compile and execute the code except that they have different interfaces.
In this lab we are going to use “CodeBlocks” to practice programming in C++ language. However, you
may choose any IDE you are comfortable with.

Activity 2:
Use recommended programming environment to create a new source file MyFirstProgram.cpp
and write the following code in it:

#include <iostream>
using namespace std;

int main()
{
cout << ”Hello World!”;
return 0;
}
Code Explanation:
iostream is the header file which is responsible to add the functionality of getting input and showing
output in a program.
#include is a preprocessor directive which is used to include the iostream header file in the program.
Namepaces are used to prevent name collisions. We are using a namespace std for the same purpose.
int main() is a special function in all C++ programs; it is the function called when the program is run. The
execution of all C++ programs begins with the main function

Activity 3:
Compile and execute the above program and write below what you have seen on the screen.

_________________________________________

1.2.5 NUMBER SYSTEMS


Digital computers are the machines which respond numbers, all the data and instruction must therefore
be represented in a numeral format. Thus number systems are very important to understand because a
computer understands numbers only. The binary system has been found to be the most natural and
efficient system for modern digital machine.

Decimal Binary Octal Hexadecimal


0 0000 0 0
1 0001 1 1
2 0010 2 2
3 0011 3 3
4 0100 4 4
5 0101 5 5
6 0110 6 6
7 0111 7 7
8 1000 10 8
9 1001 11 9
10 1010 12 A
11 1011 13 B
12 1100 14 C
13 1101 15 D
14 1110 16 E
15 1111 17 F
16 10000 20 10

Table 1 Representation of numbers in different base

You might also like