Fundamental Principles of Programming and Linear Structures
Fundamental Principles of Programming and Linear Structures
Mamapule, Siyabonga
❑Major Hardware components include the central processing unit (CPU); main memory
(MM), also called random access memory (RAM); I/O devices; and secondary.
❑CPU is the “brain” of a computer and the most important piece of hardware in a computer.
❑Software are programs written to perform specific tasks. There are two types of programs:
system programs and application programs.
❑->> System programs control the computer. The system program that loads first when you
turn on your computer is called the operating system.
❑->> Application programs perform a specific task. Word processors, spreadsheets, and
games are examples of application programs.
The Language of a Computer
• Well, what is the language of the computer?
How does it store whatever you type on the
keyboard?
Computers use digital signals
❑ Digital signals represent information with a sequence of 0s and 1s. A 0
represents a low voltage, and a 1 represents a high voltage.
❑ Because digital signals are processed inside a computer, the language of a
computer, called Machine Language, is a sequence of 0s and 1s.
❑ The digit 0 or 1 is called a binary digit, or bit. Sometimes a sequence of 0s and
1s is referred to as a binary code or a binary number.
The Language of a Computer
❑A sequence of eight bits is called a byte. Moreover, 210 bytes 1024 bytes is called a kilobyte (KB).
❑The Table below summarises the terms used to describe various numbers of bytes.
Identifiers
A third category of tokens is Identifiers. Identifies are names of things that appear in programs,
such as variables, constants, and functions
❑A C++ identifier consists of letters, digits, and the underscore character (_) and must begin
with a letter or underscore.
Following are legal identifiers in C++: First, Conversion, payrate, counter1
Legal Identifiers
Illegal Identifier Reason A Correct Identifier
Employee Salary There can be no space between employeeSalary
employee and Salary
should be 95
Writing a Program
Writing a program involves designing a strategy for solving a problem
and then using a programming language to implement that strategy.
Consider the simple problem Writing a program involves designing algorithms and
of computing the area of a
translating them into programming instructions, or code.
circle. How do we write a
program for solving this?
An algorithm describes how a problem is solved by
listing the actions that must be taken and the order of
their execution. Algorithms can help the programmer
plan a program before writing it in a programming
language. Algorithms can be described in natural
languages or in pseudocode (natural language mixed
with some programming code).
Pseudocode and Algorithm
The algorithm for calculating the area of a circle can be described as follows
1. Read in the circle’s radius.
2. Compute the area using the following formula:
area = radius * radius * p
3. Display the result.
When you code—that is, when you write a program—you translate an algorithm into a program. The main
function would look like this
int main()
{
// Step 1: Read in radius
// Step 2: Compute area
// Step 3: Display the area
}
Variables and Data types
The program needs to read the radius entered by the user from the keyboard. This raises two
important issues:
❑Reading the radius
❑Storing the radius in the program
To store the radius, the program needs to declare a symbol called a variable. A variable
represents a value stored in the computer’s memory.
Rather than using x and y as variable names, choose descriptive names: in this case, radius for
radius, and area for area. To let the compiler know what radius and area are, specify their data
types. That is the kind of the data stored in a variable, whether integer, floating-point number,
or something else. This is known as declaring variables. C++ provides simple data types for
representing integers, floating-point numbers (i.e., numbers with a decimal point), characters,
and Boolean types. These types are known as primitive data types or fundamental types.
Data Types
Data Type: A set of values together with a set of allowed operations
C++ data types fall into the following three categories:
❑Simple data type
❑Structured data type
❑Pointers
Simple data types: The simple data type is the fundamental data type in C++ because it
becomes a building block for the structured data type.
Integral, which is a data type that deals with integers, or number without a decimal part.
Floating point, which is data type that deals with decimal numbers
Enumeration, which is user-defined data type.
Data Types
Integral data types are further classified into the following categories: char, short, int, long, bool,
unsigned char, unsigned short, unsigned int, unsigned long, long long, and unsigned long long
To deal with decimal numbers, C++ provides the floating-point data type, which we discuss in this section. To
facilitate the discussion, let us review a concept from a high school or college algebra course. You may be familiar
with scientific notation. For example:
❑ 43872918 = 4.3872918 * 107 { 10 to the power of seven}
❑ .0000265 = 2.65 * 10−5 { 10 to the power of minus five}
❑ 47.9832 = 4.79832 * 101 { 10 to the power of one}
To represent decimal numbers, C++ uses a form of scientific notation called floating-point notation.
An enumeration in C++ is a user-defined data type that consists of a set of named integer constants. It
is used to assign meaningful names to integral values, improving code readability and maintainability.
Declaring Variables
Declare radius and area as double-precision floating-point numbers. The program can
be expanded as follows:
Note that cin (pronounced see-in) stands for console input. The >> symbol, referred to as the stream
extraction operator, assigns an input to a variable. As shown in the sample run, the program displays
the prompting message "Enter a radius: "; the user then enters number 2, which is assigned to
variable radius. The cin object causes a program to wait until data is entered at the keyboard and the
Enter key is pressed. C++ automatically converts the data read from the keyboard to the data type of
the variable.
Reading Input from ComputeAverage.cpp
1 #include <iostream>
the Keyboard 2 using namespace std;
3
Different techniques to read in input. 4 int main()
5{
❑You can use a single statement to 6 // Prompt the user to enter three numbers
read multiple input. For example, 7 double number1, number2, number3;
the following statement reads three 8 cout << "Enter three numbers: ";
9 cin >> number1 >> number2 >> number3;
values into variable x1, x2, and x3: 10
11 // Compute average
12 double average = (number1 + number2 + number3) / 3;
Note! 13
We say, “declare a variable,” but not “define a 14 // Display result
variable.” We are making a subtle distinction 15 cout << "The average of " << number1 << " " << number2
16 << " " << number3 << " is " << average << endl;
here. A definition defines what the defined item
17
is, but a declaration usually involves allocating 18 return 0;
memory to store data for the declared item. 19 }
Initialize Variables
Variables often have initial values. You can declare a variable and initialize it in one step.
❑Consider, for instance, the following code:
int count = 1;
❑This is equivalent to the next two statements:
int count;
count = 1;
You can also use shorthand to declare and initialize variables of the same type together.
❑For example:
int i = 1, j = 2;
Assignment Statements and Assignment
Expressions
After a variable is declared, you can assign a value to it by using an assignment statement.
In C++, the equal sign (=) is used as the assignment operator. The syntax for assignment
statements is as follows:
variable = expression;
An expression represents a computation involving values, variables, and operators that,
taking them together, evaluates to a value. For example, consider the following code:
int y = 1; // Assign 1 to variable y
double radius = 1.0; // Assign 1.0 to variable radius
int x = 5 * (3 / 2); // Assign the value of the expression to x
x = y + 1; // Assign the addition of y and 1 to x
area = radius * radius * 3.14159; // Compute area
You can use a variable in an expression. A variable can also be used in both sides of the = operator. For example,
x = x + 1;
Assignment Statements and Assignment
Expressions
In C++, an assignment statement is essentially an expression that evaluates to the value to be assigned to
the variable on the left side of the assignment operator. For this reason, an assignment statement is also
known as an assignment expression. For example, the following statement is correct:
cout << x = 1;
x = 1;
…which is equivalent to
cout << x;
i = j = k = 1;
which is equivalent to
If a value is assigned to multiple variables, you k = 1;
can use this syntax: j = k;
i = j;
Named Constants
A named constant is an identifier that represents a permanent value.
❑ Every variable has a scope. The scope of a variable is the part of the program where the variable can
be referenced.
The value of a variable may change during the execution of a program, but a named constant, or
simply constant, represents permanent data that never changes. In our ComputeArea program, 𝝅 is a
constant. If you use it frequently, you don’t want to keep typing 3.14159; instead, you can declare a
constant for 𝝅. Here is the syntax for declaring a constant:
count = count + 1;
❑ C++ allows you to combine assignment and addition operators using an augmented
assignment operator. For example, the preceding statement can be written as follows:
count += 8;
-= Subtraction i -= 8 i=i–8
assignment
*= Multiplication i *= 8 i=i*8
assignment
❑ When operators have the same level of precedence, the operations are performed
from left to right. To avoid confusion, you can use parentheses to group arithmetic
expressions.
Relational and Logical Operators in C++
❑In C++, relational and logical operators are used for decision-making by comparing values
and forming complex conditions.
❑ The bool data type declares a variable with the value either true or false. How do you compare two
values, such as whether a radius is greater than 0, equal to 0, or less than 0? C++ provides six relational
operators, shown in Table below, which can be used to compare two values (assume radius is 5 in the
table).
Relational and Logical Operators in C++
Logical Operators !, &&, and || can be used to create a compound Boolean expression.
❑ Sometimes, a combination of several conditions determines whether a statement is executed.
❑ You can use logical operators to combine these conditions. Logical operators, also known as Boolean
operators, operate on Boolean values to create a new Boolean value. The table below gives a list of
Boolean operators.
❑ Table 3.4 defines the not (!) operator. The not (!) operator negates true to false and false to true. Table
3.5 defines the and (&&) operator. The and (&&) of two Boolean operands is true if and only if both
operands are true. Table 3.6 defines the or (||) operator. The or (||) of two Boolean operands is true if
at least one of the operands is true.
Relational and Logical Operators in C++
Increment and Decrement Operators
The increment (++) and decrement (--) operators are for incrementing and Decrementing a
variable by 1.
❑ The ++ and -- are two shorthand operators for incrementing and decrementing a variable by 1.
These are handy, because that’s often how much the value needs to be changed in many
programming tasks. For example, the following code increments i by 1 and decrements j by 1.
int i = 3, j = 3;
i++; // i becomes 4
j--; // j becomes 2
❑ i++ is pronounced as i plus plus and i-- as i minus minus. These operators are known as
postfix increment (postincrement) and postfix decrement (postdecrement), because the
operators ++ and -- are placed after the variable
Increment and Decrement Operators
These operators can also be placed before the variable. For example,
int i = 3, j = 3;
++i; // i becomes 4
--j; // j becomes 2
❑ ++i increments i by 1 and --j decrements j by 1. These operators are known as prefix increment
(preincrement) and prefix decrement (predecrement).
Increment and Decrement Operators
❑The Table below shows Increment and Decrement Operators
Logo, company name
www.spu.ac.za
www.spu.ac.za
8300