Algorithms week 1
Algorithms week 1
Objectives
Ninth Edition
• Learn about exciting recent developments in computing.
• Learn computer hardware, software and Internet basics.
Chapter 1 • Understand the data hierarchy from bits to databases.
Introduction to Computers • Understand the different types of programming
and C languages.
• Understand the strengths of C and other leading
programming languages.
• Be introduced to the C standard library of reusable
functions that help you avoid “reinventing the wheel.”
Copyright © 2022 Pearson Education, Inc. All Rights Reserved Copyright © 2022 Pearson Education, Inc. All Rights Reserved
Outline (1 of 2) Outline (2 of 2)
1.1 Introduction 1.8 Other Popular Programming Languages
1.2 Hardware and Software 1.9 Typical C Program-Development Environment
1.2.1 Moore’s Law 1.9.1 Phase 1: Creating a Program
1.2.2 Computer Organization 1.9.2 Phases 2 and 3: Preprocessing and Compiling a C
1.3 Data Hierarchy Program
1.4 Machine Languages, Assembly Languages and High-Level 1.9.3 Phase 4: Linking
Languages 1.9.4 Phase 5: Loading
1.5 Operating Systems 1.9.5 Phase 6: Execution
1.9.6 Problems That May Occur at Execution Time
1.6 The C Programming Language
1.9.7 Standard Input, Standard Output and Standard Error
1.7 The C Standard Library and Open-Source Libraries Streams
Copyright © 2022 Pearson Education, Inc. All Rights Reserved Copyright © 2022 Pearson Education, Inc. All Rights Reserved
1
1.1 Introduction (1 of 4) 1.1 Introduction (2 of 4)
• C is one of the world’s most senior computer programming languages and, • Introduce terminology and concepts that lay the
according to the Tiobe Index, the world’s most popular
groundwork for the C programming you’ll learn, beginning
• You’re probably familiar with many of the powerful tasks computers in Chapter 2
perform—in this textbook, you’ll get intensive, hands-on experience writing C
instructions that command computers to perform those and other tasks • Introduce hardware and software concepts
• Software (that is, the C instructions you write, which are also called code)
controls hardware (that is, computers and related devices) • Overview the data hierarchy
• C is widely used in industry for a wide range of tasks
– From individual bits (ones and zeros) to databases,
which store the massive amounts of data that
• Today’s popular desktop operating systems—Windows, macOS and Linux—
are partially written in C
organizations need to implement contemporary
applications such as Google Search, Netflix, Twitter,
• Many popular applications are partially written in C, including popular web
browsers (e.g., Google Chrome and Mozilla Firefox), database management
Waze, Uber, Airbnb and a myriad of others
systems (e.g., Microsoft SQL Server, Oracle and MySQL) and more
Copyright © 2022 Pearson Education, Inc. All Rights Reserved Copyright © 2022 Pearson Education, Inc. All Rights Reserved
• We’ll discuss the types of programming languages • Many development environments are available in which
you can compile, build and run C applications
• We’ll introduce the C standard library and various C-
based “open-source” libraries that help you avoid • In the past, most computer applications ran on
“reinventing the wheel” “standalone” computers (that is, not networked together)
• We’ll introduce additional software technologies that • Today’s applications can communicate among the
you’re likely to use as you develop software in your world’s computers via the Internet
career
Copyright © 2022 Pearson Education, Inc. All Rights Reserved Copyright © 2022 Pearson Education, Inc. All Rights Reserved
2
1.2 Hardware and Software (1 of 2) 1.2 Hardware and Software (2 of 2)
• Computers can perform calculations and make logical decisions • Computers process data under the control of sequences of instructions
phenomenally faster than human beings can called computer programs (or simply programs)
• Today’s personal computers and smartphones can perform billions of • Specified by people called computer programmers
calculations in one second—more than a human can perform in a • A computer consists of various physical devices referred to as hardware
lifetime – keyboard, screen, mouse, solid-state disks, hard disks, memory, DVD
• Supercomputers already perform thousands of trillions (quadrillions) drives and processing units
of instructions per second! • Computing costs are dropping dramatically due to rapid developments in
– As of December 2020, Fujitsu’s Fugaku is the world’s fastest hardware and software technologies
supercomputer—it can perform 442 quadrillion calculations per – Computers that might have filled large rooms and cost millions of dollars
second (442 petaflops)! decades ago are now inscribed on silicon computer chips smaller than a
fingernail, costing perhaps a few dollars each
– To put that in perspective, this supercomputer can perform in
one second almost 58 million calculations for every person on – Silicon-chip technology has made computing so economical that
computers and computerized devices have become commodities.
the planet! And supercomputing upper limits are growing quickly.
Copyright © 2022 Pearson Education, Inc. All Rights Reserved Copyright © 2022 Pearson Education, Inc. All Rights Reserved
3
1.2.2 Computer Organization (1 of 5) 1.2.2 Computer Organization (2 of 5)
• Regardless of physical differences, computers can be • Memory Unit—This rapid-access, relatively low-capacity “warehouse”
envisioned as divided into various logical units or section retains information entered through the input unit, making it
immediately available for processing when needed.
sections
– The memory unit also retains processed information until it can
• Input Unit—This “receiving” section obtains information be placed on output devices by the output unit
(data and computer programs) from input devices and – Information in the memory unit is volatile—it’s typically lost when
places it at the other units’ disposal for processing the computer’s power is turned off
– The memory unit is often called either memory, primary memory
• Output Unit—This “shipping” section takes information or RAM (Random Access Memory). Main memories on desktop
the computer has processed and places it on various and notebook computers contain as much as 128 GB of RAM,
output devices to make it available outside the computer though 8 to 16 GB is most common. GB stands for gigabytes; a
gigabyte is approximately one billion bytes. A byte is eight bits. A
bit (short for “binary digit”) is either a 0 or a 1.
Copyright © 2022 Pearson Education, Inc. All Rights Reserved Copyright © 2022 Pearson Education, Inc. All Rights Reserved
Copyright © 2022 Pearson Education, Inc. All Rights Reserved Copyright © 2022 Pearson Education, Inc. All Rights Reserved
4
1.2.2 Computer Organization (5 of 5) 1.3 Data Hierarchy
• Secondary Storage Unit—This is the long-term, high-capacity • Data items processed by
“warehousing” section
computers form a data
– Programs and data not actively being used by the other units are
placed on secondary storage devices until they’re again needed, hierarchy that becomes
possibly hours, days, months or even years later larger and more complex
– Information on secondary storage devices is persistent in structure as we
– Secondary storage information takes much longer to access than progress from the
information in primary memory, but its cost per byte is much less
simplest data items
– Examples of secondary storage devices include solid-state drives
(SSDs), USB flash drives, hard drives and read/write Blu-ray drives
(called “bits”) to richer
– Many current drives hold terabytes (TB) of data
ones, such as characters
– A terabyte is approximately one trillion bytes and fields
Copyright © 2022 Pearson Education, Inc. All Rights Reserved Copyright © 2022 Pearson Education, Inc. All Rights Reserved
Copyright © 2022 Pearson Education, Inc. All Rights Reserved Copyright © 2022 Pearson Education, Inc. All Rights Reserved
5
1.3 Data Hierarchy—Fields 1.3 Data Hierarchy—Records
• Just as characters are composed of bits, fields are • Several related fields can be used to compose a record
composed of characters or bytes • An employee record might consist of
• A field is a group of characters or bytes that conveys – Employee identification number (a whole number).
meaning – Name (a group of characters).
• A field consisting of uppercase and lowercase letters – Address (a group of characters).
could represent a person’s name – Hourly pay rate (a number with a decimal point).
– Year-to-date earnings (a number with a decimal point).
• A field consisting of decimal digits could represent a
– Amount of taxes withheld (a number with a decimal point).
person’s age in years
• A record is a group of related fields
Copyright © 2022 Pearson Education, Inc. All Rights Reserved Copyright © 2022 Pearson Education, Inc. All Rights Reserved
6
1.3 Data Hierarchy—Big Data (1 of 2) 1.3 Data Hierarchy—Big Data (2 of 2)
• The amount of data being produced worldwide is
enormous, and its growth is accelerating Unit Bytes Which is approximately
1 kilobyte (KB) 1024 bytes 103 bytes (1024 bytes exactly)
• Big data applications deal with massive amounts of data
10 c ubed
• This field is growing quickly, creating lots of opportunities 1 gigabyte (GB) 1024 megabytes 109 (1,000,000,000) bytes
1 0 to th e ni n th po we r
for software developers 1 terabyte (TB) 1024 gigabytes 1012 (1,000,000,000,000) bytes
1 0 to th e twe l fth p owe r
Copyright © 2022 Pearson Education, Inc. All Rights Reserved Copyright © 2022 Pearson Education, Inc. All Rights Reserved
Copyright © 2022 Pearson Education, Inc. All Rights Reserved Copyright © 2022 Pearson Education, Inc. All Rights Reserved
7
1.4 Machine Languages, Assembly Languages and
1.4 Machine Languages, Assembly Languages and High-Level Languages—Assembly Languages and
High-Level Languages—Machine Languages Assemblers
• Any computer can directly understand only its own • Programming in machine language was simply too slow
machine language, defined by its hardware design and tedious for most programmers
• Machine languages generally consist of strings of • Instead of using the strings of numbers that computers
numbers (ultimately reduced to 1s and 0s) that instruct could directly understand, programmers began using
computers to perform their most elementary operations English-like abbreviations to represent elementary
one at a time operations
• Machine languages are machine-dependent—a particular • These abbreviations formed the basis of assembly
machine language can be used on only one type of languages
computer
• Translator programs called assemblers were developed
• Such languages are cumbersome for humans to convert assembly-language programs to machine
language at computer speeds
Copyright © 2022 Pearson Education, Inc. All Rights Reserved Copyright © 2022 Pearson Education, Inc. All Rights Reserved
• To speed the programming process, high-level languages were • Compiling a large high-level language program into
developed in which single statements could accomplish substantial machine language can take considerable computer time
tasks
• A typical high-level-language program contains many statements,
• Interpreters execute high-level language programs
known as the program’s source code directly
• Translator programs called compilers convert high-level-language • Interpreters avoid compilation delays, but your code runs
source code into machine language slower than compiled programs
• High-level languages allow you to write instructions that look almost • Some programming languages, such as Java and
like everyday English and contain common mathematical notations
Python, use a clever mixture of compilation and
• C is among the world’s most widely used high-level programming interpretation to run programs
languages.
Copyright © 2022 Pearson Education, Inc. All Rights Reserved Copyright © 2022 Pearson Education, Inc. All Rights Reserved
8
1.5 Operating Systems 1.6 The C Programming Language
• Operating systems are software that make using computers more • The C Programming Language
convenient for users, software developers and system administrators – C evolved from two earlier languages, BCPL and B
▪ BCPL was developed in 1967 by Martin Richards as a language for
• Provide services that allow applications to execute safely, efficiently
writing operating systems and compilers
and concurrently with one another.
▪ Ken Thompson modeled many features in his B language after
• The software that contains the core operating-system components is their counterparts in BCPL, and in 1970 he used B to create early
called the kernel versions of the UNIX operating system at Bell Laboratories.
– The C language was evolved from B by Dennis Ritchie at Bell
• Linux, Windows and macOS are popular desktop computer operating Laboratories and was originally implemented in 1972
systems – C initially became widely known as the development language of UNIX
– Each is partially written in C – Many of today’s leading operating systems are written in C and/or C++
• The most popular mobile operating systems used in smartphones – C is mostly hardware-independent—with careful design, it’s possible to
and tablets are Google’s Android and Apple’s iOS write C programs that are portable to most computers.
Copyright © 2022 Pearson Education, Inc. All Rights Reserved Copyright © 2022 Pearson Education, Inc. All Rights Reserved
• C is widely used to develop systems that demand • C’s rapid expansion to various hardware platforms (that is, types of
performance computer hardware) led to many similar but often incompatible C
versions
– operating systems
• This was a serious problem for programmers who needed to develop
– embedded systems code for several platforms
– real-time systems
• In 1983, the American National Standards Committee on Computers
– communications systems: and Information Processing (X3) created the X3J11 technical
committee to “provide an unambiguous and machine-independent
• By the late 1970s, C had evolved into what’s now definition of the language.”
referred to as “traditional C.”
• In 1989, the standard was approved in the United States through the
American National Standards Institute (ANSI), then worldwide
through the International Standards Organization (ISO)
Copyright © 2022 Pearson Education, Inc. All Rights Reserved Copyright © 2022 Pearson Education, Inc. All Rights Reserved
9
1.6 The C Programming Language—The 1.7 The C Standard Library and Open-
C11 and C18 Standards Source Libraries (1 of 2)
• We discuss the latest C standard (referred to as C11), • C programs consist of pieces called functions
which was approved in 2011 and updated with bug fixes • You can program all the functions you need to form a C
in 2018 (referred to as C18) program
• C11 refined and expanded C’s capabilities • However, most C programmers take advantage of the rich
collection of existing functions in the C standard library
• According to the C standard committee, the next C
standard is likely to be released in 2022 • Two parts to learning C programming:
• Because C is a hardware-independent, widely available – learning the C language itself, and
language, C applications often can run with little or no – learning how to use the functions in the C standard
modification on a wide range of computer systems library.
• Throughout the book, we discuss many of these functions
Copyright © 2022 Pearson Education, Inc. All Rights Reserved Copyright © 2022 Pearson Education, Inc. All Rights Reserved
1.7 The C Standard Library and Open- 1.7 The C Standard Library and Open-
Source Libraries (2 of 2) Source Libraries—Open-Source Libraries
• When programming in C, you’ll typically use the • There are enormous numbers of third-party and open-
following building blocks: source C libraries that can help you perform significant
– C standard library functions, tasks with modest amounts of code. GitHub lists over
– open-source C library functions, 32,000 repositories in their C category:
– functions you create yourself, and – https://github.com/topics/c
– functions other people have created and made • In addition, pages such as Awesome C provide curated
available to you. lists of popular C libraries for a wide range of application
areas.
• Throughout the book, we focus on using the existing C
– https://github.com/kozross/awesome-c
standard library to leverage your program-development
efforts and avoid “reinventing the wheel.”
• This is called software reuse.
Copyright © 2022 Pearson Education, Inc. All Rights Reserved Copyright © 2022 Pearson Education, Inc. All Rights Reserved
10
1.8 Other Popular Programming 1.8 Other Popular Programming
Languages (1 of 3) Languages (2 of 3)
• BASIC was developed in the 1960s at Dartmouth College to familiarize • Java—Sun Microsystems in 1991 funded an internal corporate
novices with programming techniques research project led by James Gosling, which resulted in the
– Many of its latest versions are object-oriented C++-based object-oriented programming language called Java
• C++, which is based on C, was developed by Bjarne Stroustrup in the early – “write once, run anywhere”
1980s at Bell Laboratories
– used in enterprise applications, web servers, applications
– C++ provides features that enhance the C language and adds object-
oriented programming capabilities for consumer devices, …
• Python is an object-oriented language that was released publicly in 1991 • C# (based on C++ and Java) is one of Microsoft’s three
– Developed by Guido van Rossum of the National Research Institute for primary object-oriented programming languages
Mathematics and Computer Science in Amsterdam – developed to integrate the web into computer applications
– Python has rapidly become one of the world’s most popular
– now widely used to develop many kinds of applications
programming languages, especially for educational and scientific
computing, and in 2017 it surpassed the programming language R as – As part of Microsoft’s many open-source initiatives, they
the most popular data-science programming language now offer open-source versions of C# and Visual Basic
Copyright © 2022 Pearson Education, Inc. All Rights Reserved Copyright © 2022 Pearson Education, Inc. All Rights Reserved
11
1.9.2 Phases 2 and 3: Preprocessing and
1.9.1 Phase 1: Creating a Program Compiling a C Program (1 of 3)
• Consists of editing a file in an editor program: • You give the command to compile the program
• Compiler translates it into machine-language code
• The compilation command invokes a preprocessor first
– Performs text manipulations on a program’s source-
code files
– Inserting the contents of other files
– Text replacements
Copyright © 2022 Pearson Education, Inc. All Rights Reserved Copyright © 2022 Pearson Education, Inc. All Rights Reserved
1.9.2 Phases 2 and 3: Preprocessing and 1.9.2 Phases 2 and 3: Preprocessing and
Compiling a C Program (2 of 3) Compiling a C Program (3 of 3)
Copyright © 2022 Pearson Education, Inc. All Rights Reserved Copyright © 2022 Pearson Education, Inc. All Rights Reserved
12
1.9.3 Phase 4: Linking 1.9.4 Phase 5: Loading (1 of 2)
• C programs use functions defined elsewhere • Before a program can execute, the operating system
– standard libraries, open-source libraries or private libraries must load it into memory
of a particular project.
• Loader transfers the executable image from disk to
• The object code produced by the C compiler typically contains memory
“holes”
• Additional components from shared libraries that support
• Linker links a program’s object code with the code for the the program also are loaded
missing functions to produce an executable image (with no
missing pieces)
Copyright © 2022 Pearson Education, Inc. All Rights Reserved Copyright © 2022 Pearson Education, Inc. All Rights Reserved
Copyright © 2022 Pearson Education, Inc. All Rights Reserved Copyright © 2022 Pearson Education, Inc. All Rights Reserved
13
1.9.6 Problems That May Occur at 1.9.7 Standard Input, Standard Output
Execution Time and Standard Error Streams
• Errors that occur as programs run are called runtime • Most C programs input and/or output data
errors or execution-time errors
• Certain C functions take their input from stdin (the
• Fatal errors cause a program to terminate immediately standard input stream), which is normally the keyboard
without successfully performing its job
• Data is often output to stdout (the standard output
• Nonfatal errors allow programs to run to completion, stream), which is normally the computer screen
often producing incorrect results
• Data also may be output to devices such as disks and
printers
• There’s also a standard error stream referred to as stderr,
which is normally connected to the screen and used to
display error messages
Copyright © 2022 Pearson Education, Inc. All Rights Reserved Copyright © 2022 Pearson Education, Inc. All Rights Reserved
Copyright
14