Specification and Abstraction

Chapter 6 · Tamil Nadu State Board

  • 13 min read
  • 5-question quiz

Every key point below shows the sentence from your textbook it comes from. Book back answers are solved and checked against the book; answers we could not prove are left out. How we check content

In short

This lesson explains how algorithms solve problems by processing inputs and producing outputs that meet a stated relation. It teaches specification, which defines the input properties and desired output relation, and abstraction, which keeps essential details while setting irrelevant ones aside. Variables represent data and the changing state of a process, while assignment and control flow describe how an algorithm proceeds. These ideas help students describe problems clearly and manage the complexity of designing algorithms.

Written by AI from this lesson. How we check it

Key points

  • An algorithm is an ordered set of instructions that aims to solve a problem or complete a task.
    From your textbook
    “An algorithm is a sequence of instructions to accomplish a task or solve a problem.”
  • A problem is solved only when the output produced from the input satisfies the required relation.
    From your textbook
    “Only then has the algorithm solved the given problem.”
  • An algorithmic problem may require constructing an algorithm or proving properties of one that is already provided.
    From your textbook
    “Algorithmic problem solving involves construction of algorithms as well as proving properties of algorithms.”
  • Variables store data and can represent the state of a process as their values change.
    From your textbook
    “The state of a process can be represented by a set of variables in an algorithm.”
  • Control flow describes the order in which algorithm statements are executed.
    From your textbook
    “This order of execution of statements is known as the control flow.”
  • Abstraction hides irrelevant information so a problem is represented by its essential features.
    From your textbook
    “Abstraction is the process of ignoring or hiding irrelevant details and modeling a problem only by its essential features.”
  • A specification states the algorithm's name and inputs, input properties, and desired input-output relation.
    From your textbook
    “The specification of an algorithm consists of the name of the algorithm (together with its inputs), the input property, and the desired input-output relation.”
Test yourself: 5 questions

Formulas in this chapter

Algorithms and Problem Specification

Definition

Algorithm
An algorithm is a step-by-step sequence of instructions intended to complete a task or solve a problem.

Each instruction describes an action. When the instructions run, they create a process that produces the intended result. A computer can execute instructions written as statements in a programming language.

What specifies a problem?

  • Input data: what the algorithm starts with.
  • Desired output data: what the algorithm should produce.
  • A relation between the input and output data: the condition the result must satisfy.

An algorithm begins with input data and ends with output data. It solves the problem only if the output satisfies the specified relation to the input.

Polya's problem-solving steps

  • Understand the problem.
  • Devise a plan.
  • Carry out the plan.
  • Review your work.

Algorithmic Problems

Definition

Algorithmic problem
A problem is algorithmic in nature when solving it involves constructing an algorithm.

Two kinds of algorithmic problems

Construct an algorithmStudy an algorithm
Create a sequence of instructions to reach the goal.Use a given algorithm's properties to prove or determine something.
Example: plan how a farmer can take a goat, grass bundle and wolf across a river without leaving unsafe pairs alone.Example: determine which starting numbers of red, green and blue chameleons can lead to all displaying the same color.

Remember: An algorithmic problem may ask you to build an algorithm, or to reason about the properties of one that is already provided.

Data, Variables and State

Basic building blocks of algorithms

  • Data
  • Variables
  • Control flow
  • Functions

Algorithms take input data, process it and produce output data. Data can include numbers and text. Instructions can perform operations on data, such as adding, subtracting, multiplying and dividing.

Definition

Variable
A variable is a named box used to store data, and the data stored in it is called its value.

An assignment statement stores a value in a variable or changes its value. A process has a state, and an algorithm can represent that state using variables. The state at any point is the set of values held by those variables at that point.

Worked example

Represent the state of a traffic signal.

Try it first, then show the solution
  1. Use a variable named signal.
  2. Let it hold one of the values green, amber, or red.

Answer: The value of signal represents the traffic signal's current state.

Remember: When variable values change, the state represented by those variables changes too.

Control Flow in Algorithms

Definition

Control flow
Control flow is the order in which an algorithm's statements are executed.

Statements may run in a different order from the order in which they are written. The next statement can depend on the state of the process.

Three types of control flow

TypeHow it works
SequentialStatements run one after another in the order they are written.
AlternativeA condition is tested. One statement runs if it is true, and another runs if it is false.
IterativeA condition is tested, and a statement runs when it is true. Testing and running repeat until the condition becomes false.

Functions and Decomposition

Definition

Function
A function is a part of an algorithm, like a sub algorithm, that takes an input and produces an output satisfying a desired input-output relation.

A complex algorithm can be difficult to build correctly. We can split it into smaller parts, construct each part separately, and then combine them into the complete algorithm.

Cylinder surface area example

  • The surface area is A = 2πr2 + 2πrh.
  • Use circle_area(r) to calculate the area of a circle.
  • Use circle_circumference(r) to calculate the circumference of a circle.
  • The functions combine as cylinder_area (r,h) = 2 X circle_area (r) + circle_circumference (r) X h.

Remember: When using a function, knowing its specification is enough; you do not need to know how it is implemented.

Algorithm Design Techniques

Four basic techniques

  • Specification: State the problem precisely using its inputs, desired outputs, input properties, and the relation between inputs and outputs.
  • Abstraction: Hide unnecessary details and represent an entity using only the properties essential to the problem.
  • Composition: Build an algorithm from assignment and control flow statements. Control flow tests a condition and uses its value to choose the next statement.
  • Decomposition: Divide the main algorithm into functions. Construct each function independently, then build the main algorithm using those functions.

When representing the state of a process, abstraction means selecting only the variables essential to the problem. Decomposition lets each function be constructed separately from the main algorithm and other functions.

Algorithm Specification and Comments

Definition

Specification of an algorithm
The specification of an algorithm is the desired input-output relation, stated using the input properties and the relation between inputs and outputs.

An algorithm starts with an initial state and finishes with a final state. These are the values of its variables at the start and finish. A specification describes what must be true about the inputs and outputs.

Three parts of a specification

  • Write the algorithm name and its inputs.
  • Write the input properties as a comment starting with -- inputs:.
  • Write the desired input-output relation as a comment starting with -- outputs:.

Definition

Comment
A comment is text used to explain a program to human readers; the computer does not execute it.

In the algorithmic notation, -- begins a comment. In C++, // indicates that the rest of the line is a comment. Comments at important points in the flow help readers understand an algorithm.

Worked example

Specify an algorithm to divide integer A by integer B, producing quotient q and remainder r.

Try it first, then show the solution
  1. Name the algorithm divide and use A and B as its inputs.
  2. Require A to be an integer and B ≠ 0.
  3. Require A = q X B + r and 0 ≤ r < B.

Answer: divide (A , B) -- inputs: A is an integer and B ≠ 0 -- outputs : A = q X B + r and 0 ≤ r < B

Worked example

Specify an algorithm for computing the square root of a number.

Try it first, then show the solution
  1. Name the algorithm square_root and use n as its input.
  2. Require n to be a real number with n ≥ 0.
  3. Use y as the output, with y a real number such that y 2= n.

Answer: square_root(n) -- inputs: n is a real number, n ≥ 0. -- outputs: y is a real number such that y 2= n.

Remember: A specification states what an algorithm must achieve, not how it performs the task.

Algorithm Specification as a Contract

A specification is a contract between the algorithm designer and its users. The user must provide inputs with the required properties. The designer must produce the promised relationship between inputs and outputs.

User and designer responsibilities

UserAlgorithm designer
Responsibility: provide inputs with the required properties.Right: assume the inputs have the required properties.
Right: expect the desired input-output relationship.Responsibility: provide the desired input-output relationship.

If the user does not provide inputs that meet the specification, the designer is not required to provide the promised output. For square_root(n), the input must be a real number with n ≥ 0. The output y must be a real number such that y2 = n.

Remember: Check that the input meets the specification before expecting the promised output.

Abstraction and Essential Details

Definition

Abstraction
Abstraction means hiding or ignoring irrelevant details and representing a problem using only its essential features.

Abstraction helps us manage complexity. A bicycle rider can focus on the pedals, handlebar, brakes and bell, and ignore details that do not matter for riding. Including unnecessary details can make a problem and its solution more complicated.

Examples of abstraction

  • A road map is made for drivers, so most hills may be left out.
  • A walker's map can leave out whether a road is one-way.
  • An orthopaedician focuses on the skeletal system.
  • A gastroenterologist focuses on the digestive system.
  • A physiotherapist focuses on the muscular system.

In algorithms, abstraction is used in problem specifications, in representing state with variables, and in dividing a complex algorithm into functions. A specification keeps the input properties and desired input-output relationship that matter for solving the problem.

Abstraction in algorithms

PartHow it is abstracted
StateBy a set of variables.
FunctionBy its specification.

State as an Abstraction

Definition

State
The state of a computation is the values of its variables at a particular point in its execution.

A computational process begins in an initial state. Its state changes as actions take place, and it ends in a final state. Variables provide a simple way to represent these states.

For the chocolate-bar problem, the essential variables are p, the number of pieces, and c, the number of cuts. The shapes and sizes of the pieces, and the order of the cuts, are not needed to represent the problem for solving it.

For the goat, grass and wolf problem, the variables farmer, goat, grass and wolf each have the value L or R. L means the left side of the river, and R means the right side. The boat's position does not need a variable because it is always with the farmer.

River-crossing states

Statefarmer, goat, grass, wolf
Initial stateL, L, L, L
Final stateR, R, R, R

Remember: A good state representation keeps the variables needed for the problem and leaves out irrelevant details.

Assignment statements and variables

A variable is a named box that stores a value. An assignment statement puts a value into a variable. Write the variable on the left of := and the value or expression on the right.

Assignment form

variable := value

The variable on the left receives the value on the right. Read := as “becomes” or “is assigned”.

If a variable already has a value, a new assignment replaces it. The old value is lost. When the right side is an expression, calculate it first using the variable’s current value, then store the result.

Worked example

If m contains 2, what value does m have after m := m + 3?

Try it first, then show the solution
  1. Use the current value of m: m + 3 = 2 + 3.
  2. Calculate the expression to get 5.
  3. Store 5 in m.

Answer: m contains 5.

Assignment and equality

:= assignment operator= equality operator
Changes a variable’s stored value.States a mathematical equality.
m := m + 3 means m becomes the result of m + 3.m = m + 3 does not mean that m is updated.

Remember: An assignment changes a variable’s value; it does not state that the old value equals the expression.

Assigning values to several variables

One assignment statement can update several variables. The left side lists variables, and the right side lists expressions. Both lists must have the same number of items.

Multiple assignment form

v1, v2, v3 := e1, e2, e3

Evaluate each expression on the right using the current values, then store the results in the matching variables on the left.

Worked example

Find m and n after m, n := 2, 5 followed by m, n := m + 3, n - 1.

Try it first, then show the solution
  1. The first assignment gives m = 2 and n = 5.
  2. Evaluate the second assignment using those current values: m + 3 = 2 + 3 = 5, and n - 1 = 5 - 1 = 4.
  3. Store the results in the matching variables.

Answer: m = 5 and n = 4.

Worked example

Model one cut of a piece of chocolate when both the number of pieces p and the number of cuts c increase by 1.

Try it first, then show the solution
  1. Increase p by 1 and c by 1.
  2. Write both updates in one assignment.

Answer: p, c := p + 1, c + 1

State, abstraction, and algorithms

Definition

State
The values of the variables in an algorithm define the state of the process.

Definition

Abstraction
Abstraction hides or ignores details that are not needed and models a problem using its essential features.

A process changes as an algorithm’s statements are executed. Since assignments change variable values, they also change the state. An algorithm is a step-by-step sequence of statements used to solve a problem.

Key ideas

  • A programming language provides basic statements and a notation for composing compound statements.
  • Algorithmic problem solving includes constructing algorithms and proving their properties.
  • An algorithm specification gives the algorithm’s name and inputs, the input property, and the desired input-output relation.
  • A specification is a contract between the algorithm’s designer and its users.

Remember: Assignments change variable values, and those values represent the state of the process.

Questions students ask

Short, direct answers to what students search for in this chapter.

What is an algorithm?

An algorithm is a step-by-step sequence of instructions designed to complete a task or solve a problem. When its instructions are executed with input data, they create a process and produce output data.

What information is needed to specify a problem?

A problem is specified by its input data, its desired output data and the relation that the output must satisfy with the input. An algorithm solves the problem when its final output meets that relation.

What is an algorithmic problem?

An algorithmic problem is one whose solution involves constructing an algorithm. Some such problems instead provide an algorithm and ask us to use its properties to prove or determine something.

How do variables represent the state of a process?

An algorithm represents a process's state with a set of variables. At any point during execution, the state is the values held by those variables. When the values change, the represented state changes.

What is control flow in an algorithm?

Control flow is the order in which an algorithm's statements are executed. This order may differ from the written order because the next statement can depend on the state of the process. Sequential, alternative, and iterative control flow are three important types.

What is the difference between alternative and iterative control flow?

Alternative control flow tests a condition and runs one statement if it is true or another if it is false. Iterative control flow tests a condition and runs a statement when it is true, repeating the test and statement until the condition becomes false.

Show 12 more questions

What is a function in an algorithm?

A function is a part of an algorithm, like a sub algorithm. It takes an input and produces an output that satisfies a desired input-output relation. Functions help divide complex algorithms into parts that can be constructed separately and then combined.

What are the four basic algorithm design techniques?

The four basic techniques are specification, abstraction, composition, and decomposition. Specification states the problem precisely. Abstraction hides unnecessary details. Composition uses assignment and control flow statements, while decomposition divides the main algorithm into functions.

What are the three parts of an algorithm specification?

An algorithm specification has three parts: the algorithm name and inputs, the input properties, and the desired input-output relation. The input properties are written in a comment beginning with -- inputs:, and the relation is written in a comment beginning with -- outputs:.

What does a specification mean in an algorithm?

A specification acts as a contract between an algorithm designer and its users. It states the required properties of the inputs and the desired relationship between inputs and outputs. The user must meet the input requirements, and the designer must provide the promised result.

What is abstraction in computer science?

Abstraction is the process of ignoring or hiding details that are not needed and representing a problem through its essential features. It helps manage complexity. In algorithms, abstraction is used in specifications, in representing state with variables, and in describing functions.

How is the state of a computation represented?

The state of a computation is represented by a set of variables. At any point during execution, the state is the values of those variables. A process starts in an initial state, changes as actions occur, and finishes in a final state.

Why does the goat, grass and wolf problem not need a boat variable?

The problem uses the variables farmer, goat, grass and wolf to show which side of the river each is on. Each variable has the value L or R. The boat does not need its own variable because it is always with the farmer.

What is an assignment statement in computer science?

An assignment statement stores a value in a variable. The variable is written on the left of :=, and a value or expression is written on the right. If the right side is an expression, calculate it using the current values before updating the variable.

What does the assignment operator := mean?

The symbol := is the assignment operator. It is read as “becomes” or “is assigned”. In m := m + 3, the expression uses the current value of m, and its result replaces the value stored in m.

What is the difference between assignment and equality?

Assignment changes the value stored in a variable. Equality states a mathematical relationship. So m := m + 3 updates m with the calculated result, while m = m + 3 does not describe an update.

How does multiple assignment work?

In multiple assignment, the left side lists variables and the right side lists matching expressions. The two lists must have the same number of items. First evaluate every right-side expression using the current values, then store each result in its corresponding variable.

What is abstraction in algorithms?

Abstraction means ignoring details that are not needed for the task and keeping the essential features of the problem. It helps represent a problem using suitable variables, whose values describe the state of the process.

Book back answers

17 textbook exercises, solved and checked against the book. Questions we could not prove are left out.

SECTION – A Choose the correct answer

  1. Which of the following activities is algorithmic in nature?

    • (a) Assemble a bicycle.
    • (b) Describe a bicycle.
    • (c) Label the parts of a bicycle.
    • (d) Explain how a bicycle works.
    Try it, then show the answer

    (a) Assemble a bicycle.

    Assembling a bicycle involves carrying out a task through a sequence of instructions.

    From the book: “An algorithm is a sequence of instructions to accomplish a task or solve a problem.”

  2. Omitting details inessential to the task and representing only the essential features of the task is known as

    • (a) specification
    • (b) abstraction
    • (c) composition
    • (d) decomposition
    Try it, then show the answer

    (b) abstraction

    Abstraction hides or ignores irrelevant details and models a problem using its essential features.

    From the book: “Abstraction is the process of ignoring or hiding irrelevant details and modeling a problem only by its essential features.”

  3. Stating the input property and the input-output relation a problem is known

    • (a) specification
    • (b) statement
    • (c) algorithm
    • (d) definition
    Try it, then show the answer

    (a) specification

    The specification states the property of the inputs and the desired relation between input and output.

    From the book: “An algorithm is specified by the properties of the given input and the relation between the input and the desired output.”

  4. Ensuring the input-output relation is

    • (a) the responsibility of the algorithm and the right of the user.
    • (b) the responsibility of the user and the right of the algorithm.
    • (c) the responsibility of the algorithm but not the right of the user.
    • (d) the responsibility of both the user and the algorithm.
    Try it, then show the answer

    (a) the responsibility of the algorithm and the right of the user.

    The designer is responsible for the desired input-output relation, and the user has the right to expect it.

    From the book: “The desired input-output relation is the responsibility of the designer and the right of the user.”

All 17 book back answers →

Practice

Question 1 of 5Score 0

What must be true for an algorithm to have solved its specified problem?

Exam questions

Practice questions with model answers. These are not from an official paper.

What information is included in the specification of an algorithm?2 marks

It includes the algorithm's name and inputs, the required properties of the inputs, and the desired relation between inputs and outputs.

Explain abstraction and give one example from this lesson.5 marks

Abstraction means ignoring details that are irrelevant to a task and representing the problem through essential features. For riding a bicycle, a rider can focus on the pedals, handlebar, brakes and bell, while ignoring other details.

How do variables represent the state of a process?2 marks

The state is represented by a set of variables. At any point in execution, their current values describe the state; when those values change, the state changes.

Distinguish between an assignment operator and an equality operator.2 marks

An assignment operator changes the value stored in a variable, using the expression on its right. An equality operator expresses equality; assignment does not assert a mathematical equality.

Watch out

  • Do not treat assignment as mathematical equality: assignment changes a variable's stored value.
  • Do not assume a specification guarantees more than it states; for example, the square_root specification does not require the positive root.

Still stuck on this lesson?

Get an AI answer grounded in your actual textbook — with the exact page reference.

Ask AI about this topic →