Module 1 - Processing A C++ Languange Program PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

North Luzon Philippines State College

CS 2 - Fundamentals of Programming

North Luzon Philippines State College


San Nicolas, Candon City, Ilocos Sur

Ronald M. Gacusan, MIT


Instructor

1
North Luzon Philippines State College
CS 2 - Fundamentals of Programming

Module 1: An overview of
computers and programming
languages

1.1 Learning about deferent types of


computers

After successfully completing this lesson, you will be able to:

1. Identify and define the different types of computers


2. Identify the capabilities of the different types of computer

Computers and software can help you


perform many tasks, such as maintaining a
bank account and writing assignments or
reports. Software is developed using
programming languages. The C++
programming languages is developed
software that can accomplish specific tasks.

2
North Luzon Philippines State College
CS 2 - Fundamentals of Programming

Computers have evolved from their introduction in the 1950s, when there were few computers,
through the 1960s, when
manufacturers produced
very large, expensive
computers, to the 1970s
when people began to use
cheaper, smaller
computers. Today,
computers are even more
affordable and faster.
Regardless of the type,
mainframe, midsize and
micro (personal) computers
share some basic elements,
including input, output,
storage and performance of
arithmetic and logical
operations.
A mainframe
computer was once on of
the largest, fastest and most powerful computers available. However, to put this in perspective, a
mainframe in 1960 was less powerful than today’s personal computer. During the next decade, the
midsize computer was introduced as a less expensive, less powerful, smaller and more affordable
alternative to mainframe. The introduction of micro computers or personal computers (PC), brought
all of these efficiencies to a level that made computers a household commodity. PCs are usually sold
with descriptions of their features to help consumers (who are often not computer experts) match the
computer’s features with their needs.

Learn about different types of computers

Match the following terms with the appropriate definitions.

a) Personal computers
1. 1950s
b) When computers became affordable for non-
2. 1960s specialist
3. 1990s c) A less expensive and smaller computers than a
mainframe that allowed more companies to
4. Mainframe
affordable computers
5. Midsize computer d) When accessibility to computers was limited to
6. microcomputer very few people
e) When large companies began to use
computers
f) Until recently, one of the largest, fastest and
most powerful computers

3
North Luzon Philippines State College
CS 2 - Fundamentals of Programming

Exploring the hardware and software


1.2 components of a computer system

After successfully completing this lesson, you will be able to:

1. Identify and defined hardware components.


2. Distinguish between types of programs.
3. Recognize addressing, storage, input and output.

A computer is an electronic device made up of physical components (hardware). Major


hardware components include the following:

✓ Central processing unit (CPU), which contains the control unit (CU), arithmetic logic
unit (ALU), and various registers for special purposes such as the instruction register
(IR) and program counter (PC);
✓ Main memory (MM), also called random access memory (RAM); and
✓ Input/output devices and secondary storage

The CPU is the brain of the computer and determines the computer’s speed. The ALU, an
important

component of the CPU, carries out all arithmetic and logical operations. The accumulator, holds the
results of the operations performed by the ALU. Amin memory is directly connected to the CPU. Main
memory is an ordered sequence of cells. Each cell has a unique location in a memory, called the
address of the cell. Both data and instructions (program) are stored in a main memory. All instructions
(programs) and data must be loaded into main memory before they can be executed or performed.
Programs and data are transferred to secondary storage when they are not being executed. Programs

4
North Luzon Philippines State College
CS 2 - Fundamentals of Programming

are commands that perform input (get data), produce output (display result), provide storage, and
calculate arithmetic and logical operations. There are two types of programs, system programs and
applications programs. Application programs perform a specific task. Examples of application
programs include word processing, spreadsheets, and games.

A. Explore the software components of a computer system

Match the following terms with the appropriate definitions.

a) Computer instructions used to solve a


1. Application program
problem
2. Program b) Programs run by the computer
3. Software c) Used to monitor the overall activity of
the computer and provide services
4. System program
d) Used to perform a specific task;
examples include word processing,
spreadsheets and games

B. Explore the hardware components of a computer system

Match the following terms with the appropriate definitions.

a) An electronic device can perform commands to input,


output, or store data and can calculate arithmetic and
1. Accumulator logical expressions
b) Computer components including the central
2. Address
processing unit (CPU), main memory (MM),
3. Arithmetic logic unit input/output devices and secondary storage
4. Computer c) The brain of the computer, containing several
components such as the control unit (CU), program
5. CPU counter (PC), instruction register (IR), arithmetic logic
6. CU unit (ALU) and accumulator (ACC)
d) Controls a program’s overall executions
7. Hardware
e) Points to the next instruction to be executed
8. Input devices f) Holds the instruction that is currently being executed
9. Instruction register g) Holds the result of the operations performed by the
arithmetic logical operations
10. Output devices h) The components of the CPU that performs arithmetic
11. Program counter and logical units
i) A unique location in main memory
12. Secondary storage
j) Stores information permanently
k) Devices including the keyboard, mouse and
secondary storage
l) Device including monitor, printer and secondary
storage 5
North Luzon Philippines State College
CS 2 - Fundamentals of Programming

C. Answer the following questions about your computer/smart phone at home.

1. What is the computer’s operating system?


___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________

2. How much memory does the computer have?


___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________

3. What type of CPU does the computer have?


___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________

4. How much secondary storage does the computer have?


___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________

6
North Luzon Philippines State College
CS 2 - Fundamentals of Programming

1.3 Storing Information Electronically

After successfully completing this lesson, you will be able to:

1. Identify machine representation of computer code.

A computer is an electronic device that


processes digital signals, which represent information
with a sequence of 0s and 1s – binary code. These
digital signals called the language of a computer, or
machine language. The digit 0 or 1 is called a binary
digit, or bit. A sequence of eight bits is called byte.
All data is stored and manipulated as a binary
codes. Inside the computer, every character is
represented as a sequence of bits, each having a
unique representation. Encoding schemes need either
one or two bytes to store characters. Depending on
the scheme. Although most computers perform the same kinds of operations, the machine language
of another computer.
Machine language code, or programs written is binary code, are very difficult to write and are
error-prone. Assembly language instructions written in an easy-to-remember from called a mnemonic,
were developed to make programming easier. However, assembly language instruction must be
translated into machine language before being executed. A program called an assembler translates
the assembly language instructions into machine language.
The next step toward making programming easier was the creation of high-level language that
were closer to natural languages. To run a program written in a high-level language, the instruction
must first be translated into machine language using a program called a compiler.

Storing information electronically

Fill in the blank with the appropriate term.

1. The two types of electrical signal are ____________ and ____________.


2. ____________ signals represent information with a sequence of 0s and 1s.
3. The digits 0 and 1 are used to represent ____________.

7
North Luzon Philippines State College
CS 2 - Fundamentals of Programming

4. The digits 0 and 1 are called ____________ or the shortened term ____________.
5. The digits 0s and 1s is referred to as ____________.
6. The sequence of eight bits is referred to as ____________.
7. The most common encoding scheme on personal computer is ____________ and is
abbreviated as ____________.
8. C++ differentiates uppercase and ____________ characters.
9. A program that translate assembly language instructions into machine language is called
a ____________.
10. A program that translate instructions written in a high-level language into machine code
is called a ____________.

8
North Luzon Philippines State College
CS 2 - Fundamentals of Programming

1.4 Processing a C++ Language Program

After successfully completing this lesson, you will be able to:

1. Identify the process needed to convert a high-level language program, in this case C++ to
an executable program

The following is an example of a C++ programming:

1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 cout << “Welcome to the C++ Programming “ << endl << endl;
7 cout << “Anything in double quotes is a string.” << endl;
8 cout << “ Numbers in quotes are a string like 8 + 7 “ << endl;
9 cout << “ Numbers not in quotes like “ << 8 << “ or “
<< 7<< “ have their values printed.” << endl;
10 cout << Numbers not in quotes with arithmetic operations are “
<< “evaluated “ << 8+7 << endl;
11 return 0;
12 }

When compiled, linked, and executed, this program will display:

Welcome to the C++ Programming


Anything in double quotes is a string.
Numbers in quotes are a string like 8 + 7
Numbers not in quotes like 8 or 7 have their values printed.
Numbers not in quotes with arithmetic operations are evaluated 15

Visit the uniform resource locator (url)


https://www.w3schools.com/cpp/showcpp.asp?filename=demo_helloworld for you to code and
compile your source code above to experience the actual coding and testing. You can also code,
compile and execute using your android phone. Just go to google play store of our smart phone and
download the C++ Compiler IDE. You can use online using the url https://www.w3schools.com/cpp/
or offline using C++ Compiler IDE.

9
North Luzon Philippines State College
CS 2 - Fundamentals of Programming

The line numbers along the left margin of the code are not part of the program but are used
to identify the line; each number line refers to a statement in the code, as explained in the following:

Line1 is a statement that begins with a #. This is a preprocessor directive. There


can be multiple preprocessor directives, each of which represent pre written code in a C++ library.
This iostream library contains code needed for program input and output operations.

Line 2 using namespace std; is required in order to use cout ( the predefined object) and endl (the
manipulator); cout is used for output and endl is used for moving the insertion point to the next line.

Line 3 is empty line, often called “whitespace”

Line 4 identifies the beginning of the main function, int main (). All programs are functions in C++.

Line 5 begin with an opening brace ( { ), is the beginning of the body of the main function. All functions
begin with an opening and end with a closing ( } ).

Line 6, 7 and 8 the cout object and the insertion operator ( << ) cause the values between double
quotes, referred to as a string to be displayed exactly as written. Each string, expression, or
manipulator is preceded by the insertion operator ( << ) within the cout statement.

Line 9 an output statement that contains both a string and literal numbers ( numbers not in quotes)
as separate data items ( called expressions ). Numbers displayed as their literal value.

Line 10 is another output statement that contains a string and an arithmetic operation (expression).
The arithmetic operation is evaluated and displayed as evaluated. Lines 9 and 10 are continued the
next line but remain part of the same statement

Line 11 contains the reserved word return. When the program finishes, a return value of 0 tells the
operating system that the program has completed.

C++m is up one language level from assembly language. It is one of many high-level language
that use parts of a natural language to make it easier to understand. Six steps are necessary to
process a program written in a high-level language:

1. Use a text editor to create source code in the high level language. The text editor is contained
in the SDK. The source code must follow the rules of (syntax) of the language. Save the
source code of the program in a file that has a .cpp file extension. The next editor contained
in the SDK.
2. Begin with the preprocessor directives needed for the program. This statement is processed
by a program called the preprocessor.
3. The compiler is a program that verifies that the source code obeys the rules of the
programming language (the syntax). When the program is syntactically correct, the compiler

10
North Luzon Philippines State College
CS 2 - Fundamentals of Programming

translates the program into a machine language program called an object program. The
compiler is a part of a software development kit (SDK).
4. Another program in the SDK is called a linker, which combines the object program with
prewritten code from libraries, such as iostream, creating an executable program. This is
usually performed with the “Build” or “ Make” command in the SDK.
5. An additional program in the SDK is called the loader, which loads the executable program
into main memory.
6. Finally, execute or run programs.

Process a C++ language program

Select the correct answer to the following questions:

1. A program that combines the source code with prewritten code is


a. processor c. linker
b. loader d. compiler

2. A file that contains the program after it has been translated into machine code is
a. editor file c. object file
b. source code file d. .cpp file

3. An assembler is
a. A high-level language that uses a natural language
b. A program is the SDK that links source code with prewritten code
c. A program that translates assembly code into machine code
d. A program that place a source code file into main memory

4. The rules for correctly writing a C++ source code program are known as
a. Grammar c. Preprocessing
b. Syntax d. SDK

5. The program that places the executable program into main memory in
a. Compiler c. Preprocessor
b. Editor d. loader

11
North Luzon Philippines State College
CS 2 - Fundamentals of Programming

Answer the following questions in a short bond and complete the steps for problem solving.

1. Consider the problem of calculating a paycheck. The pay is calculated by multiplying


the number of hours worked by the rate of pay. All hours greater than 40 are calculated
at time and half. Correct any steps below that are in error.

a. Get the number of hours worked.


b. Get the rate of pay.
c. Get the amount pay.
d. Find the amount of pay at the base salary.
e. Find the number of overtime hours.
f. Find the number of hours at the regular pay.
g. Find the amount of pay at overtime rate.
h. Calculate the paycheck using the following equation:
Pay=hoursWorked * rateOfPay + overtimeHours * pay* 1.5

2. Create a program that display your 5-personal information.

References

Nugent, G. & Scholl, J. (2011) C++ Programming, problem analysis to program


design, Cengage Learning Asia Pte. Ltd. Philippines

Bronson, G. (2006). Program development and design using C++, 3rd ed. USA:
Course technology

https://www.tutorialspoint.com/cplusplus/cpp_basic_syntax.htm

https://www.computerscience.gcse.guru/topic/programming

https://edu.glogster.com/glog/the-evolution-of-the-omputer/243q4cn92aq?=
glogpedia-source

https://www.w3schools.com/cpp/

https://play.google.com/store/apps/details?id=org.zhiyuan.zhiyuan24r/243q4cn92aq?=

12

You might also like