You are on page 1of 18

Chapter – 1

Universal Programming Concepts

Computer Programs

Computer programs are collections of instructions that tell a computer how to interact with
the user, interact with the computer hardware and process data. The first programmable
computers required the programmers to write explicit instructions to directly manipulate the
hardware of the computer. This "machine language" was very tedious to write by hand since
even simple tasks such as printing some output on the screen require 10 or 20 machine
language commands. Machine language is often referred to as a "low level language" since
the code directly manipulates the hardware of the computer.

By contrast, higher level languages such as "C", C++, Pascal, Cobol, Fortran, ADA and Java
are called "compiled languages". In a compiled language, the programmer writes more
general instructions and a compiler (a special piece of software) automatically translates these
high level instructions into machine language. The machine language is then executed by the
computer. A large portion of software in use today is programmed in this fashion.

Program Structure

Virtually all structured programs share a similar overall structure:

 Statements to establish the start of the program


 Variable declaration
 Program statements (blocks of code)

The following is a simple example of a program written in several different programming


languages. We call this the "Hello World" example since all the program does is print "Hello
World" on the computer screen.

The following is a simple example of a program written in several different programming


languages. We call this the "Hello World" example since all the program does is print "Hello
World" on the computer screen.

Language Example program

#include <stdio.h>
"C" void main() {
printf("Hello World");
}
C++
#include <iostream>

Page 1 of 18
int main()
{
cout << "Hello World";
return 0;
}

program helloworld (output);


Pascal begin
writeln('Hello World');
end.

Variable Declaration

Variables are place holders for data a program might use or manipulate. Variables are given
names so that we can assign values to them and refer to them later to read the values.
Variables typically store values of a given type. Types generally include:

 Integer - to store integer or "whole" numbers


 Real - to store real or fractional numbers (also called float to indicate a floating point
number)
 Character - A single character such as a letter of the alphabet or punctuation.
 String - A collection of characters

Language Example program

#include <stdio.h>
void main() {
int age;
float salary;
char middle_initial;

"C" age = 21;


salary = 29521.12;
middle_initial = "K";

printf("I am %d years old ", age);


printf("I make %f8.2 per year " salary);
printf("My middle initial is %c ", middle_initial);
}
Pascal
program myexample (output);
var
age: integer;
salary: real;
middle_initial: char;

Page 2 of 18
begin
age := 21;
salary := 29521.12;
middle_initial := 'K';

writeln('I am ', age, ' years old');


writeln('I make ', salary, ' per year');
writeln('My middle initial is ', middle_initial);
end.

The Boolean Operators

There are three Boolean operators: AND, OR and NOT. These operators are written
differently depending on the language being used. In mathematics, the logical operators are
written as     

The following table compares how AND OR and NOT are written in different programming
languages:

Language AND OR NOT

Mathematics

"C" or C++ && || !

SQL AND OR NOT

Pascal AND OR NOT

Perl & || !

Java & || !

Basic AND OR NOT

The Boolean operators are evaluated in the following fashion. For the AND operator, the
combination of two "True" values results in "True" all other combinations evaluate to False.
For example:

Page 3 of 18
True AND True evaluates to True
True AND False evaluates to False
False AND True evaluates to False
False AND False evaluates to False

For the OR operator, as long as one of the value is True, then the expression evaluates to
True:
True OR True evaluates to True
True OR False evaluates to True
False OR True evaluates to True
False OR False evaluates to False

The NOT operator is called the "complimentary" operator. It reverses the truth value of the
operand:
NOT True evaluates to False
NOT False evaluates to True.

The Boolean operators and their evaluations are often expressed in Truth Tables. We
write T and F to signify the values of True and False respectively. The following are truth
tables for the three operators:

T F T F T F

T T F T T T F T

F F F F T F

To use a truth table, choose a value from a row on the top and from a column on the left side
and then see where they intersect to determine what they evaluate to. For example, looking at
the truth table for AND, pick the value F for False on the top and then pick the value T for
True along the left side. Where these two intersect shows an F for False meaningTrue AND
False evaluates to False.

Comparison Operators

Boolean expressions often involve comparison operators that can be evaluated to determine if
they are True or False. Comparison operators
include: 

Different programming languages write these comparison operators in different ways:

Page 4 of 18
Language Equality Greater than Less than Inequality

Mathematics = > <

"C" or C++ == > < !=

Pascal = > < <>

SQL = > < <>

Perl == or eq > < != or ne

Java == > < !=

Basic = > < !=

Comparison operators are used to form expressions that can be evaluated as True or False.
For example, we might ask if there are more than 30 students in the class using the following
expression:
Number_of_Students > 30

If there are more than 30 students in the class, then this expression will evaluate to True. If
there are 30 or fewer students in the class, then this expression will evaluate to False.

The following are more examples of these types of expressions:

Question Expression

Does Alice make more than $35,000 per year? Alice_Salary > 35000

Did the NY Giants defeat the Dallas Cowboys? Giants_Points > Cowboys_Points

Did anyone get a perfect score on the test? TestScore = 100

For each of these examples, supplying values for each of the expressions allows us to
determine if that expression is True or False. For example, if Alice only makes $31,000, then
the first expression would evaluate to False.

Page 5 of 18
Combining Boolean and Comparison Operators

In the previous sections, we have seen how the Boolean operators and Comparison operators
can be used to form expressions. In this section, we combine the two types of operators to
form more complex expressions.

For example, suppose we ask the question: Are there more than 30 students in the class, and
are there more than 30 seats in the room? This might be expressed as:
Number_of_Students > 30 AND Number_of_Seats > 30

We evaluate such an expression by determining the value (True or False) of each of the
comparison expressions, and then use those values to evaluate the Boolean expression. In our
example, suppose the class is a very large one that is held in a large room. In other words,
there are more than 30 students and the room has more than 30 seats. Therefore, we would
evaluate the expression as follows:

1. The Number_of_Students > 30 portion of the expression evaluates to True.


2. The Number_of_Seats > 30 portion of the expression evaluates to True.

3. Substituting those values in the expression give us:


True AND True

4. The True AND True expression evaluates to True as can be seen in the Truth


tables for the AND operator.

5. Therefore, we conclude that the entire expression evaluates to True.

The following example expressions are based on these assumptions:

1. All employees belong to a department


2. All employees working in department 5 have salaries greater than $25,000
3. All employees in department 4 make exactly $40,000 per year.
4. Alice is an employee and she works in department 5
5. Bill is an employee and he works in department 4

Given the above 5 items are facts, consider the truth values of the following expressions:

Expression Evaluation

Alice_salary < 25000 is


False
Alice_salary < 25000 AND
Alice_Department = 5 is
Alice_Department = 5
True 
False AND True is False

Page 6 of 18
Alice_salary < 25000 is
NOT Alice_salary < 25000 False
NOT False is True

Alice_salary < 25000 is


False
Alice_salary < 25000 OR Bill_Salary =
Bill_Salary = 40000 is
40000
True 
False OR True is True

Alice_salary > 25000 is


True
Alice_salary > 25000 AND NOT Bill_Salary < 40000 is
Bill_Salary < 40000 False 
NOT False is True
True AND True is True

Conditional Statements (IF..THEN..ELSE)

Control statements "control" which sections of code in a program are executed. For example,
we might want a set of statements to execute only if certain conditions are met, otherwise, the
statements would not be executed. There are three general types of control statements:

Decision or Conditional statements are a type of Control statement and are often referred to
as IF..THEN..ELSE statements as can be seen in these examples:

Programming Language Conditional Code

if (condition) {
statements
"C", C++, Java, Perl } else {

else_statements
}
if (condition)
then
Pascal begin
statements
end;

Page 7 of 18
Iterative Constructs

Virtually all programming languages have a facility to allow a section of code to be repeated
(iterated). There are several variations to iterative or looping constructs. For the most part,
they fall into two categories: FOR loops and WHILE/DO loops.

FOR loops

Programming Language FOR loop

for (initialize; condition; increment) {


"C", C++, Java, Perl statements
}
for variable := low_integer to high_integer
do
Pascal begin
statements
end;

WHILE loops

Programming Language FOR loop


while (condition) {
"C", C++, Java, Perl statements
}
while (condition) do
begin
Pascal statements
end;

Graph Theory Basics

Many aspects of computer science, and especially programming can be explained or


demonstrated with the aid of graph theory.

Nodes and Arcs


Graph theory is based on the notion of Nodes (vertices) and arcs (edges). A node represents
some state of the system. It could represent the value of a variable or a particular situation.
We generally draw a node as a circle with some label inside of it. In the following figure,
three nodes (labeled, 1, 2 and 3) are defined.

Page 8 of 18
Arcs represent a way to transition from one node to the next. They can represent actions that
move form one node to another or relationships between nodes. Arcs always begin and end at
some node. It's possible for an arc to begin and end at the same node.

In the following figure, nodes 1, 2 and 3 of the graph have been joined by arcs
labeled a and b.

Tree Structures
Tree structures are quite common in computer science. They can represent a sorted collection
of numbers or names, or a series of decisions that need to be made. Like graphs (described in
another section), trees are made up of nodes and directed arcs. Nodes represent some value
and arcs point from one node to another. Trees are in many ways like graphs with restrictions
on how the arcs are arranged.

In computer science trees grow upside down. The root is at the top and the leaves are at the
bottom. Below is an example of a tree that is being used to hold some integers in a particular
order:

Page 9 of 18
The nodes in this tree represent integers. In this case, the integers 1, 3, 4, 5, 7, 8 and 10 have
been used. The root node is labeled as "5" and appears at the top of the tree. Severalinterior
nodes (nodes that are neither root nor leaves) labeled 3 and 8 appear in the middle of the tree.
Finally, leaf nodes with labels 1, 4, 7 and 10 appear at the bottom of the tree.

A Parent node is one that has at least one node below it. In the above tree, node 3 is a parent
and its Child nodes are 1 and 4. (Can you find the other 2 parent nodes in the graph?) Child
nodes with a common parent are called sibling nodes. In the example, nodes 1 and 4 are
siblings. As a rule, child nodes in a tree may only have one parent.

Basics of Linux Operating Systems – Getting Started with Linux

Installing Fedora 7
Now that you’ve planned your installation and prepared the computer, you’re ready to
actually install Linux. The following procedure describes the steps you must follow to install
Fedora 7 on a computer that has a bootable DVD-ROM drive:
1. Insert the Fedora 7 CD in the DVD drive and restart the computer.
The computer boots from the DVD drive and displays a Linux boot prompt, which looks like
this: Boot:
2. Press Enter.
The computer starts Linux from the installation disk. The screen shown in Figure 1-1 soon
appears. This screen gives you several options for starting the installation. The most common
is to simply press Enter.
3. Press Enter.
Now a bunch of text messages fly by your screen as Linux starts up. Eventually, the screen
shown in Figure 1-2 appears, offering to test the CD media you’re installing from to make
sure you have downloaded and burned the CD images correctly.

Figure 1-1: Selecting an installation option.

Page 10 of 18
Figure 1-2: Testing the CD media.
4. Press the Tab key and then press Enter to skip the test.
Still more text messages fly by, but soon Fedora switches into graphics mode and displays the
Welcome to Fedora Core screen, as shown in Figure 1-3.
5. Click Next.
The Language Selection screen appears, offering quite a few language choices, as shown in
Fig1-4.
6. Choose your language and then click Next.
The Keyboard Configuration screen appears. It lets you choose from about 55 different
keyboard styles.

Figure 1-3: Welcome to Fedora.


Page 11 of 18
Figure 1-4: Choosing a language.
7. Choose your keyboard type and then click Next.
Next, Fedora asks for the host name for the computer, as shown in Figure 1-5.
8. Enter the name you want to assign this computer and then click Next.
The Time Zone Selection screen appears, as shown in Figure 1-6. On this screen is a map of
the world with dots representing about 500 different locations throughout the world.

Page 12 of 18
Figure 1-5: Choosing the host name.

Figure 1-6: Choose a time zone.


9. Click the location that’s closest to you and then click Next.
Next, the Set Root Password screen appears, as shown in Figure 1-7. This screen lets you
enter the all-important root account password. It’s vital that this account be protected by a
strong password, so choose a good one. Write down the password somewhere and store it in a
secure location away from the computer. The screen shown in Figure 1-8 is displayed.

Page 13 of 18
Figure 1-7: Setting the root password.

Figure 1-8: Partitioning the disk.


Page 14 of 18
10. Click Next.
Fedora warns you that it is about to wipe out any existing data on your
hard disk, as shown in Figure 1-9.

11. Click Write Changes to Disk.


Next, the installation program displays the screen shown in Figure 1-10,
which lets you choose the optional features you want to install.
12. Select the Web Server and Customize Now options and then click Next.
The screen shown in Figure 1-11 appears. This screen allows you to further
customize the packages to be installed.

Figure 1-10: Choosing optional features.

Figure 1-11: Selecting additional packages.


Page 15 of 18
13. Click Servers.
The list of optional server packages appears, as shown in Figure 1-12.
14. Select the server packages you want to install and then click Next.
When you click Next, the installation program does some double-checking to make sure that
none of the packages you have chosen depend on other packages you have not chosen. If it
finds such a dependency, it adds the dependent package so that your system will function
properly. After these dependencies have been verified, the installation programs begins the
process of installing Linux. This will take awhile — maybe a long while — so now would be
a good time to grab a book or take a nap. When the installation finishes, the screen shown in
Figure 1-13 is shown. Pat yourself on the back for your ingenuity and perseverance.
15. Remove the installation disk from the drive and then click Reboot.
The system is rebooted. Installation is done!

Using the Setup Agent When Fedora restarts the first time after completing the installation
program, it launches a handy feature called the Setup Agent, as shown in Figure 1-14.
(The Setup Agent runs only if you installed a GUI.) The Setup Agent resembles the
installation program, but it asks a few questions that the installation program forgot to ask.

Page 16 of 18
Figure 1-14: The Setup Agent.
When the Setup Agent starts, follow these steps to see it through to completion:
1. On the Welcome screen, click Forward to get started.
The Setup Agent displays the License Agreement, as if after going through the previous 15
steps to install Fedora, you’re now going to decide you don’t agree to their terms. It’s useless
to resist.
2. Click Forward to confirm your acceptance of the license agreement.
The Setup Agent asks you to create a user account so that you don’t have to access the system
by using the root account, as shown in Figure 1-15.
3. Type a name and password for the user account and then click Forward.
The Setup Agent now asks you to set the date and time.

4. Select the correct date and time and then click Forward.
Now the Setup Agent asks if you want to submit details about your hardware configuration as
part of an effort to improve Linux’s support for various hardware devices.
5. Click Send Profile or Do Not Send Profile and then click Finish.
That’s all there is. Your Linux system is now set up and ready to go.

Page 17 of 18
Figure 1-15: Creating a user account.

Page 18 of 18

You might also like