PSP - Module 1
PSP - Module 1
Characteristics of an Algorithm
Clear and Unambiguous: Algorithm should be clear and unambiguous. Each of its
steps should be clear in all aspects and must lead to only one meaning.
Well-Defined Inputs: If an algorithm says to take inputs, it should be well-defined
inputs.
Well-Defined Outputs: The algorithm must clearly define what output will be
yielded and it should be well-defined as well.
Finite-ness: The algorithm must be finite, i.e., it should not end up in an infinite loop
or similar.
Feasible: The algorithm must be simple, generic and practical, such that it can be
executed upon with the available resources. It must not contain some future
technology, or anything.
Language Independent: The Algorithm designed must be language-independent, i.e.,
it must be just plain instructions that can be implemented in any language.
Advantages of Algorithms:
It is easy to understand.
Algorithm is a step-wise representation of a solution to a given problem.
In Algorithm the problem is broken down into smaller pieces or steps hence, it is
easier for the programmer to convert it into an actual program.
Terminal: The oval symbol indicates Start, Stop and Halt in a program’s logic flow.
Decision Diamond symbol represents a decision point. Decision based operations such
as yes/no question or true/false are indicated by diamond in flowchart.
Connectors: Whenever flowchart becomes complex or it spreads over more than one
page, it is useful to use connectors to avoid any confusions. It is represented by a
circle.
Flow lines: Flow lines indicate the exact sequence in which instructions are executed.
Arrows represent the direction of flow of control and relationship among different
symbols of flowchart.
Advantages of Flowchart:
Problems:
Programming is the process of creating a set of instructions that tell a computer how to
perform a task. Programming can be done using a variety of computer "languages," such as
C,C++,JAVA etc.
Syntax refers to the spelling and grammar of a programming language. Computers are
inflexible machines that understand what you type only if you type it in the exact form that the
computer expects. The expected form is called the syntax.
Program development is the process of creating application programs. Program
development life cycle (PDLC) The process containing the five phases of program
development: analysing, designing, coding, debugging and testing, and implementing and
maintaining application software.
The following are six steps in the Program Development Life Cycle:
1. Analyze the problem. The computer user must figure out the problem, then decide how to
resolve the problem.
2. Design the program. A flow chart is important to use during this step of the PDLC. This is
a visual diagram of the flow containing the program.
3. Code the program. This is using the language of programming to write the lines of code.
The code is called the source code.
4. Debug the program. The bugs are important to find because this is known as errors in a
program.
5. Formalize the solution. One must run the program to make sure there are no syntax and
logic errors. ie testing the program.
6. Document and maintain the program. This step is the final step of gathering everything
together. Internal documentation is involved in this step because it explains the reasoning one
might have made a change in the program or how to write a program.
Programming Languages:
High-level language
Translators :
Interpreter − An interpreter is a program that executes the programming code directly rather
than only translating it into another format. It translates and executes programming language
statements one by one.
#include<stdio.h>
main()
printf(“WELCOME”);
Save – CONTROL+O
Exit – CONTROL+X
Execute - ./a.out
C Program Structure
#include<stdio.h>
main()
{
printf("Hello, World!\n");
}
/* Comments */ Comments are a way of explaining what makes a program. The
compiler ignores comments and used by others to understand the
code.
Braces Two curly brackets "{...}" are used to group all statements.
The Documentation section usually contains the collection of comment lines giving the
name of the program, author's or programmer's name and few other details. The second part is
the link-section which instructs the compiler to connect to the various functions from the
system library. The Definition section describes all the symbolic-constants. The global
declaration section is used to define those variables that are used globally within the entire
program and is used in more than one function. This section also declares all the user-defined
functions. Then comes the main(). All C programs must have a main() which contains two
parts:
Declaration part
Execution part
The declaration part is used to declare all variables that will be used within the program.
There needs to be at least one statement in the executable part, and these two parts are
declared within the opening and closing curly braces of the main(). The execution of the
program begins at the opening brace '{' and ends with the closing brace '}'. Also, it has to be
noted that all the statements of these two parts need to be terminated with a semi-colon. The
sub-program section deals with all user-defined functions that are called from the main().
These user-defined functions are declared and usually defined after the main() function.
C Identifiers
C identifiers represent the name in the C program, for example, variables. An identifier
can be composed of letters such as uppercase, lowercase letters, underscore, digits, but the
starting letter should be either an alphabet or an underscore.
C Keywords
Keywords are predefined, reserved words used in programming that have special
meanings to the compiler. Keywords are part of the syntax and they cannot be used as an
identifier.
do if static while
Following are the examples of some very common data types used in C:
o char: The most basic data type in C. It stores a single character and requires
a single byte of memory in almost all compilers.
o int: As the name suggests, an int variable is used to store an integer.
o float: It is used to store decimal numbers (numbers with floating point value)
with single precision.
o double: It is used to store decimal numbers (numbers with floating point
value) with double precision.
unsignedint 4 0to4,294,967,295 %u
float 4 %f
double 8 %lf
Output
The area of triangle :400.000000
C - Variables
A variable is nothing but a name given to a storage area that our programs can
manipulate. The name of a variable can be composed of letters, digits, and the underscore
character. It must begin with either a letter or an underscore. Upper and lowercase letters are
distinct because C is case-sensitive.
A variable definition tells the compiler where and how much storage to create for the
variable. A variable definition specifies a data type and contains a list of one or more
variables of that type as follows −
type variable_list;
Here, type must be a valid C data type including char, w_char, int, float, double, bool,
or any user-defined object; and variable_list may consist of one or more identifier names
separated by commas. Some valid declarations are shown here −
int i, j, k;
char c, ch;
float f, salary;
double d;
Variable Declaration in C
A variable declaration provides assurance to the compiler that there exists a variable
with the given type and name so that the compiler can proceed for further compilation
without requiring the complete detail about the variable.
#include <stdio.h>
int main () {
/* variable declaration: */
int a, b;
int c;
float f;
/* actual initialization */
a = 10;
b = 20;
c = a + b;
printf("value of c : %d \n", c);
f = 70.0/3.0;
printf("value of f : %f \n", f);
return 0;
}
Output
value of c : 30
value of f : 23.333334
C Operators
An operator is a symbol that tells the compiler to perform specific mathematical or logical
functions. C language is rich in built-in operators and provides the following types of
operators
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Conditional Operators
Increment or Decrement Operators
Arithmetic Operators
The following table shows all the arithmetic operators supported by the C language. Assume
variable A holds 10 and variable B holds 20 then –
Operat Description Example
or
Relational Operators
The following table shows all the relational operators supported by C. Assume variable A
holds 10 and variable B holds 20 then −
Operat Description Exampl
or e
== Checks if the values of two operands are equal or not. If yes, then (A
the condition becomes true. == B)
is not
true.
!= Checks if the values of two operands are equal or not. If the values (A !=
are not equal, then the condition becomes true. B)
is true.
> Checks if the value of left operand is greater than the value of right (A >
operand. If yes, then the condition becomes true. B)
is not
true.
< Checks if the value of left operand is less than the value of right (A <
operand. If yes, then the condition becomes true. B)
is true.
>= Checks if the value of left operand is greater than or equal to the (A
value of right operand. If yes, then the condition becomes true. >= B)
is not
true.
<= Checks if the value of left operand is less than or equal to the (A
value of right operand. If yes, then the condition becomes true. <= B)
is
true.
Logical Operators
Following table shows all the logical operators supported by C language. Assume variable A
holds 1 and variable B holds 0, then –
&& Called Logical AND operator. If both the operands are non-zero, then (A
the condition becomes true. &&
B) is
false.
|| Called Logical OR Operator. If any of the two operands is non- (A ||
zero, then the condition becomes true. B) is
true.
! Called Logical NOT Operator. It is used to reverse the logical state of !(A
its operand. If a condition is true, then Logical NOT operator will &&
make it false. B) is
true.
Bitwise Operators
Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |, and
^ is as follows −
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Assume variable 'A' holds 60 and variable 'B' holds 13, then −
Operato Description Example
r
& Binary AND Operator copies a bit to the (A & B) = 12, i.e., 0000 1100
result if it exists in both operands.
^ Binary XOR Operator copies the bit if it is (A ^ B) = 49, i.e., 0011 0001
set in one operand but not both.
>> Binary Right Shift Operator. The left A >> 2 = 15 i.e., 0000 1111
operands value is moved right by the
number of bits specified by the right
operand.
Assignment Operators
The following table lists the assignment operators supported by the C language −
Operator Description Example
Conditional Operator
?: Conditional Expression. If Condition is true ? then value X : otherwise value Y
10 + 20 * 30
10 + 20 * 30 is calculated as 10 + (20 * 30)
100 + 200 / 10 - 3 * 10
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
C Expressions
An expression is a formula in which operands are linked to each other by the use of
operators to compute a value. An operand can be a function reference, a variable, an array
element or a constant.
C - Type Casting
Converting one datatype into another is known as type casting or, type-conversion.
You can convert the values from one type to another explicitly using the cast operator as
follows −
(type_name) expression
#include <stdio.h>
main() {
The cast operator has precedence over division, so the value of sum is first converted to
type float and finally it gets divided by count yielding a float value.
scanf()
The scanf() method, in C, reads the value from the console as per the type specified.
Syntax:
scanf(“%X”, &variableOfXType);
where %X is the format specifier in C. It is a way to tell the compiler what type of data is in
a variable and & is the address operator in C, which tells the compiler to change the real value
of this varia ble, stored at this address in the memory.
printf()
The printf() method, in C, prints the value passed as the parameter to it, on the console
screen.
Syntax:
printf(“%X”, variableOfXType);
where %X is the format specifier in C. It is a way to tell the compiler what type of data is in
a variable and & is the address operator in C, which tells the compiler to change the real
value of this varia ble, stored at this address in the memory.
• Float:
Input: scanf("%f", &floatVariable);
Output: printf("%f", floatVariable);
• Character:
Input: scanf("%c", &charVariable);
Output: printf("%c", charVariable);
Basic Programs
1. Display a message.
2. a=10, b=20 Find a+b;
3. Print 100 five times.
4. a=10, b=4 Find the Quotient and Remainder of a/b.
5. Read a and b then find (a+b)/2.
6. Read a and b then find (a+b)2.
7. Read a and b then perform arithmetic operations such as addition, subtraction,
multiplication and division.