Introduction to Problem Solving
Introduction to Problem Solving
Page 2 of 198
Introduction to Problem Solving - CSC 102
Headquarters
Utako, Abuja
Page 3 of 198
Introduction to Problem Solving - CSC 102
Page 4 of 198
Introduction to Problem Solving - CSC 102
Table of Contents
Table of Contents 5
List of Figures 14
List of Tables 14
Study Session 1: Computer Systems 16
Introduction 16
Learning Outcomes (LOs) 16
1.1 Computer Systems 16
1.2 Hardware 17
1.2.1 Primary memory 18
Cache memory: 18
1.2.2 Secondary Memory: 18
Magnetic Storage: 19
Optical Storage: 19
1.3 Computer Software 19
1.3.1 System Software 19
1.3.2 Application software 20
Conclusion 21
Summary 22
Self-Assessment Questions (SAQ) 22
Self-Assessment Answers (SAA) 22
Tutor-Marked Assignment 23
References/Further Reading 23
Study Session 2: Computing Environment 25
Introduction 25
Learning Outcomes (LOs) 25
2.1 Computing Environments 25
2.1.1 Personal Computing Environment 26
Page 5 of 198
Introduction to Problem Solving - CSC 102
References/Further Reading 42
Study Session 4: Procedure (Steps Involved in Problem Solving) 44
Introduction 44
Learning Outcomes (LOs) 45
4.1 Procedure (Steps involved in Problem Solving) 45
4.2 Algorithm 46
4.2.1 Steps involved in algorithm development 47
Example 49
4.2.2 Algorithms for Simple Problem 49
Conclusion 54
Summary 54
Self-Assessment Questions (SAQ) 55
Self-Assessment Answers (SAA) 55
Tutor-Marked Assignment 59
References/Further Reading 59
Study Session 5: Flowchart 61
Introduction 61
Learning Outcomes (LOs) 61
5.1 Flowchart 62
5.1.1 Advantages of Flowcharts 62
5.1.2 Differences between Algorithm and Flowchart 63
5.1.3 Symbols used in a flowchart 63
5.1.4 Simple Problems solved using FlowChart 66
Pseudocode 70
Conclusion 71
Summary 71
Self-Assessment Questions (SAQ) 71
Self-Assessment Answers (SAA) 72
Tutor-Marked Assignment 74
References/Further Reading 74
Study Session 6: Features of C 76
Page 7 of 198
Introduction to Problem Solving - CSC 102
Introduction 76
Learning Outcomes (LOs) 76
6.1 Features of C language 77
6.2 Character Set 77
6.3 Structure of a C-Program 79
6.4 Data types in C language. 81
6.4.1 Basic Data Types 82
6.5 Tokens in C 83
6.5.1 Constant/Literals: 84
Numeric Constants: 84
Integer Constants 84
A decimal integer constant is characterised by the
following properties: 85
An octal integer constant is characterised by the following
properties: 85
An hexadecimal integer constant is characterised by the
following properties: 86
Real Constants 87
Fractional Form 87
Exponential Form 88
Character Constants 90
String Constants 90
6.5.2 Reserved Words/Keywords 91
6.5.3 Delimiters 92
6.5.4 Variables / Identifiers 92
Rules for naming a variable:- 93
Declaration of variables: 93
6.6 Operators 93
6.6.1 Arithmetic Operators 94
Operator Meaning 94
6.6.2 Relational Operators 95
Page 8 of 198
Introduction to Problem Solving - CSC 102
Operator Meaning 95
Examples : 95
6.6.3 Logical Operators 96
Logical And (&&): 96
Example: 97
Logical OR: 97
Example: 97
Logical NOT: 98
Example: 98
6.6.4 Assignment Operator 98
Example: 99
Multiple assignment 99
Example: 99
Example: 100
6.6.5 Increment & Decrement Operator 100
Example 1: 101
Example 2: 101
6.6.6 Conditional operator (or) Ternary operator (? :) 101
Example: 102
6.6.7 Bit wise Operator 102
The logical Bitwise Operator 103
Example: 104
The Bitwise shift Operations: 104
Example: 105
Example: 105
Bit wise complement: 106
6.6.8 Comma Operator 106
Example : 106
Size of operator: 106
Example : 107
Conclusion 107
Page 9 of 198
Introduction to Problem Solving - CSC 102
Summary 107
Self-Assessment Questions (SAQ) 108
Self-Assessment Answers (SAA) 108
Tutor-Marked Assignment 111
References/Further Reading 112
Study Session 7: Expressions in C-Language 114
Introduction 114
Learning Outcomes (LOs) 114
7.1 Expression 114
Example: 114
7.2 Evaluation Procedure 115
7.3 Statements 115
7.3.1 Data Input & Output 115
getchar() 116
putchar() 117
gets() 117
puts() 117
printf() 118
Example: 118
Example: 119
scanf() 119
Example: 120
7.4 Assignment Statement 121
Conclusion 121
Summary 121
Self-Assessment Questions (SAQ) 122
Self-Assessment Answers (SAA) 122
Tutor-Marked Assignment 124
References/Further Reading 124
Study Session 8: Control Structure 126
Page 10 of 198
Introduction to Problem Solving - CSC 102
Introduction 126
Learning Outcomes (LOs) 126
8.1 Conditional Statements 127
8.1.1 if statement 127
8.1.2 if-else statements 128
8.1.3 Nested else-if statements 129
8.1.4 Nested if-else statement 130
8.1.5 Switch statement 132
Example 1: 133
Example 2: 134
Conclusion 134
Summary 135
Self-Assessment Questions (SAQ) 135
Self-Assessment Answers (SAA) 136
Tutor-Marked Assignment 138
References/Further Reading 139
Study Session 9: Looping Structures 141
Introduction 141
Learning Outcomes (LOs) 141
9.1 Loop Statement 141
9.1.1 While statement 142
Example: 142
9.1.2 do-while statement 143
Example: 143
9.1.3 for statement 144
Format - I 146
Example 1 146
Format - II 146
Example 2 146
Format - III 147
Page 11 of 198
Introduction to Problem Solving - CSC 102
Example 3 147
9.2 Nested Looping Statements 148
9.2.1 Definition of Nesting 148
9.3 Multi-Objective Loops 149
9.3.1 Break Statement 149
Example 149
9.3.2 The Continue Statement 150
Example 150
9.4 Differences between Break and Continue statements 151
9.5 Unconditional Branching (goto Statement) 153
Example 1 153
Practice Programs 155
Conclusion 157
Summary 157
Self-Assessment Questions (SAQ) 158
Self-Assessment Answers (SAA) 158
Tutor-Marked Assignment 163
References/Further Reading 163
Study Session 10: Functions in C 165
Introduction 165
Learning Outcomes (LOs) 166
10.1 Definition of Function 166
10.1.1 Return Type 167
Example 168
10.1.2 Function Name 168
Example 168
Formal arguments 169
Example 169
10.1.3 Function Body 169
Example 170
10.1.4 The Return Statement 171
Page 12 of 198
Introduction to Problem Solving - CSC 102
Page 13 of 198
Introduction to Problem Solving - CSC 102
List of Figures
List of Tables
Page 14 of 198
Introduction to Problem Solving - CSC 102
Page 15 of 198
Introduction to Problem Solving - CSC 102
Introduction
software.
Page 16 of 198
Introduction to Problem Solving - CSC 102
system can have objects and processes. The following are the objects of
a computer system:
b) Hardware
c) Software
1.2 Hardware
we can touch and feel. Example: keyboard and mouse. The hardware of a
Processing Unit (CPU), Output Devices(O/P) as you will see in figure 1.1
below:
MU: The memory unit is used for storing the data. The memory unit is
Page 17 of 198
Introduction to Problem Solving - CSC 102
● Primary memory.
● Secondary memory.
The following are the types of memories that are treated as primary ROM:
It represents Read Only Memory that stores data and instructions even
when the computer is turned off. The contents of the ROM cannot be
modified once they are written. It is used to store BIOS information. RAM:
when the computer is turned on. The contents of the RAM can be
Cache memory:
Cache memory is a small, fast memory located between the CPU and main
hierarchical structure with multiple levels (L1, L2, L3), the CPU checks the
cache for data before accessing the main memory. Cache management
devices that can store data even without a constant connection to the
Page 18 of 198
Introduction to Problem Solving - CSC 102
Magnetic Storage:
The magnetic storage devices store information that can be read, erased,
Magnetic Tapes
Optical Storage:
Optical storage devices that use laser beams to read and write stored
the other hand, is directly responsible for helping users solve their
problems.
tasks. These programs are divided into three classes: the operating
Page 19 of 198
Introduction to Problem Solving - CSC 102
The operating system provides services such as a user interface, file and
services. Examples of system utilities are sort programs and disk format
protect the system and data. The last system software category, system
ensure that the programs are error free and computer –assisted software
is purchased from a software developer and can be used for more than
systems. They are labelled general purpose because they can solve a
Page 20 of 198
Introduction to Problem Solving - CSC 102
uses. They can be used only for the task for which they were designed;
this figure, each circle represents an interface point. The inner core is
hardware. The user is represented by the outer layer. To work with the
system, the typical user uses some form of application software. The
part of the system software layer. The system software provides direct
interaction with the hardware. The opening at the bottom of figure 1.2 is
the path followed by the user, who interacts directly with the operating
Conclusion
This unit introduced the students to the computer system, the hardware
Page 21 of 198
Introduction to Problem Solving - CSC 102
primary memory. The application software and system software were also
Summary
outcome 1.5)
Page 22 of 198
Introduction to Problem Solving - CSC 102
problems.
storage for programs and files even when the computer is turned
off.
Tutor-Marked Assignment
References/Further Reading
Page 23 of 198
Introduction to Problem Solving - CSC 102
Bell, Eona, and David Potter, “Computer Applications for People with
Autism,” 2001.
http://www.nas.org.uk/nas/jsp/polopoly.jsp?d=303a=3276.
http://www.howstuffworks.com/
mouse.htm.
http://www.columbia.edu/acis/history/
hollerith.html.
Stats,
2003. http://www.pcstats.com/articleview.cfm?articleid=868.
http://www.ee.ryerson.ca:8080/
~elf/abacus/history.html.
Page 24 of 198
Introduction to Problem Solving - CSC 102
Introduction
computing technologies, and all are different from each other. By using
this, we are finding output based on the input given by the user. In a
technology, which is responsible for all the types of input and output
3. Client/Server Environment
In 1971, Mercian E. Hoff, working for INTEL, combined the basic elements of
personal computer, then all the computer hardware components are tied
user, who uses the computer for personal tasks. Ex: Personal Computer
Page 26 of 198
Introduction to Problem Solving - CSC 102
The central computer handles all of the processing. The computers that
ask for processing are only dumb terminals. Figure 2.2 below shows a time
sharing environment:
client machine is the one that requests processing. The server machine is
the one that offers the processing. Hence, the client is capable enough to
Page 27 of 198
Introduction to Problem Solving - CSC 102
just a requestor for processing information from the server. The client
computing.
Page 28 of 198
Introduction to Problem Solving - CSC 102
Conclusion
in this session.
Summary
Page 29 of 198
Introduction to Problem Solving - CSC 102
Page 30 of 198
Introduction to Problem Solving - CSC 102
manner.
Tutor-Marked Assignment
References/Further Reading
Bell, Eona, and David Potter, “Computer Applications for People with
Autism,” 2001.
http://www.nas.org.uk/nas/jsp/polopoly.jsp?d=303a=3276.
http://www.howstuffworks.com/
mouse.htm.
http://www.columbia.edu/acis/history/
hollerith.html.
Stats,
Page 31 of 198
Introduction to Problem Solving - CSC 102
2003. http://www.pcstats.com/articleview.cfm?articleid=868.
http://www.ee.ryerson.ca:8080/
~elf/abacus/history.html.
Page 32 of 198
Introduction to Problem Solving - CSC 102
Introduction
language
computer what to do). Over the years, computer languages have evolved
Page 33 of 198
Introduction to Problem Solving - CSC 102
1940’s -- Machine
Languages
1950’s -- Symbolic
Languages
Languages
available were machine languages. Each computer has its own machine
binary digits. These are so named as the machine can directly understand
the programs
3. No translation is needed.
1. Machine dependent
Page 34 of 198
Introduction to Problem Solving - CSC 102
3. Difficult to understand
code into the machine language. Hence, they are called assembly
language.
Page 35 of 198
Introduction to Problem Solving - CSC 102
3. High efficiency
2. Requires translator
5. Less efficient
assembly language.
were using. Working with symbolic languages was also very tedious
desire to improve programmer efficiency and shift the focus from the
4. Easy to maintain
5. Better readability
7. Easier to document
8. Portable
1. Needs translator
Page 37 of 198
Introduction to Problem Solving - CSC 102
4. Less efficient
#include<stdio.h>
void main() {
Table 3.1 shows the difference between machine, assembly and high-level
languages
Page 38 of 198
Introduction to Problem Solving - CSC 102
These are the programs that are used for converting the programs in one
computer.
and soon.
Page 39 of 198
Introduction to Problem Solving - CSC 102
Conclusion
This session explained computer languages and it’s main types, it further
will know which one best fits their needs. It ended the session talking
another.
Summary
language
3.5)
Page 40 of 198
Introduction to Problem Solving - CSC 102
Ruby.
SAA 3.4. A compiler translates the entire code into machine code
Page 41 of 198
Introduction to Problem Solving - CSC 102
Tutor-Marked Assignment
language
References/Further Reading
Page 42 of 198
Introduction to Problem Solving - CSC 102
Bell, Eona, and David Potter, “Computer Applications for People with
Autism,” 2001.
http://www.nas.org.uk/nas/jsp/polopoly.jsp?d=303a=3276.
http://www.howstuffworks.com/
mouse.htm.
http://www.columbia.edu/acis/history/
hollerith.html.
Stats,
2003. http://www.pcstats.com/articleview.cfm?articleid=868.
http://www.ee.ryerson.ca:8080/
~elf/abacus/history.html.
Page 43 of 198
Introduction to Problem Solving - CSC 102
Introduction
correct and precise manner, so that the machine is able to perform the
Page 44 of 198
Introduction to Problem Solving - CSC 102
the problem is defined, the steps required to solve it, must be stated
A computer cannot solve a problem on its own. One has to provide step
Page 45 of 198
Introduction to Problem Solving - CSC 102
problem.
4.2 Algorithm
Page 46 of 198
Introduction to Problem Solving - CSC 102
problem in more than one way, resulting in more than one algorithm.
accuracy and ease to modify. The most important factor in the choice
will need the least time when executed is considered the best.
specified problem.
problem.
Page 47 of 198
Introduction to Problem Solving - CSC 102
number of steps
Page 48 of 198
Introduction to Problem Solving - CSC 102
Example
Page 49 of 198
Introduction to Problem Solving - CSC 102
quantities, P, N, and R.
Simple interest = P* N*
R/100
interest.
Step 4: Stop.
triangle.
Step 4: Stop.
Page 50 of 198
Introduction to Problem Solving - CSC 102
Step 2: if (X > Y)
Big = X
else BIG = Y
Step 4: Big = Z
Step 6: Stop
Step 1: LARGE ← 0
3.1.1 then
Page 51 of 198
Introduction to Problem Solving - CSC 102
Step 5: end.
Step 1: M ← 2
Step 2: read N
4.1.1 then
4.1.1.1 go to step 7
4.2. M ← M + 1
prime”
Step 6: go to step 8
Step 8: end
Page 52 of 198
Introduction to Problem Solving - CSC 102
Step 1: PROD ← 1
Step 2: I ← 0
Step 3: read N
4.1 I ← I + 1
Step 6: end.
Step 1: SUM ← 0
Step 2: I ← 0
4.2 1 ← I + 1
Page 53 of 198
Introduction to Problem Solving - CSC 102
Step 6: END
length*width
Step 7: Stop.
Conclusion
Summary
Page 54 of 198
Introduction to Problem Solving - CSC 102
outcome 4.3)
Page 55 of 198
Introduction to Problem Solving - CSC 102
SAA4.3. Algorithm:
4.2: Increment I by 1.
Step 6: End.
Example:
Input: N = 5
Page 56 of 198
Introduction to Problem Solving - CSC 102
Algorithm execution:
Step 1: N = 5
Step 2: PROD = 1
Step 3: I = 1
Step 4:
Step 6: End
SAA4.4. Algorithm:
4.2: Increment I by 1.
Step 6: End.
Example:
Page 57 of 198
Introduction to Problem Solving - CSC 102
Input: N = 10
Algorithm execution:
Step 1: N = 10
Step 2: SUM = 0
Step 3: I = 1
Step 4:
Step 6: End
Page 58 of 198
Introduction to Problem Solving - CSC 102
Tutor-Marked Assignment
1. Define an Algorithm
number or not.
References/Further Reading
Bell, Eona, and David Potter, “Computer Applications for People with
Autism,” 2001.
http://www.nas.org.uk/nas/jsp/polopoly.jsp?d=303a=3276.
http://www.howstuffworks.com/
mouse.htm.
http://www.columbia.edu/acis/history/
Page 59 of 198
Introduction to Problem Solving - CSC 102
hollerith.html.
Stats,
2003. http://www.pcstats.com/articleview.cfm?articleid=868.
http://www.ee.ryerson.ca:8080/
~elf/abacus/history.html
Page 60 of 198
Introduction to Problem Solving - CSC 102
Introduction
Flowcharts are nothing but the graphical representation of the data or the
Page 61 of 198
Introduction to Problem Solving - CSC 102
5.1 Flowchart
used to solve a given problem and help a great deal to analyse the
algorithm.
solution to a problem.
a master chart.
Algorithm Flowchart
problem.
programs.
Organisation (ISO):
Page 63 of 198
Introduction to Problem Solving - CSC 102
these parallelograms.
arithmetic operations.
Page 64 of 198
Introduction to Problem Solving - CSC 102
the second.
Page 65 of 198
Introduction to Problem Solving - CSC 102
bx + c
Page 66 of 198
Introduction to Problem Solving - CSC 102
Page 67 of 198
Introduction to Problem Solving - CSC 102
Page 68 of 198
Introduction to Problem Solving - CSC 102
Page 69 of 198
Introduction to Problem Solving - CSC 102
Pseudocode
modify.
Example:
Read n1, n2
Sum = n1 + n2
Diff = n1 – n2
Mult = n1 * n2
Quot = n1/n2
End.
Conclusion
pseudocode and the advantages it has. Through using the above problem
Summary
Page 71 of 198
Introduction to Problem Solving - CSC 102
● Oval: The oval symbol is used to represent the start and end
flowchart.
Page 72 of 198
Introduction to Problem Solving - CSC 102
Set sum = 0
Repeat n times:
Read num
Display average
4. Read num1
Read num2
Read num3
Display maxNum
Page 73 of 198
Introduction to Problem Solving - CSC 102
5. Read number
Set factorial = 1
Set i = 1
Increment i by 1
Display factorial
Tutor-Marked Assignment
1. What is a Flowchart?
2. What is pseudocode?
References/Further Reading
Page 74 of 198
Introduction to Problem Solving - CSC 102
Bell, Eona, and David Potter, “Computer Applications for People with
Autism,” 2001.
http://www.nas.org.uk/nas/jsp/polopoly.jsp?d=303a=3276.
http://www.howstuffworks.com/
mouse.htm.
http://www.columbia.edu/acis/history/
hollerith.html.
Stats,
2003. http://www.pcstats.com/articleview.cfm?articleid=868.
http://www.ee.ryerson.ca:8080/
~elf/abacus/history.html.
Page 75 of 198
Introduction to Problem Solving - CSC 102
Introduction
applications.
This language also has the features of low -level languages and
statements in C
Page 76 of 198
Introduction to Problem Solving - CSC 102
Page 78 of 198
Introduction to Problem Solving - CSC 102
character execution
string Null
logical page
I. main()
file Test.
Page 80 of 198
Introduction to Problem Solving - CSC 102
program.
Braces: Every C program uses a pair of curly braces ({,}0. The left
hand, the right brace indicates the end of the main() function. The
arrays, functions etc., used in the C program are declared and may
comments.
perform a specific task. These are written by the user; hence the
Page 81 of 198
Introduction to Problem Solving - CSC 102
There are four basic data types in C language. They are integer
data, character data, floating point data, and double data types.
character.
classified as
Page 82 of 198
Introduction to Problem Solving - CSC 102
types.
as ‘float’ in C.
value.
numbers.
6.5 Tokens in C
1. Constants/Literals
3. Delimiters
4. Variables/Identifiers
6.5.1 Constant/Literals:
1. Numeric Constants
2. Character Constants
3. String Constants
Numeric Constants:
Integer Constants
have a decimal point. These are further divided into three types
Page 84 of 198
Introduction to Problem Solving - CSC 102
following properties:
constants: 456
-123
not permitted
following properties:
0456
-0123
+0123
permissible symbol
number system).
0x456
-0x123 0x56A
0XB78
Real Constants
Fractional Form
following characteristics:
is taken as positive.
Page 87 of 198
Introduction to Problem Solving - CSC 102
456.78
-123.56
Exponential Form
The exponential form offers a convenient way for writing very large
be written as 0.56
Mantissa part, (ii) Exponent part. Mantissa is the part of the real
Page 88 of 198
Introduction to Problem Solving - CSC 102
sa ent sa ent
0.5 E8 0.2 E
6 34 -6
In the above examples, 0.56 and 0.234 are the mantissa parts of
the first and second numbers, respectively, and 8 and -6 are the
exponent.
integer.
3E4
23e-6
Page 89 of 198
Introduction to Problem Solving - CSC 102
0.34E6
allowed
Character Constants
constant.
A character constant:
String Constants
characters.
Page 90 of 198
Introduction to Problem Solving - CSC 102
quotation marks.
quotation marks
Auto Do goto
Break Double if
Page 91 of 198
Introduction to Problem Solving - CSC 102
6.5.3 Delimiters
separate variable
statements
Page 92 of 198
Introduction to Problem Solving - CSC 102
Identifier].
Declaration of variables:
point variable
variable
6.6 Operators
data items that operators act upon are called operands. Some
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
6. Conditional Operator
7. Bitwise Operator
8. Comma Operator
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo division
Page 94 of 198
Introduction to Problem Solving - CSC 102
left hand side expression with their right hand side expression.
Operator Meaning
== Equal to
!= Not Equal to
Expression-2
Examples :
Expression Evaluate to
Page 95 of 198
Introduction to Problem Solving - CSC 102
otherwise, FALSE.
Operators Expression
|| Logical OR
! Logical NOT
The result of a logical AND operation will be true only if both operands are
True
Page 96 of 198
Introduction to Problem Solving - CSC 102
Example:
a = 5; b = 10; c = 15;
Logical OR:
Example:
a = 5; b = 10; c = 15;
Page 97 of 198
Introduction to Problem Solving - CSC 102
Logical NOT:
! ( Relational Expression )
Example:
a = 5; b = 10; c = 15
1. !( a< b ) False
data types, then the value of the expression on the right will
Page 98 of 198
Introduction to Problem Solving - CSC 102
Example:
1. I = 3.33 ( Value of I )
Multiple assignment
<exp>;
Example:
1. a = b = c = 3;
1. +=
2. -=
3. +=
4. *=
5. /=
Page 99 of 198
Introduction to Problem Solving - CSC 102
Example:
I = 10 (assume that)
1. I + = 5; I = I + 5; result = 15
2. I - = 5; I = I - 5; result = 10
3. I * = 5; I = I * 5; result = 50
4. I / = 5; I = I / 5; result = 10
++a; Pre-increment (or) a++ Post increment —a; Pre- Decrement (or) a—
Post decrement
Example 1:
Suppose a = 5 ;
Statements; Outputs
Example 2:
Suppose : a = 5 ;
Statements; Output
increment operator.
Flow Chart :
Exp-1
Page 101 of 198
Introduction to Problem Solving - CSC 102
Exp-2
Exp-3 Exit
Example:
1. a = 5 ; b = 3;
Output is :a is larger
2. a = 3; b = 3;
Output is :b is larger
Meaning Operator:
b)Bitwise OR |
Suppose b1 and b2 represent the corresponding bits within the first and
B1 B2 B1 & B2 B1 | B2 B1 ^ B2
11110
10011
01011
00000
bits (i.e., the rightmost bits) within the two operands. Will be
compared until all the bits have been compared. The results of
Example:
X 5 0101
Y 2 0010
X & Y 0 0000
X | Y 7 0111
X ^ Y 7 0111
The two bitwise shift operators are Shift left (<<) and Shift right
(>>). Each operator requires two operands. The first operand that
Example:
c = a << 3;
The value in the integer a is shifted to the left by three -bit position. The
result is assigned to c.
A = 13; c= A<<3;
1000
Example:
c = a >> 2 ;
in C.
Example :
int i, j; i= ( j =
3, j + 2 ) ;
above example, I = 5.
Size of operator:
The operator size gives the size of the data type or variable in
items.
Example :
Conclusion
features, structures, various data types, tokens, operators used for basic
Summary
● main() function
● Pair of braces {}
● User-defined functions
● Preprocessor statements
● Global declarations
They are used to store and manipulate data. Variables are declared
followed.
following:
● Arithmetic Operators:
calculations.
(division), % (modulo)
● Relational Operators:
Examples: == (equal to), != (not equal to), < (less than), >
(greater than), <= (less than or equal to), >= (greater than or
equal to)
● Logical Operators:
● Assignment Operators:
● Bitwise Operators:
level.
statements.
● Comma Operator:
They include: < (less than), > (greater than), <= (less than or equal
to), >= (greater than or equal to), == (equal to), and != (not equal
to).
Tutor-Marked Assignment
References/Further Reading
Bell, Eona, and David Potter, “Computer Applications for People with
Autism,” 2001.
http://www.nas.org.uk/nas/jsp/polopoly.jsp?d=303a=3276.
http://www.howstuffworks.com/
mouse.htm.
http://www.columbia.edu/acis/history/
hollerith.html.
Stats,
2003. http://www.pcstats.com/articleview.cfm?articleid=868.
http://www.ee.ryerson.ca:8080/
~elf/abacus/history.html.
Introduction
statements.
7.1 Expression
object.
Example:
a+b
x + y + 6.0
Page 113 of 198
Introduction to Problem Solving - CSC 102
3.14 * r * r
( a + b ) * ( a – b)
operators.
for the operators are (*, /, %) are evaluated first and (+, -)
hierarchy.
7.3 Statements
Input Output
Statements Statements
Formatted scanf() printf()
gets() puts()
getchar()
arguments.
Example:
char c;
c = getchar();
putchar()
Example:
char c;
putchar( c );
gets()
The function gets() receives the string from the standard input device.
puts()
The function puts() outputs the string to the standard output device.
printf()
The printf() function is used to print the data from the computer’s
and strings.
Example:
#include<st
dio.h> main()
char
item[20];
int partno;
float cost;
———————
Syntax: puts(s);
Example:
main()
char line[80];
gets(line);
puts(line);
scanf()
Scanf() function can be used to input the data into memory from
the standard input device. This function can be used to enter any
Example:
#include<st
dio.h> main()
char
item[20];
int partno;
float cost;
scanf(“%s %d %f”,&item,&partno,&cost);
Example:
sum = a + b + c;
tot = s1 + s2 + s3;
area = ½ * b* h;
Conclusion
Summary
outcome 7.1)
outcome 7.3)
These functions enable the program to take input from the user,
input/output operations.
from the standard input (usually the keyboard). It does not require
any arguments. The function waits for the user to enter a character
variable.
Tutor-Marked Assignment
some examples.
References/Further Reading
Bell, Eona, and David Potter, “Computer Applications for People with
Autism,” 2001.
http://www.nas.org.uk/nas/jsp/polopoly.jsp?d=303a=3276.
http://www.howstuffworks.com/
Page 123 of 198
Introduction to Problem Solving - CSC 102
mouse.htm.
http://www.columbia.edu/acis/history/
hollerith.html.
Stats,
2003. http://www.pcstats.com/articleview.cfm?articleid=868.
http://www.ee.ryerson.ca:8080/
~elf/abacus/history.html.
Introduction
The conditional expressions are mainly used for decision making. The
operations.
1. if statement.
5. Switch statement.
8.1.1 if statement
statement-1;
(or)
statement-1; statement-2;
statement-3;
……………
…………… STATEMENT-N
statements.
Example:
a=20; b=10;
if ( a > b )
depending upon the value of the expression. The general form is:
if(<exp>)
Statement-1;
Statement -2;
………….. “ SET-I”
Page 127 of 198
Introduction to Problem Solving - CSC 102
……………
Statement- n;
else
Statement1;
Statement 2;
………….. “ SET-II
……………
Statement n;
Example:
if ( a> b )
else
Syntax
if ( <exp1> )
Statement-1;
else if ( <exp2> )
Statement-2;
else if ( <exp3> )
Statement-3;
else
Statement-4;
encountered.
The final else clause will be applied if none of the expressions is true.
take.
if(exp1){
if(exp2){
statement - 1;}
else{
statement - 2;}
else{
if (exp3){
statement - 3;}
else{
statement - 4;}
expression 1 is false.
executed.
Syntax:
Switch (exp){
case constant1 :
statement-1;
break;
case constant2 :
statement-2;
break;
default :
statement-3;}
whose case label value matches the value of the exp. If none of the case
label values match the value of the exp, then the default part statements
will be executed.
If none of the case labels match the value of the exp and the
statement.
Example 1:
main()
char choice;
choice=getchar();
switch(choice= getchar())
case‘r’:
case‘R’:
printf
(“Red”);
Page 132 of 198
Introduction to Problem Solving - CSC 102
break;
case‘w’:
case‘W’:
default :
Example 2:
switch(day)
case 1:
printf
(“Monday”);
break;
———
———
Conclusion
statement, the if else statement, and the nested if else statement. The
session.
Summary
● if statement
● if-else statement
● switch statement
of statements is skipped.
Example:
if (num > 0) {
true, the statements in the "if" block are executed. If the condition
Example:
if (num % 2 == 0) {
} else {
Syntax:
if (condition1) {
} else if (condition2) {
} else {
cases efficiently.
Syntax:
switch (expression) {
case constant1:
break;
case constant2:
break;
default:
Tutor-Marked Assignment
References/Further Reading
Bell, Eona, and David Potter, “Computer Applications for People with
Autism,” 2001.
http://www.nas.org.uk/nas/jsp/polopoly.jsp?d=303a=3276.
http://www.howstuffworks.com/
mouse.htm.
http://www.columbia.edu/acis/history/
hollerith.html.
Stats,
2003. http://www.pcstats.com/articleview.cfm?articleid=868.
http://www.ee.ryerson.ca:8080/
~elf/abacus/history.html.
Introduction
Loops are among the most basic and powerful programming concepts. A
and again until no further action is required. Each time the question is
the same lines of code many times in a program can use a loop to save
loop.
false, the control is transferred out of the loop. There are three
kinds of loops in C, they are while loop, do-while loop, and for loop.
Syntax:
while (exp)
statements;
true. If the exp is false, then the control is transferred out of the
while loop.
Example:
int digit = 1;
Statements; ++digit;
The while loop is top tested i.e., it evaluates the condition before
Syntax:
do
Statement; While<exp>;
Here also, the statements will be executed as long as the exp value
is true. If the expression is false the control comes out of the loop.
Example:
-int
d=1;
do
++d;
} while (d<=5);
statements;
exit
known in advance.
Syntax:
for (exp1;exp2;exp3)
Statement –1;
Statement – 2;
} —————; FALSE
exp2 —————;
Statement - n; TRUE
exp3
Statements;
Exit
loop
exp1
start
the statement.
Format - I
) Statements;
The initial value of the variable can be assigned outside of the for
loop.
Example 1
int i = 1;
for( ; i<=10;
i++ ) printf
Format - II
for( ; exp2 ; )
Statements;
statement part.
Example 2
int i = 1;
for( ; i<=10; )
printf (“%d
\n”,i); i++;
Format - III
for( ; ; )
Statements;
In this format the three expressions are omitted. The loop itself
executed infinitely.
Example 3
int i = 1;
for ( ; i<=10; )
printf (“%d
\n”,i); i++;
inner and outer control structures may be of the same type or may
#include <stdio.h>
main()
int a,b,c,
printf (“%d”,a)
for (b=1,b<=2,b++)
print f(%d”,b)
for (c=1,c<=2,c++)
statements.
Example
break Condition
within loop
break;
sum = sum + n;
current pass through a loop. The loop does not terminate when a
next pass through the loop. The “continue” can be included within
The continue statement is used for the inverse operation of the break
statement .
Remaining part
of loop continue
Example
while (x<=100)
if (x <= 0)
continue;
the message will be displayed, and it will continue the same loop
Break Continue
while loop.
2; statement3; break; }
statement.
Syntax
appear as:
Syntax
goto<label>;
label : statements;
Example 1
#include
<stdio.h>
main();
inta,b;
if (a>b)
gotobig;
else
gotosmall;
gotostop;
gotostop; stop}
Practice Programs
Ans:
include
<stdio.h> int
a,b,c;
clrscr();
if (a<b)
if(a<c)
printf(“a is small/n”)
else
Ans.
#include <stdio.h>
int main() {
Page 154 of 198
Introduction to Problem Solving - CSC 102
int num = 5;
int i = 1;
loop:
i, num * i);
i++;
if (i <= 10) {
goto loop;
return 0;}
three numbers.
Ans:
#include <stdio.h>
int main() {
else {
return 0;}
Conclusion
Summary
SAQ9.3. Greatly state and explain the difference between for loop
variable. The loop will execute the block of code as long as the
before executing the code block and continues until the condition
becomes false.
where the loop condition never becomes false. It can lead to the
logical errors.
SAA9.3. The main differences between the "for" loop and the
increment/decrement statements.
// code to be executed
while (condition) {
// code to be executed
executed only once at the beginning of the loop. The "while" loop
requires you to initialise the loop variable before the loop starts.
the loop body. In a "while" loop, you need to manually include the
Flexibility: The "for" loop is often used when you know the exact
statement:
#include <stdio.h>
int main() {
int number;
scanf("%d", &number);
if (number <= 0) {
goto end;
printf("Countdown:\n");
printf("%d\n", number);
number--;
if (number == 5) {
goto end;
printf("Countdown complete.\n");
end:
return 0;
printed, and the program jumps to the "end" label using the "goto"
Page 161 of 198
Introduction to Problem Solving - CSC 102
way to handle the error condition and exit the program without
Tutor-Marked Assignment
6. Mention the difference between a while loop and a do... while loop.
References/Further Reading
Bell, Eona, and David Potter, “Computer Applications for People with
Autism,” 2001.
http://www.nas.org.uk/nas/jsp/polopoly.jsp?d=303a=3276.
http://www.howstuffworks.com/
mouse.htm.
http://www.columbia.edu/acis/history/
hollerith.html.
Stats,
2003. http://www.pcstats.com/articleview.cfm?articleid=868.
http://www.ee.ryerson.ca:8080/
~elf/abacus/history.html
Introduction
into parts, and then manages those parts to be solved one by one.
Programming’.
specific task.
integer type.
body of function;
——————————
——————————
——————————
return (<something>);
• Return type
• Function name
• Function body
• Return statement
the program. It can have any of the basic data types such as int,
Example
Example
output( );
read
data( )
Formal arguments
that are transferred into the function from the calling portion of
the program.
Example
————————————
————————————
————————————
return( );
Example
if ( a > b)
return(a);
body of function.
else
return(b);
functions must be called main. Execution of the program will always begin
one another. That is, one function definition can’t be embedded within
another.
from the calling portion of the program and return a single value.
multiple values.
within a function body, and we may have more than one return
return;
(or)
return (expression);
the function.
Example 1:
Ans:
# include <stdio.h>
main( )
int n;
fact( i) int I;
int j; f = 1 ;
for ( j = I; j>0; j - -) f = f * I;
return ( f ) ;
In the above program, the function with the name ‘fact’ is called by
program.
Example 2:
Ans:
f ( );
f()
{ int x,y ;
y = (x * x + 4);
Procedure
Function
the main
program.
a value.
4. In functions both
4. Global
local and global
variables
variables can be used.
cannot be used
in procedures.
procedural C, C++
pro- gramming
such as Dbase,
Foxpro.
different parameters.
Storage class
Iteration
Iteration.
Page 174 of 198
Introduction to Problem Solving - CSC 102
Categories of Functions
1. Function Prototypes
3.Recursion.
statement.
the calling portion of a program and the function does not return
block.
Example:
#include <stdio.h>
main()
void message( )
but the function does not return any value to the caller. It is any
Example
#include <stdio.h>
main()
void square(int);
scanf (“%d”,&n);
square(n);
int value;
value = n * n;
Example:
#include <stdio.h>
main()
int value;
&n);
value = square(n);
int square(int n)
int p;
p = n * n;
Page 178 of 198
Introduction to Problem Solving - CSC 102
return(p);
1. Formal Parameters
2. Actual Parameters
1. Formal Parameters
2. Actual Parameters
Example
#include <stdio.h>
int main() {
int a, b, result;
return 0;
return sum;
In this example, we have a function sum that takes two parameters: num1
and num2, both of type int. The sum function calculates the sum of the
the user to enter two numbers using printf and scanf, and then pass
Inside the sum function, the received values for num1 and num2 are used
to perform the addition operation, and the result is stored in the sum
Finally, in the main() function, we print the result obtained from the sum
are the actual values that are passed to the function when calling it.
Local Variable
“Local” variables
Example
intk,m;
————;
————;
Global Variable
of the program.
The arguments are sent to the functions, and their values are
through which arguments are passed to the called function for the
passing.
1. Call by Value
2. Call by reference.
Call by value:
calling function.
Example
#include <stdio.h>
main();
int n1,n2,x;
int cal_by_val();
N1 = 6;
N2 = 9;
X = cal_by_Val(n1,n2);
Page 183 of 198
Introduction to Problem Solving - CSC 102
/ * end of main*/
by value*/
Cal_by_val(p1,p2)
int p1,p2;
int sum;
P2* = p1;
return( sum);
=9
P1 = 8 and p2 = 72
N1 = 6 and n2 = 9
X = 15
Call by Reference:
In this method, the actual values are not passed, instead, their
the values in the called function, then the original values get
knowledge of pointers.
Example
# include <stdio.h>
main();
void sort_nums();
scanf(“%d”, &max);
sort_nums(num,max) /* Function
reference*/
for(i=0;i<max;I++)
printf(“%3d\n”,num[i]);
If ( a[i] >a[j])
Dummy = a[i];
a[i] = a[j];
a[j] = dummy;
}
Page 186 of 198
Introduction to Problem Solving - CSC 102
10.5.3 Recursion
call itself. The function that calls itself again and again, either
For Example:
main ( )
——————
——————
——————
——————
——————
——————
by invoking it with its name. But, inside the function definition f1( ),
f1( ) again.
Examples on Recursion
Example 1 :
Ans:
#include<stdio.h>
main ( )
int result, n;
result = fact(n);
fact( n )
int n;
int i ; i = 1;
if ( i = = 1)
return ( i);
else
i = i * fact ( n - 1);
return ( i );
Example 2:
recursion.
Ans:
#include<stdio.h>
#include<conio.h>
void main ( )
int i, n; clrscr ( );
getch( );
int fno;
if (n= =1)
return 1;
else
if (n= =2);
return 1;
else
else
Page 190 of 198
Introduction to Problem Solving - CSC 102
return fno;
Conclusion
and local and global variables. The recursion technique was also
Summary
1. Define a Function
4. Function prototype
6. Recursion.
outcome 10.3)
maintenance.
multiple times.
details.
values.
#include <stdio.h>
return num1;
else
return num2;
int main() {
int a, b;
return 0;
#include <stdio.h>
int factorial(int n) {
if (n == 0)
return 1;
else
int main() {
int num;
scanf("%d", &num);
return 0;
input and returns the maximum of the two numbers. The main
function prompts the user to enter two numbers, calls the findMax
the factorial of a given number. The main function prompts the user
factorial.
factorial of a number:
#include <stdio.h>
int factorial(int n) {
if (n == 0)
return 1;
else
int main() {
int num;
scanf("%d", &num);
return 0;
and then the results are propagated back through the call stack to
Tutor-Marked Assignment
7. What is Iteration
8. What is recursion
References/Further Reading
Bell, Eona, and David Potter, “Computer Applications for People with
Autism,” 2001.
http://www.nas.org.uk/nas/jsp/polopoly.jsp?d=303a=3276.
http://www.howstuffworks.com/
mouse.htm.
http://www.columbia.edu/acis/history/
hollerith.html.
Stats,
2003. http://www.pcstats.com/articleview.cfm?articleid=868.
http://www.ee.ryerson.ca:8080/
~elf/abacus/history