ENCO101 - Unit 8
ENCO101 - Unit 8
to The School of
Engineering and
Science
Application Description
Operating systems C’s portability and performance make it desirable for implementing
operating systems, such as Linux and portions of Microsoft’s
Windows and Google’s Android. Apple’s macOS is built in
Objective-C, which was derived from C.
Embedded systems Most of the microprocessors produced each year are embedded in
devices other than general-purpose computers. These embedded
systems include navigation systems, smart home appliances, home
security systems, smartphones, tablets, robots, intelligent traffic
intersections and more. C is one of the most popular programming
languages for developing embedded systems, which typically need
to run as fast as possible and conserve memory. For example, a
car’s antilock brakes must respond immediately to slow or stop the
car without skidding; video-game controllers should respond
instantaneously to prevent lag between the controller and the
game action.
Built for Performance
Application Description
Real-time systems Real-time systems are often used for “mission-critical” applications
that require nearly instantaneous and predictable response times.
Real-time systems need to work continuously. For example, an air-
traffic-control system must continuously monitor planes’ positions
Communications systems and velocities and report that information to air-traffic controllers
without delay so they can alert the planes to change course if
there’s a possibility of a collision.
Communications systems need to route massive amounts of data
to their destinations quickly to ensure that things such as audio
and video are delivered smoothly and without delay.
Compiling a C Application with
Visual Studio
1. Download Visual Studio
Visit the Website: Go to the official https://visualstudio.microsoft.com
Select Edition: Choose the appropriate edition (Community, Professional, or Enterprise). The Community edition is free
and suitable for most users.
Download Installer: Click "Download" to get the Visual Studio Installer.
3. Set Up a C Project
Launch Visual Studio: Open Visual Studio after installation.
Create a New Project: Click on "Create a new project" from the start window.
Select Project Type: Choose "Console App" under C++ and give your project a name.
Configure: Set the project location and other options as needed, then click "Create."
Compiling a C Application with
Visual Studio
4. Write and Run C Code
Open Code Editor: Once the project is created, Visual Studio will open the main code editor.
Write Code: Enter your C code in the main.cpp file (you can write C code even though the file extension is .cpp).
Build the Project: Click "Build" from the top menu, then "Build Solution" to compile the code.
Run the Program: After building, click "Debug" > "Start Without Debugging" to run your program and see the output.
• In the C statement, parentheses are required to group the additions because division has higher precedence
than addition.
• The entire quantity (a + b + c + d + e) should be divided by 5.
• If we erroneously omit the parentheses, we obtain a + b + c + d + e /5, which evaluates incorrectly as
• The following expression is the equation of a straight line:
Arithmetic in C
• No parentheses are required.
• Multiplication evaluates first because it has higher precedence than addition.
• The following expression contains remainder (%), multiplication, division, addition, subtraction and
assignment operations:
• The circled numbers indicate the order in which C evaluates the operators.
• The multiplication, remainder and division evaluate first left-to-right (that is, they group left-to-right)
because they have higher precedence than addition and subtraction.
• Next, the addition and subtraction evaluate left-to-right.
• Finally, the result is assigned to z.
Decision Making: Equality and Relational
Operators
• Executable statements either perform
actions like calculations, input and output, or,
as you’re about to see, make decisions.
• For example, a program might determine
whether a person’s grade on an exam is
greater than or equal to 60, so it can decide
whether to print the message
“Congratulations! You passed.”
• A condition is an expression that can be true
(that is, the condition is met) or false (that is,
the condition isn’t met).
• This section introduces the if statement,
which allows a program to make a decision
based on a condition’s value.
• If the condition is true, the statement in the if
statement’s body executes; otherwise, it
does not.
Decision Making: Equality and Relational
Operators
Confusing the Equality Operator == with the
Assignment Operator
• Confusing == with the assignment operator (=) is a
common programming error.
• To avoid this confusion, read the equality operator as
“double equals” and the assignment operator as
“gets” or “is assigned the value of.”
• As you’ll see, confusing these operators can cause
difficult-to-find logic errors rather than compilation
errors.
Demonstrating the if Statement
• Figure 2.5 uses six if statements to compare two
numbers entered by the user.
• For each if statement with a true condition, the
corresponding printf executes.
• The program and three sample execution outputs are
shown in the figure.
Demonstrating the if Statement
Even and Odd numbers
Heart Rate Monitor
Introduction
■ Use basic problem-solving techniques.
■ Develop algorithms through the process of top-down, stepwise
refinement.
■ Select actions to execute based on a condition using the if and if…else
selection statements.
■ Execute statements in a program repeatedly using the while iteration
statement.
■ Use counter-controlled iteration and sentinel-controlled iteration.
■ Use structured programming techniques.
■ Use increment, decrement and assignment operators.
Algorithms
• The solution to any computing problem involves executing a series of actions in a
specific order.
• An algorithm is a procedure for solving a problem in terms of
1. the actions to execute, and
2. the order in which these actions should execute.
• The following example shows that correctly specifying the order in which the
actions should execute is important.
• Consider the “rise-and-shine algorithm” followed by one junior executive for
getting out of bed and going to work:
1. Get out of bed,
2. take-off pyjamas,
3. take a shower,
4. get dressed,
5. eat breakfast, and
6. carpool to work.
• This routine gets the executive to work well prepared to make critical
decisions.
• Suppose that the same steps are performed in a slightly different order:
Algorithms
• Suppose that the same steps are performed in a slightly
different order:
1. Get out of bed,
2. take-off pyjamas,
3. get dressed,
4. take a shower,
5. eat breakfast,
6. carpool to work.
• In this case, our junior executive shows up for work soaking wet.
• Specifying the order in which statements should execute in a computer
program is called program control.
• In this and the next chapter, we investigate C’s program control
capabilities.
Pseudocode
• Pseudocode is an informal artificial language similar to everyday English that helps you
develop algorithms before converting them to structured C programs.
• Pseudocode is convenient and user friendly.
• It helps you “think out” a program before writing it in a programming language.
• Computers do not execute pseudocode.
• Pseudocode consists purely of characters, so you may type it in any text editor.
• Pseudocode describes the actions and decisions that will execute once you convert the
pseudocode to C and run the program.
• Definitions are not executable statements they’re simply messages to the compiler.
• For example, the definition
• int i = 0;
• tells the compiler variable i’s type, instructs the compiler to reserve space in memory for
the variable and initializes it to 0.
• But this definition does not perform an action when the program executes, such as
input, output, a calculation or a comparison.
• So, some programmers do not include definitions in their pseudocode.
• Others choose to list each variable and briefly mention its purpose.
Control Structures
• Böhm and Jacopini’s work demonstrated that all programs could be written in
terms of three control structures, namely the sequence structure, the
selection structure and the iteration structure.
• The sequence structure is simple—unless directed otherwise, the computer
executes C statements one after the other in the order in which they’re
written.
• Flowcharts
• A flowchart is a graphical representation of an algorithm or of a portion of an
algorithm.
• You draw flowcharts using certain special-purpose symbols such as rectangles,
diamonds, rounded rectangles, and small circles, connected by arrows called
flowlines.
• Flowcharts help you develop and represent algorithms.
• Flowcharts clearly show how control structures operate.
• Consider the following flowchart for a sequence structure in a portion of an
algorithm that calculates the class average on a quiz:
Control Structures
• The rectangle (or action) symbol indicates any action, such as a calculation,
input or output.
• The flowlines indicate the order in which to perform the actions.
• This program segment first adds grade to total, then adds 1 to counter.
• As we’ll soon see, anywhere in a program a single action may be placed, you
may place several actions in sequence.
• When drawing a flowchart for a complete algorithm, the first symbol is a
rounded rectangle symbol containing “Begin”, and the last is a rounded
rectangle containing “End”.
• When drawing only a portion of an algorithm, we omit the rounded rectangle
symbols in favor of using small circles called connector symbols.
Control Structures
• The class average is the sum of the grades divided by the number of
students.
• The algorithm to solve this problem must input the grades, then calculate
and display the class average.
Pseudocode for the Class-Average Problem
• Let’s use pseudocode to list the actions to execute
and specify the order in which they should execute.
• We use counter-controlled iteration to input the
grades one at a time.
• This technique uses a variable called a counter to
specify the number of times a set of statements
should execute. In this example, we know that ten
students took a quiz, so we need to input 10 grades.
• Iteration terminates when the counter exceeds 10.
• In this case study, we simply present the final
pseudocode algorithm (Fig. 3.1) and the
corresponding C program (Fig. 3.2).
• In the next case study, we show how to develop
pseudocode algorithms.
• Counter-controlled iteration is often called definite
iteration because the number of iterations is known
before the loop begins executing.
Class-average problem with
counter-controlled iteration
Formulating Algorithms with Top-Down, Stepwise
Refinement Case Study 2: Sentinel-Controlled
Iteration
• Second Refinement
• To proceed to the next level of refinement, i.e., the second refinement, we commit to
specific variables. We need:
• a running total of the grades,
• a count of how many grades have been processed,
• a variable to receive the value of each grade as it is input and
• a variable to hold the calculated average.
• The pseudocode statement
• Initialize variables
• can be refined as follows:
• Initialize total to zero
• Initialize counter to zero
• Only the total and counter need to be initialized.
• The variables for the calculated average and the grade the user inputs need not be
initialized because their values will be calculated and input from the user, respectively.
• The pseudocode statement
• Input, sum, and count the quiz grades
Formulating Algorithms with Top-Down, Stepwise
Refinement Case Study 2: Sentinel-Controlled
Iteration
• The program will test for this value after each grade is input and will terminate the loop when
the sentinel is entered.
• The refinement of the preceding pseudocode statement is then
Input the first grade (possibly the sentinel)
While the user has not as yet entered the sentinel
Add this grade into the running total
Add one to the grade counter
Input the next grade (possibly the sentinel)
• In pseudocode, we do not use braces around the set of statements that form a loop’s body.
• We simply indent the body statements under the while.
• Again, pseudocode is an informal program-development aid.
• The pseudocode statement
Calculate and print the class average
• may be refined as follows:
If the counter is not equal to zero
Set the average to the total divided by the counter
Print the average
else
Print “No grades were entered”
Formulating Algorithms with Top-Down, Stepwise
Refinement Case Study 2: Sentinel-Controlled
Iteration
else
Print “No grades were entered”
Phases in a Basic Program