+ All Categories
Home > Documents > Chapter 1 INTRODUCTION - Computer Science Department …©2017 Gilbert Ndjatou Page 1 Chapter 1...

Chapter 1 INTRODUCTION - Computer Science Department …©2017 Gilbert Ndjatou Page 1 Chapter 1...

Date post: 14-Sep-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
23
©2017 Gilbert Ndjatou Page 1 Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware consists of the physical components of the system. The software is the collection of programs that a computer can execute and a program is a sequence of step-by-step instructions that a computer executes in order to solve a problem or to perform a task. This chapter discusses the concepts of a computer system hardware and software, computer programs, basic elements of a high-level language, structure of a high-level language program, functions, header files and the preprocessor, library functions, and the processing of a C/C++ language program. 1.1 Computer System Hardware A computer system hardware consists of four major components: the Central Processing Unit (CPU), the Main (or Primary) Memory, the input system, and the output system. The general structure of a digital computer system hardware is shown in figure 1.1. Main Memory The main memory (also called primary memory) is a collection of two-state devices. The states of each of these devices are used to represent binary digits or bits (0 or 1): one state represents 0 and the other represents 1. Consecutive bits in the main memory are grouped into units of 8 called bytes. A byte is characterized by two things: its address and its content. The address of a byte corresponds to its relative position in the main memory: the first byte is at address 0, the second at address 1, . . . , etc. The content of a byte is the bit pattern that it represents. For example 0110 1001. The main memory holds the data to be processed by the computer and the program that it must execute in order to process it. Central Processing Unit The central processing unit (CPU) is the center of activities in a computer: it controls the operations of the other components of a computer, and also interprets and executes the instructions of a program. It consists of the Control Unit (CU), the Arithmetic and Logic Unit (ALU) and the registers. The Control Unit (CU) controls the operations of the different components of a computer, and also interprets (or decodes) each instruction of a program. The Arithmetic and Logic Unit (ALU) executes the instructions of a program. It can only perform the basic arithmetic operations of addition, subtraction, multiplication, and division, and logical operations
Transcript
Page 1: Chapter 1 INTRODUCTION - Computer Science Department …©2017 Gilbert Ndjatou Page 1 Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware

©2017 Gilbert Ndjatou Page 1

Chapter 1 INTRODUCTION

A digital computer system consists of hardware and software:

The hardware consists of the physical components of the system.

The software is the collection of programs that a computer can execute and a program is a sequence

of step-by-step instructions that a computer executes in order to solve a problem or to perform a task.

This chapter discusses the concepts of a computer system hardware and software, computer programs,

basic elements of a high-level language, structure of a high-level language program, functions, header

files and the preprocessor, library functions, and the processing of a C/C++ language program.

1.1 Computer System Hardware

A computer system hardware consists of four major components: the Central Processing Unit (CPU),

the Main (or Primary) Memory, the input system, and the output system. The general structure of a

digital computer system hardware is shown in figure 1.1.

Main Memory

The main memory (also called primary memory) is a collection of two-state devices.

The states of each of these devices are used to represent binary digits or bits (0 or 1): one state

represents 0 and the other represents 1.

Consecutive bits in the main memory are grouped into units of 8 called bytes.

A byte is characterized by two things: its address and its content.

The address of a byte corresponds to its relative position in the main memory: the first byte is at address

0, the second at address 1, . . . , etc.

The content of a byte is the bit pattern that it represents. For example 0110 1001.

The main memory holds the data to be processed by the computer and the program that it must execute

in order to process it.

Central Processing Unit

The central processing unit (CPU) is the center of activities in a computer: it controls the operations of

the other components of a computer, and also interprets and executes the instructions of a program.

It consists of the Control Unit (CU), the Arithmetic and Logic Unit (ALU) and the registers.

The Control Unit (CU) controls the operations of the different components of a computer, and also

interprets (or decodes) each instruction of a program.

The Arithmetic and Logic Unit (ALU) executes the instructions of a program. It can only perform the

basic arithmetic operations of addition, subtraction, multiplication, and division, and logical operations

Page 2: Chapter 1 INTRODUCTION - Computer Science Department …©2017 Gilbert Ndjatou Page 1 Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware

©2017 Gilbert Ndjatou Page 2

such as comparing two numbers or the Boolean operations (AND, OR, or NOT).

Registers are high-speed memory locations within the CPU that are used to hold certain data.

Figure 1.1 General Structure of a Digital Computer System Hardware

The arrows indicate the flow of data between the computer system hardware components.

Input/Output Systems

The Input system consists of the Input devices and their controllers.

Examples of input devices are keyboards, microphones, scanners, and mice.

An input device is used to transmit information from the outside world to the computer.

The output system consists of the output devices and their controllers.

Examples of output devices are printers, monitors, and speakers.

An output device is used to transmit information from the computer to the outside world.

There is a controller between the CPU and each input/output device.

For an output device, the controller accepts incoming data (as a sequence of bits) from the computer

that it converts into the signals used by that device.

CPU

ALU

CU Registers

Main Memory

Controller

Input DeviceOutput Device

Controller Controller

DriveSecondaryStorage

Page 3: Chapter 1 INTRODUCTION - Computer Science Department …©2017 Gilbert Ndjatou Page 1 Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware

©2017 Gilbert Ndjatou Page 3

For an input device, the controller accepts signals from the device that it converts into sequences of

bits to be used inside the computer.

A device controller is also used for communication with the CPU: it accepts commands from the

CPU and also lets the CPU know what is happening with the device.

Secondary Storage

A Secondary storage is used to permanently save data and programs for future use inside a computer.

Examples of secondary storages are flash drives, hard disks, tapes, CDs, and DVDs.

A secondary storage can be used on a computer only if that computer has a drive that can read and/or

write on that secondary storage.

There is also a controller between the CPU and each drive: this controller converts signals from the

secondary storage drive into bits that are sent to the main memory and vice versa.

The collection of input devices, output devices, and the drives are referred to as system peripherals.

System Interconnection

The system interconnection consists of the wiring and the devices that allow data and control codes to

move from one part of the computer system hardware to another. It is in general referred to as the

system bus.

Exercise 1.1

Part I Fill in the blanks with the most appropriate answers.

1. A _________________________ is a sequence of step-by-step instructions that a computer executes in order to

solve a problem or to perform a task.

2. A digital computer system consists of _________________________ and ______________________________.

3. The _________________________________consists of the physical components of a digital computer system.

4. The ______________________________________ is the collection of programs that a computer can execute.

5. A _____________________________________________ is a group of 8 consecutive bits.

6. The _________________________________ of a byte corresponds to its relative position in the main memory.

7. The _________________________________ of a byte is the bit pattern that it represents.

8. The ______________________________________ holds the data to be processed by the computer, and the

program that it must execute in order to process it.

9. The _________________________________________________ controls the operations of the different

components of a computer system, and interprets and executes the instructions of a program.

10. The ____________________________ controls the operations of the different components of a computer, and also

interprets (or decodes) each instruction of a program.

11. The _____________________________________________________ executes the instructions of a program.

Page 4: Chapter 1 INTRODUCTION - Computer Science Department …©2017 Gilbert Ndjatou Page 1 Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware

©2017 Gilbert Ndjatou Page 4

12. ________________________________ are special high-speed memory locations within the CPU that are used by

programmers or the CPU to hold certain data.

13. The input system consists of the ______________________________ and their _________________________.

14. The output system consists of the ____________________________ and their __________________________.

15. An output device ____________________________ accepts incoming data (as a sequence of bits) from the

computer that it converts into the signals used by that device.

16. An input device __________________________________ accepts signals from the device that it converts into

sequences of bits to be used inside the computer.

17. A __________________________________________ is used to permanently save data and programs for future

use inside the computer.

18. In order to use a particular secondary storage on a computer, that computer must be equipped with a

_______________________ for that type of secondary storage and a ___________________________ for the

communication between the computer and the drive.

19. A computer system ____________________________________ is the collection of input devices, output devices,

and the drives on that computer system.

20. The ________________________________________________________ consists of the wiring and the devices

that allow data and control codes to move from one part of the computer system hardware to another.

Part II.

A. What are the four major components of a digital computer system hardware?

B. What are the three major components of a CPU?

C. Provide three examples of each of the following components of a digital computer system hardware:

a. input devices b. output devices c. secondary storage

D. What are the major functions of each of the following components of a digital computer system hardware?

a. main memory b. input device c. output device d. CPU e. secondary storage

1.2 Computer Software

A computer software is the collection of programs that it can execute.

There are two major classes of computer software: system programs and application programs.

An application program is a program that is executed by a computer in order to perform a specific task

or to solve a particular problem for its user.

Examples of application programs are word-processors, data base management systems, spreadsheet

programs, and web browsers.

A word-processors is a program that lets its users create documents with formatting features.

A data base management system is a program that lets its users create tables of information, and

also provides for easy access and update of information in those tables.

A spreadsheet program lets its users create electronic spreadsheets.

Page 5: Chapter 1 INTRODUCTION - Computer Science Department …©2017 Gilbert Ndjatou Page 1 Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware

©2017 Gilbert Ndjatou Page 5

A web browser is used to display web pages.

A system program is a program that is used for the operation of a computer.

Examples of system programs are editors, compilers, linkers, and operating systems.

An editor reads characters from the keyboard and places them into a file.

Every compiler software package comes with an editor that you can use to type and edit your programs.

An operating system performs the following tasks:

Manages all the computer resources making sure that resources such as the CPU, the main memory, the

printer, the hard disk, . . . , etc are used properly.

Provides an interface to the hardware that is easy to understand and use in other programs.

Examples of operating systems running on IBM PC and compatible computers are MS Windows XP,

MS Windows vista, MS Windows 7, MS Windows 8, and LINUX.

UNIX is the operating system running on most minicomputers.

Some operating system software packages also include programs called shells or command interpreters

that provide the access of the computer system to users.

Exercise 1.2

Fill in the blanks with the most appropriate answers.

1. An _________________________________________________ is a program that is used to perform a

specific task or to solve a particular problem for its user.

2. A _____________________________________ is a program that is used for the operation of the computer.

3. A ______________________________________________ is a program that lets its users create documents

with formatting features. It also inserts hidden codes in a document in order to obtain certain features such as

boldfacing, underlining, italic, . . . etc.

4. A _________________________________________________ is a program that lets its users create tables of

information, and also provides for easy access and update of information in those tables.

5. A ______________________________________is a program that lets its users create electronic spreadsheets.

6. An _____________________is a program that reads characters from the keyboard and places them into a file.

7. An _________________________________________ is a program that manages all the computer resources,

making sure that resources such as the main memory, the printer, the hard disk, . . . , etc are used properly.

8. An ___________________________________________________________ is a program that provides an

interface to the hardware that is easy to understand and use in other programs.

9. A ______________________________ is a program that provides the access of the computer system to users.

Page 6: Chapter 1 INTRODUCTION - Computer Science Department …©2017 Gilbert Ndjatou Page 1 Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware

©2017 Gilbert Ndjatou Page 6

1.3 Computers and Programming

As shown in figure 1.2, the three major functions of a digital computer system are to input data, to

process it, and to output the result(s) of the computation.

The input data is represented inside the main memory as a sequence of bits.

The computer processes data by execution the step-by-step instructions of a program.

A program and its input data must be in the main memory before it can be executed by the CPU.

Figure 1.2

As shown in figure 1.3, data are represented (held) in memory locations inside the main memory.

The size of a memory location (in bytes) depends on the type of data that it holds.

For example, on IBM PC and compatible computer systems, one byte is used to hold a character, and

four consecutive bytes are used to hold positive and negative integer values.

The address of a memory location is the address of its first byte.

Figure 1.3 Examples of Memory locations

Address:

149

150

151

152

153

154

155

156

157

158

This figure illustrates three memory locations:

- the first memory location has two bytes and its address is 149

- the second has one byte and its address is 152

- the third has four bytes and its address is 155

- bytes at addresses, 151, 153 and 154 are not used.

Computer System

data Process data Result(s)

Page 7: Chapter 1 INTRODUCTION - Computer Science Department …©2017 Gilbert Ndjatou Page 1 Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware

©2017 Gilbert Ndjatou Page 7

A computer can execute a program only if it is written as sequences of bits in a language called machine

language.

An example of a machine language instruction for the IBM PC and compatible computers follows:

1000 0011 1100 0011 0000 0101

Each type of computer has its own set of machine language instructions called its machine language.

For example, a program written for the IBM PC cannot be executed on Apple Macintosh computers

because they have different machine languages.

Most programmers write programs in languages usually referred to as high-level languages.

It is easier to write programs and correct mistakes (debug) in a high-level language than in a machine

language.

Examples of high-level languages are FORTRAN, JavaScript, PHP, BASIC, C++, Ada, and Java.

A high-level language program must first be translated into the machine language of a computer before it

can be executed by that computer.

The compiler is a system program that translates a high-level language program into a program in the

machine language of a particular type of computer.

There is a compiler for each high-level programming language and for each type of computer: for

example, a C++ compiler for the IBM PC and compatible computers; a C++ compiler for the SUN

microsystem computer; a FORTRAN compiler for the Apple Macintosh computers.

Some high-level programming languages such as PHP, Java, and JavaScript have a system program

called interpreter that takes a program in that language and its input data as input, executes the program

on a computer, and produces the output of that program.

The functions of a compiler and those of an interpreter are illustrated in figure 1.4.

Figure 1.4

(a) Functions of a Compiler

Program Program

( in high-level language) Compiler (in machine language)

(b) Functions of an Interpreter

Program

( in high-level language) Output

(of the program)

Input Data Interpreter

(of the program)

Page 8: Chapter 1 INTRODUCTION - Computer Science Department …©2017 Gilbert Ndjatou Page 1 Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware

©2017 Gilbert Ndjatou Page 8

Exercise 1.3

1. What are the three major functions of a digital computer system?

2. Where is the input data represented inside the main memory?

3. How are data represented inside a computer?

4. What is the size of the memory location used to hold each of the following types of data inside an IBM PC?

a. a character. b a positive or negative integer value.

5. What is the address of a memory location?

6. A program is executed by a computer only if it is written in what language?

7. How is a machine language instruction of a computer specified?

8. Can a program in the machine language of the IBM PC be executed by the SUN Microsystem computer? Why?

9. Provide three examples of high-level programming languages.

10. You have written a program in C++. What do you have to do in order to execute it on the IBM PC?

11. You have a C++ compiler for the IBM PC. Can you use this compiler on the SUN Microsystem computer? Why?

12. What is the difference between a compiler and an interpreter?

13. Name a programming language that has an interpreter?

1.4 High-Level Language Programs

When you write a program in a high level language, you do not know where the data will be held in

memory. For this reason, you use a variable to represent a memory location in a high level language

program.

Also, the only things that you can write in a C++ program are: comments, statements and preprocessor

directives.

An example of a C++ program is shown in figure 1.5.

Figure 1.5 An Example of C++ Program

Line # Statements /Comments

1 /*----------------- A Sample Program ------------------ */

2 /* read a character and an integer value from the keyboard,

3 compute the double of the value, and print the results */

4 /* Prepared by: <enter your name here> */

5

6 #include <iostream>

7 using namespace std;

8

9 int main()

10 {

11 int num; // to hold an integer value

12 int result; // to hold the result of the computation

13 char letter; // to hold a character

14

15 cout << endl << “Hello World”;

16

Page 9: Chapter 1 INTRODUCTION - Computer Science Department …©2017 Gilbert Ndjatou Page 1 Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware

©2017 Gilbert Ndjatou Page 9

17 /*-------- read a character and an integer value ------*/

18 cout << endl << “Enter a character please: “;

19 cin >> letter;

20 cout << endl << “Enter an integer value: “;

21 cin >> num;

22

23 /* compute the double of the value and print the results */

24 result = 2 * num;

25 cout << endl << “The character is: “

26 << letter;

27 cout << endl << “The double of the value is: “;

28 cout << result;

29 cout << endl << “Bye bye”;

30 return (0);

31 }

Comments

A comment is a note or a remark for a human being reading the program.

It is ignored by the compiler and may appear anywhere in a program.

You can write comments in two different ways:

1. Any sequence of character between the characters /* and */

Example: /*----------------- A Sample Program ------------------ */

2. Any sequence of characters that follows the characters // and that ends at the end of the line.

Example: // to hold an integer value

A comment is in general used to accomplish the following things:

1. To specify the object of a program. Examples: lines 2 to 3 of the program in figure 1.5.

2. To specify a computation step of a program. Examples: lines 17 and 23 of the program in figure 1.5.

3. To clarify the meaning of a sequence of one or more statements. Examples: lines 11, 12, and 13 of

the program in figure 1.5.

Statements

You can write two types of statement in a C++ program:

1. Statements to instruct or provide information about the program to the compiler. These statements

are not translated into machine language. For example, statements in lines 11, 12, and 13 of the

program in figure 1.5.

2. Statements that are translated into machine language instructions. These statements are usually

referred to as executable statements.

Every C++ statement ends with a semicolon.

Page 10: Chapter 1 INTRODUCTION - Computer Science Department …©2017 Gilbert Ndjatou Page 1 Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware

©2017 Gilbert Ndjatou Page 10

A statement may be written over more than one line. For example the statement in lines 25 and 26 of the

program in figure 1.5.

You may also leave one or more blank lines between statements and comments.

The C++ programming language has the following basic statements:

1. Declaration statement.

2. Input statement.

3. Output statement.

4. Assignment statement.

Declaration Statement

In a C++ program, you have to make a request for a memory location (variable) to the compiler before

you can use it to hold data.

You make a request for a memory location (variable) by using a declaration statement.

Examples of declaration statements in the program in figure 1.5:

Line 11 int num;

Line 12 int result;

Line 13 char letter;

In a declaration statement, you specify the data type of a variable followed by its name.

The data type of a variable tells the compiler the type of data that will be stored in the corresponding

memory location.

The compiler uses the data type of a variable to figure out how many bytes to reserve for the

corresponding memory location.

The following are the basic data types in C++:

int for integer values

char for a single character value

float for a single precision floating point value

double for a double precision floating point value

bool for true or false

You can also specify the initial value of a memory location in a declaration statement as in the following

example:

int num = 15;

15 will be stored in the corresponding memory location.

Page 11: Chapter 1 INTRODUCTION - Computer Science Department …©2017 Gilbert Ndjatou Page 1 Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware

©2017 Gilbert Ndjatou Page 11

Input Statement

You use an input statement to read (or input) data into a program.

An input statement to read data from the keyboard starts with the keyword cin which is followed by

the names of one or more variables preceded by the stream extraction operator ( >> ). The following

input statements are from the program in figure 1.5:

line 19 cin >> letter;

Line 21 cin >> num;

After the execution of a cin statement, the computer stops and waits for you to type an input value for

each variable listed in the statement.

Type each value followed by the Enter key. The value that you type for a variable is stored in the

corresponding memory location. For example, if you type:

K Enter

25 Enter

Then K will be stored in the memory location letter and 25 in the memory location num.

Two or more variable names can also be specified in a cin statement as in the following example:

cin >> letter >> num;

When two or more variables are specified in a cin statement, you must type a value for each of these

variables: you can press the Enter key after each value or after you have typed all the values separated

with one of more spaces as follows:

K 25 Enter

Assignment statement

You use an assignment statement to store a value into a memory location.

It starts with the name of the variable followed by the assignment operator ( = ), which is then followed

by the value that you want to store in that memory location.

Example

Suppose that we have three variables letter, num, and result declared in a program as follows:

char letter;

int num;

int result;

letter = 'a'; says to store the character a in the memory location letter.

num = 25; says to store 25 in the memory location num.

result = 2 * num; says to take the value in memory location num (25), multiply it by 2

and to store the result in the memory location result.

Page 12: Chapter 1 INTRODUCTION - Computer Science Department …©2017 Gilbert Ndjatou Page 1 Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware

©2017 Gilbert Ndjatou Page 12

Output statement

You use a cout statement to display (or print) information on the screen.

It starts with the keyword cout followed by one or more data to be displayed, or by a stream

manipulator.

Each data to be displayed or a stream manipulator is preceded by the stream insertion operator ( << ).

The string manipulator endl causes the cursor to move to the beginning of the next line before the next

data is printed.

Example

Suppose that we have three variables letter, num, and result declared in a program as follows:

char letter;

int num;

int result;

and that we have the following assignment statements:

letter = 'a'; says to store the character a in the memory location letter.

num = 25; says to store 25 in the memory location num.

result = 2 * num; says to take the value in memory location num (25), multiply it by 2

and to store the result in the memory location result.

Each of the following output statements will produce the specified output.

Statement Output

cout << endl << “Hello Word”; Hello Word

cout << endl << “The character is: “ << letter; The character is: a

cout << endl << “The double of the value is:”;

cout << result; The double of the value is:50

cout << endl << 100 << letter << num; 100a25

cout << endl << “letter = “ << letter; letter = a

cout << endl << “result = “ << result; result = 50

NOTES

1. The next information to be displayed is printed starting at the current cursor position.

2. Any sequence of characters between double quotes is displayed without the double quotes.

3. Any variable name that appears in an output statement is replaced by the current value of the

corresponding memory location.

Page 13: Chapter 1 INTRODUCTION - Computer Science Department …©2017 Gilbert Ndjatou Page 1 Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware

©2017 Gilbert Ndjatou Page 13

Exercise 1.4

1. The following program reads two integer values, computes their sum, and prints the result. Insert comments in this

program in order to accomplish the following:

a) specify the object of the program..

b) Explain the use of variables.

c) specify the steps of the computation.

Line # Statements /Comments

1 #include <iostream>

2 using namespace std;

3 int main()

4 {

5 int num1;

6 int num2;

7 int result;

8 cout << endl << “Enter two integer values, please: “;

9 cin >> num1;

10 cin >> num2;

11 result = num1 + num2;

12 cout << endl << “their sum is: “ << result;

13 return 0;

14 }

2. Explain what will happen after each of the following statements is processed by the compiler:

a. char grade;

b. int idnum;

3. Write the statements to accomplish the following:

a. Declare variable number to hold an integer value.

b. Declare variable letter to hold a character value

c. Read an integer value into variable number and a character value into variable letter.

d. Output the string “Nice day”, the value in variable letter and 5 times the value in variable number.

4. Assuming that all variables have been previously defined and that the statements are executed in the given order,

show the output produced by the following statements if the input value is 9:

cout << endl << “Good job”;

cin >> num;

sum = num + 5;

cout << endl << “num = “ << num << “ sum=” << sum;

num = 7;

sum = num + 5;

cout << endl << “num = “ << num << “ sum=” << sum;

Page 14: Chapter 1 INTRODUCTION - Computer Science Department …©2017 Gilbert Ndjatou Page 1 Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware

©2017 Gilbert Ndjatou Page 14

1.5 Structure of a High-Level Language Program

A high-level language program consists of one or more files called source files or source modules.

the filename extension of a source file depends on the programming language. The following are

filename extensions of source files in some programming languages:

Programming Language Filename Extension

C .c

C++ .cpp

Java .java

BASIC .bas

COBOL .cob

Examples of valid filenames of C++ source files are lab1.cpp, program.cpp, and payroll.cpp.

C++ Program Source Module

A C++ program source module consists of one or more functions.

A function has a name and consists of a function header followed by a sequence of statements

enclosed between the left brace { and the right brace }. An example of function is shown in figure 1.6.

The function header of this function is: void test( ).

Its name is test.

Every C++ program must have a unique function named main.

The execution of a program starts with the first executable statement in function main.

Statements in any other function are executed only if that function is called or invoked by function main

or a function that is called by function main.

After the execution of the last statement in a function that is called, the next statement to be executed is

the statement that follows the statement in which that function is called.

In the source module of figure 1.7, the order of execution of the executable statements is: 14, 15, 16, 17,

4, 5, 6, 7, 8, 9, 10, 11, 18, 19, and 20.

Statement 20 is a return statement. Its execution terminates the execution of the program.

Page 15: Chapter 1 INTRODUCTION - Computer Science Department …©2017 Gilbert Ndjatou Page 1 Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware

©2017 Gilbert Ndjatou Page 15

Figure 1.6 An Example of Function Definition

/* function to read a character and an integer value, to compute the double of

the value, and to print it with the character read. */

void test()

{

Char letter; // to hold the character

int num, // to hold the integer value

result; // to hold the double of the value

/*-------- read a character and an integer value ------*/

cout << endl << “Enter a character please: “;

cin >> letter;

cout << endl << “Enter an integer value: “;

cin >> num;

/* compute the double of the value and print the results */

result = 2 * num;

cout << endl << “The character is: “ << letter;

cout << endl << “The double of the value is: “ << result;

cout << endl << “Bye bye”;

}

Figure 1.7 An Example of Function Definitions and Function Call

#include <iostream>

using namespace std;

/* function to read a character and an integer value, to compute the double of the

value, and to print it with the character read. */

void test()

{

1 Char letter; // to hold the character

2 int num; // to hold the integer value

3 int result; // to hold the double of the value

/*-------- read a character and an integer value ------*/

4 cout << endl << “Enter a character please: “;

5 cin >> letter;

6 cout << endl << “Enter an integer value: “;

7 cin >> num;

/*compute the double of the value and print the results */

8 result = 2 * num;

9 cout << endl << “The character is: “ << letter;

10 cout << endl << “The double of the value is: “ << result;

11 cout << endl << “Bye bye”;

}

Page 16: Chapter 1 INTRODUCTION - Computer Science Department …©2017 Gilbert Ndjatou Page 1 Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware

©2017 Gilbert Ndjatou Page 16

/*------------------ function main ---------------------- */

int main()

{

12 int num1; // the first number

13 int num2; // the second number

/*-----------read two integer values ----------------*/

14 cout << endl << “Enter two integer values: “;

15 cin >> num1 >> num2;

/*----read a character and an integer value and compute its double*/

16 cout << endl << “I am about to enter function test( )”;

17 test();

18 cout << endl << “I am back in function main”;

/*------ compute the sum of the two values and print it */

19 cout << endl << “The sum of the two values is: “ << (num1 + num2);

20 return (0);

}

Functions That Return a Value

A function returns a value by using the return statement in which the value that is returned is specified.

An example of a function that returns a value is defined and called in the program in figure 1.8.

A function that returns a value must be called in such a way that the value that it returns is used in

function main.

For example, if function tester2 defined in figure 1.8 is called as follows, the value returned will be lost:

tester2( );

Figure 1.8 Calling a Function that Returns a Value

Given the following definitions of functions tester2 and main:

#include <iostream>

using namespace std;

int tester2( )

{

return (5);

}

int main( )

{

int num;

int result1;

int result2;

num = 10;

result1 = tester2( );

result2 = num + tester2( );

cout << endl << “result1 =” << result1

<< endl << “result2 =” << result2;

return ( 0 );

}

Page 17: Chapter 1 INTRODUCTION - Computer Science Department …©2017 Gilbert Ndjatou Page 1 Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware

©2017 Gilbert Ndjatou Page 17

The output of this program is:

result1 = 5

result2 = 15

Exercise 1.5

A. List the numbers of the statements of the following program in the order in which they will be executed by the

CPU.

#include <iostream>

using namespace std;

void newtest()

{

1 int num1; // to hold the first integer value

2 int num2; // to hold the second integer value

3 int sum; // to hold their sum

4 cin >> num1;

5 num2 = 25;

6 sum = num1 + num2;

7 cout << endl << “sum = “ << sum;

}

int main()

{

8 int number1; // the first number

9 int number2; // the second number

10 number1 = 14;

11 number2 = number1 + 50;

12 12 newtest(); // call function newtest()

13 13 cout << endl << “number1 = “ << number1;

14 14 cout << endl << “number2 = “ << number2;

15 return (0);

}

B. What is the output of the following program?

#include <iostream>

using namespace std;

int newtester2( )

{

return (7);

}

int main( )

{

int result1;

int result2;

result1 = newtester2( );

result2 = newtester2( ) + 9;

cout << endl << “result1 =” << result1

<< endl << “result2 =” << result2;

return ( 0 );

}

Page 18: Chapter 1 INTRODUCTION - Computer Science Department …©2017 Gilbert Ndjatou Page 1 Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware

©2017 Gilbert Ndjatou Page 18

Header Files and the Preprocessor

In general, C++ program source files contain identical parts that are placed in files called header files.

Instead of typing these parts in every source module, the contents of these files can be copied and pasted

in each source module in which they are needed.

However, instead of doing this copying and pasting yourself, you can instruct the preprocessor to do it

for you by using the preprocessor directive, #include.

It has the following syntax:

#include <filename> // for header files that come with the compiler

#include “file-specification” // for your own header files

Example: #include <iostream>

The preprocessor is a program that comes with the C++ compiler software package. It first inserts the

content of header files into a program source file before that source file is translated into machine

language by the compiler.

An example of the processing of the #include directive is shown in figure 1.9.

Figure 1.9 Processing the include Directive

Suppose that the content of the file myheader.h is as follows:

/*------------------------ myheader.h------------------------------------*/

int gnum = 10;

int tester ( ); // a new function

char letter = ‘A’;

And that the content of the file myprog.cpp is as follows:

/*-----------------------myprog.cpp---------------------------------------*/

#include “myheader.h”

int main( )

{

int num = 15;

int result;

result = num + tester( );

cout << endl << “The result is: “ << result;

return ( 0 );

}

The contents of the file myprog.cpp after it has been processed by the preprocessor is as follows:

Page 19: Chapter 1 INTRODUCTION - Computer Science Department …©2017 Gilbert Ndjatou Page 1 Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware

©2017 Gilbert Ndjatou Page 19

/*-----------------------myprog.cpp---------------------------------------*/

/*------------------------ myheader.h------------------------------------*/

int gnum = 10;

int tester ( ); // a new function

char letter = ‘A’;

int main( )

{

int num = 15;

int result;

result = num + tester( );

cout << endl << “The result is: “ << result;

return ( 0 );

}

Exercise 1.6

Suppose that the content of the header file test.h is as follows:

/* ---------------------------------- my header file -------------------------- */

int flag = 0;

void tfun( int ); // to perform some tasks

int process( int );

/* That’s all */

And that the content of the file myprog.cpp is as follows:

/* ----------------------------- my program file -------------------------------*/

#include <test.h>

int main()

{

int number, sum;

number = 16;

sum = number - process( number );

tfun( number );

return ( 0 );

}

Show the contents of the file myprog.cpp after it has been processed by the preprocessor.

Page 20: Chapter 1 INTRODUCTION - Computer Science Department …©2017 Gilbert Ndjatou Page 1 Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware

©2017 Gilbert Ndjatou Page 20

1.6 Library Functions

A C++ compiler comes with a collection of functions that are already translated into machine language.

These functions are referred to as library functions and the files in which they are located are called

library files

A library function may be called in any other function of a program. But it must first be declared before

the statement in which it is called.

You declare a function by using a declaration statement.

Library functions have their declaration statements written into header files that come with the compiler

software package. For example, iostream.

An example of library function call is illustrated in figure 1.10.

Figure 1.10 Calling a Library Function

Given the following program:

#include <iostream> // in order to use cin and cout

#include <cstdlib.h> // in order to call library function abs

using namespace std;

int main()

{

int num; /* to hold an integer value */

int result; /* to hold its absolute value */

cout << endl << “Enter an integer value please: “;

cin >> num;

result = abs( num );

cout << endl << “Its absolute value is: “ << result;

return (0);

}

The following outputs are produced for the input values 5, and -8, respectively:

A. Enter an integer value please: 5

Its absolute value is: 5

B. Enter and integer value please -8

Its absolute value is: 8

Page 21: Chapter 1 INTRODUCTION - Computer Science Department …©2017 Gilbert Ndjatou Page 1 Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware

©2017 Gilbert Ndjatou Page 21

Exercise 1.7

1. Provide the output of the following program:

#include <iostream>

#include <cstdlib.h>

using namespace std;

int main()

{

int num; /* to hold an integer value */

int result; /* to hold the result */

num = -10;

result = abs( num ) + 15;

cout << endl << “The result is: “ << result;

num = 7;

result = abs( num ) + 9;

cout << endl << “The result is: “ << result;

return (0);

}

2. Find all invalid statements/comments in the following C++ program and also indicate why they are invalid.

Line # Statements/Comments

1 // This is a sample program

2 // It reads integer values, performs some computations

3 and prints the results.

4 #include <iostream>

5 using namespace std;

6 int main( )

7 {

8 int num1;

9 int num2 = 3;

10 int result1;

11 int result2;

12 cin >> num1;

13 result1 = num1 + 5; /* compute the sum of the value read and 5 */

14 result2 = num2 + 5;

15 cout << endl << “result1 =”

16 << result1;

17 num1 = num1 + 10;

18 num2 + 7 = num1;

19 cout << num1;

20 << result1;

21 cout << 15;

22 cin >> num2 + 3;

23 result3 = num1 + 25;

24 return (0);

25 }

Page 22: Chapter 1 INTRODUCTION - Computer Science Department …©2017 Gilbert Ndjatou Page 1 Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware

©2017 Gilbert Ndjatou Page 22

1.7 Processing a C/C++ Language Program

The table in figure 1.11 lists the processing steps of a C++ language program.

Figure 1.11 Processing Steps of a C++ Language Program

Step

Action

System Program

Used

File Produced

1

type your program

editor

source files

2

translate source files into

machine language

compiler

object files

3

combine object files and library

functions called in the program

linker

executable file

4

load the executable file in

memory and set it up for

execution

loader

none

Exercise 1.8

1. Type the program in figure 1.5 into the source file figure-15.cpp and then do the following:

- compile and execute your program.

2. Type the program in figure 1.7 into the source file figure-17.cpp and then do the following:

- Enter the comment /* Prepared by followed by your name */ at the top of the source file.

- compile and execute your program.

3. Type the program in figure 1.8 into the source file figure-18.cpp and then do the following:

- Enter the comment /* Prepared by followed by your name */ at the top of the source file.

- compile and execute your program.

4. Type the program in figure 1.10 into the source file figure-110.cpp and then do the following:

- Enter the comment /* Prepared by followed by your name */ at the top of the source file.

- compile and execute your program.

Page 23: Chapter 1 INTRODUCTION - Computer Science Department …©2017 Gilbert Ndjatou Page 1 Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware

©2017 Gilbert Ndjatou Page 23

Exercise 1.9

Fill in the blanks with the most appropriate answers.

1. A ______________________________________ is a portion of the main memory that is used to represent (hold)

data to be processed in a program.

2. A _______________________________________________ is a system program that translates a high-level

language program into machine language.

3. A ____________________________________ file (or module) is created by the compiler to hold the machine

language translation of a source file.

4. An________________________________ receives as input the statements of a program and its input data,

executes the statements of that program using its input data, and produces its output.

5. A memory location is represented by a _______________________ in a high-level language program

6. The ________________________________ of a variable tells the compiler the type of data that will be stored in

the corresponding memory location, so that it knows how many bytes to reserve for that memory location.

7. In a high-level language program, a ___________________________ is a note or a remark for a human being

reading the program, and is ignored by the compiler.

8. A high-level language program consists of one or more files called __________________________________.

9. A __________________________ has a name and consists of a sequence of one or more statements used to

perform a specific task.

10. A ___________________________________________________________ is a function that comes with the

compiler, and that can be called in any program.

11. The ____________________________________________ is a program that first processes and completes a

program source file before it is translated into machine language by the compiler.

12. The __________________________________ combines all the object modules of a program and the machine

language translations of library functions called in that program into a file called executable file.


Recommended