Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
The flowchart is a means of visually presenting the flow of data through an information processing
system, the operations performed within the system and the sequence in which they are performed.
The program flowchart describes what operations are required in what sequence to solve a given
problem. It can be compared to the blueprint of a building. A programmer prefers to draw a flowchart
prior to writing a computer program. As in the case of the drawing of a blueprint, the flowchart is
drawn according to defined rules and using standard flowchart symbols prescribed by the American
National Standard Institute (ANSI).
Pseudocode is an outline of a program, written in a form that can easily be converted into real
programming statements. Pseudocode cannot be compiled nor executed, and there is no real
formatting or syntax rules. It is simply one-step – an important one – in producing the final code. The
benefit of pseudocode is that it enables the programmer to concentrate on the algorithms without
worrying about all the syntactic details of a particular programming language.
Algorithm is a finite set of instructions that specify a sequence of operations to be carried out in order
to solve a specific problem or class of problems.
Objectives:
A. Flowcharts
A flowchart is a type of diagram that represents an algorithm or process, showing the steps as boxes of
various kinds, and their order by connecting these with arrows. This diagrammatic representation can
give a step-by-step solution to a given problem. It is common method for defining the logical steps of
flow a program by using a series of symbols to identify basic input, process and output (IPO’s) function
within a program.
Flowcharts are usually drawn using some standard symbols; however, some special symbols can also
be developed when required. Some standard symbols, which are frequently required for flowcharting
for many computer programs are shown below.
Input or output – shows input and output. Data are to be read into computer memory
from input device or data are be passed from the memory to an output device.
Decision making and branching – two alternative execution paths are possible. The
path to be followed is selected during the execution by testing whether or not the
condition specify within the outline is fulfilled.
On-page connector – shows the entry or exit point of the flowchart, joining of two
parts of program
Off-page connector - designates entry to or exit from one page when a flowchart
requires more than one page
a. In drawing a proper flowchart, all necessary requirements should be listed out in logical order.
b. The flowchart should be clear, neat and easy to follow. There should not be any room for
ambiguity in understanding the flowchart.
c. The usual direction of the flow of procedure or system is from left to right or top to bottom.
d. Only one flow line should come out from a process symbol.
e. Only one flow line should enter a decision symbol, but two or three flow lines, one for each
possible answer, should leave the decision symbol
f. Only one flow line is used in conjunction with terminal symbol.
g. If the flowchart becomes complex, it is better to use connector symbols to reduce the number of
flow lines. Avoid the intersection of flow lines if you want to make it more effective and better way
of communication.
h. Ensure that the flowchart has a logical start and finish.
i. It is useful to test the validity of the flowchart by passing through it with a simple test data.
1. Communication: Flowcharts are better way of communicating the logic of a system to all
concerned.
2. Effective analysis: With the help of flowchart, problem can be analyzed in more effective way.
3. Proper documentation: Program flowcharts serve as a good program documentation, which is
needed for various purposes.
4. Efficient Coding: The flowcharts act as a guide or blueprint during the systems analysis and
program development phase.
5. Proper Debugging: The flowchart helps in debugging process.
6. Efficient Program Maintenance: The maintenance of operating program becomes easy with the
help of flowchart. It helps the programmer to put efforts more efficiently on that part.
1. Complex logic: Sometimes, the program logic is quite complicated. In that case, flowchart becomes
complex and clumsy.
2. Alterations and Modifications: If alternations are required the flowchart may require re-drawing
completely.
3. Reproduction: As the flowchart symbols cannot be typed, reproduction of flowchart becomes a
problem.
4. The essentials of what is done can easily be lost in the technical details of how it is done.
As the name suggests, pseudocode generally does not actually obey the syntax rules of any particular
language; there is no systematic standard form, although any particular writer will generally borrow
style and syntax for example control structures from some conventional programming language.
Variable declarations are typically omitted. Function calls and blocks of code, for example code
contained within a loop, is often replaced by a one-line natural language sentence.
Depending on the writer, pseudocode may therefore vary widely in style, from a near-exact limitation
of a real programming language at one extreme, to a description approaching formatted prose at the
other.
Example 1. Design a flowchart that will accept and display a number. Write its equivalent algorithms.
Start Algorithm:
Print N
C: temperature in Celsius
Start
F: temperature Fahrenheit
Algorithm: Input C
End
Example. Construct a flowchart that will count from 1 to 10 and print each number
counted using the do-while-repetition structure.
Start
Step 1. Initialize the value of C to 0.
C=C+1
Print C
II. Research Activity: Design a flowchart and write its equivalent algorithm of the following basic
control structure:
C is a general-purpose programming language. It has been closely associated with the UNIX system
where it was developed, since both the system and most of the programs that run on it are written in
C. The language, however, is not tied to any one operating system or machine; and although it has
been called a “system programming” because it is useful for writing compilers and operating systems,
it has used usually well to write major programs in many different domains.
Late 1960s BCPL (Basic Combined Programming Language) was designed by Martin
Richards of University of Cambridge.
1970 B Language was designed by Ken Thompson of AT&T Bell Laboratories for
systems programming. (Accounts differ regarding the origins of the name “B”.
Ken Thompson credits the BCPL programming language, but he had also
created a language called “Bon” in honor of his wife Bonnie)
1972 C language was designed by Dennis Ritchie, also of AT&T Bell Laboratories, for
writing the Unix operating system. (It was named “C” because many of its
features were derived from B language.
Advantages:
Disadvantages:
C is not really a language for novices; it was designed for professional users.
There are many things that can go wrong if you’re not careful. Small typing errors can cause
unwanted effect.
C lacks much of the automatic checking found in other high level languages.
Does not support modern concepts such as object orientation and multi-threading.
The terms compiler and interpreter refers to the way in which a program is executed. In theory, any
programming language can be either compiled or interpreted, but some languages are usually
executed one way or the other. For example, Turbo BASIC is usually compiled (C interpreters have
some value as debugging aids). The way a program is executed is not defined by the language in which
it is written. Interpreters and compilers are simply sophisticated programs that operate on your
program source code.
An interpreter reads the source code of your program one line at a time and performs the specific
instructions contained in that line. A compiler reads the program and checks the whole program for
syntax errors but does not execute any of the code. The output of this process is called the object
code, which is a translation of the program source code into a form that can e directly executed by the
computer. Object code is also called binary code and machine code. Once a program is compiled, a line
of source code is no longer meaningful in the execution of the program.
Figure C.1 illustrates how an interpreter does the translation while the flowchart in Figure C.2 shows
the compilation process.
1.) Main Menu – instruct C to do something as indicated in the list of menu. It can be activated or can
be used by pressing Alt key and the 1 st letter of the menu.
Basic Menus of C:
1. File – used to load and save files, handles directories, invokes DOS and exist C.
Submenus under the File menu
a.) Load – enables the user to select a file to be opened or loaded into the editor (F3)
/* A simple C program*/
#include <stdio.h>
#include <conio.h>
main ()
{
clrscr();
printf(“This is the output of my first C program.\n”);
getch();
}
//end of Program
The characters /* and */ mark the start and end of the program comment. As with any
programming language, program comments are used to put clarificatory notes within the program
code, and ae ignored by the compiler. Program comments may be used anywhere in the program,
and may be of any length. However, the programmer should check that no part of the actual code
is in between the /* and */ characters so that it will not be ignored by the compiler. In compilers
that support both C and C++, the double forward slash (//) is also used for comments. The
comments, however, extend only from the double slash at the end of the line.
This C program starts with #include<stdio.h>. This line is called a preprocessor directive and it will
include the “standard I/O library” into your program. The standard I/O library lets you read input
from the keyboard (called “standard in”), write output to the screen (called “standard out”),
process text files stored in the disk, and so on. It is an extremely useful library. Another header file
Important Symbols:
\n – a line char used to move the cursor to the next line
‘ ‘ – single quote is used for single character/letter
“ “ – double quote is used for two or more character
{ – open curly brace signifies begin
} – close curly brace signifies end
& – address of operator
* – indicator operator/pointer
; – terminator
1. PRINTF() FUNCTION:
In C programming language, printf() function is used to print the “character, string, float,
integer, octal and hexadecimal values” onto the output screen.
We use printf() function with %d format specifier to display the value of an integer variable.
Similarly %c is used to display character, %f for float variable, %s for string variable, %lf for
double and %x for hexadecimal variable.
To generate a newline, we use “\n” in C printf() statement.
Syntax: printf(“control string codes”, argument list);
Example: printf(“Hello, %s, you are %d years old”, name, age);
Note:
C language is case sensitive. For example, printf() and scanf() are different from Printf() and
Scanf(). All characters in printf() and scanf() functions must be in lower case.
2. SCANF() FUNCTION:
In C programming language, scanf() function is used to read character, string, numeric data
from keyboard
Consider below example program where user enters a character. This value is assigned to the
variable “ch” and then displayed.
Then, user enters a string and this value is assigned to the variable “str” and then displayed.
The format specifier %d is used in scanf() statement. So that, the value entered is received as
an integer and %s for string.
Ampersand is used before variable name “ch” in scanf() statement as &ch.
KEY POINTS TO REMEMBER IN PRINTF() AND SCANF():
1. printf() is used to display the output and scanf() is used to read the inputs.
2. printf() and scanf() functions are declared in “stdio.h” header file in C library.
3. All syntax in C language including printf() and scanf() functions are case sensitive.
2. Floating point type - are used to store real numbers (real numbers have both an integer and a
fractional component)
4. void type - means no value. This is usually used to specify the type of functions which returns
nothing.
MODIFIERS IN C LANGUAGE:
It is used to alter the meaning of the base type to fit the needs of various situations more
precisely.
The amount of memory space to be allocated for a variable is derived by modifiers.
Modifiers are prefixed with basic data types to modify (either increase or decrease) the
amount of storage space allocated to a variable.
For example, storage space for int data type is 4 byte for 32 bit processor. We can increase the
range by using long int which is 8 byte. We can decrease the range by using short int which is 2
byte.
Modifiers available in C language:
1. short
2. long
3. signed
4. unsigned
where,
main – identifier
{,}, (,) – delimiter
int – keyword
x, y, total – identifier
main, {, }, (, ), int, x, y, total – tokens
2. IDENTIFIERS IN C LANGUAGE:
Each program elements in a C program are given a name called identifiers.
Names given to identify Variables, functions and arrays are examples for identifiers. eg. x is a
name given to integer variable in above program.
3. KEYWORDS IN C LANGUAGE:
Keywords are pre-defined words in a C compiler.
Each keyword is meant to perform a specific function in a C program.
Since keywords are referred names for compiler, they can’t be used as variable name.
Constant
Constants are also like normal variables. But, only difference is, their values cannot be
modified by the program once they are defined.
Constants refer to fixed values. They are also called as literals
Constants may be belonging to any of the data type.
Syntax: const data_type variable_name; (or) const data_type *variable_name;
TYPES OF C CONSTANT:
1. INTEGER
An integer constant must have at least one digit.
It must not have a decimal point.
It can either be positive or negative.
No commas or blanks are allowed within an integer constant.
If no sign precedes an integer constant, it is assumed to be positive.
The allowable range for integer constants is -32768 to 32767.
Example: int (53, 762, -478 etc )
unsigned int (5000u, 1000U etc)
long int, long long int
(483,647 2,147,483,680)
4. BACKSLASH CHARACTER
There are some characters which have special meaning in C language.
They should be preceded by backslash symbol to make use of special function of them.
Given below is the list of special characters and their purpose.
Backslash_character Meaning
\b Backspace
Variable
Variable is a named location in a memory where a program can manipulate the data. This location is
used to hold the value of the variable.
The value of the C variable may get change in the program.
C variable might be belonging to any of the data type like int, float, char etc.
data_type variable_name;
Variable declaration Example: int x, y, z; char flat, ch;
Declaration tells the compiler about data Definition allocates memory for the
type and size of the variable. variable.
Variable can be declared many times in a It can happen only one time for a
program. variable in a program.
TYPES OF OPERATORS:
1. ARITHMETIC OPERATORS IN C:
Arithmetic operators are used to perform mathematical calculations like addition, subtraction,
multiplication, division and modulus in C programs.
+ (Addition) A+B
– (Subtraction) A-B
* (multiplication) A*B
/ (Division) A/B
% (Modulus) A%B
2. ASSIGNMENT OPERATORS:
In C programs, values for the variables are assigned using assignment operators.
For example, if the value “10” is to be assigned for the variable “sum”, it can be assigned as
“sum = 10;”
There are 2 categories of assignment operators in C language. They are,
1. Simple assignment operator ( Example: = )
2. Compound assignment operators ( Example: +=, -=, *=, /=, %=, &=, ^= )
Operators Example/Description
sum = 10;
= 10 is assigned to variable sum
sum += 10;
+= This is same as sum = sum + 10
sum -= 10;
-= This is same as sum = sum – 10
sum *= 10;
*= This is same as sum = sum * 10
sum /= 10;
/= This is same as sum = sum / 10
sum %= 10;
%= This is same as sum = sum % 10
sum ^= 10;
^= This is same as sum = sum ^ 10
EXAMPLE PROGRAM FOR C ASSIGNMENT OPERATORS:
In this program, values from 0 – 9 are summed up and total “45” is displayed as output.
Assignment operators such as “=” and “+=” are used in this program to assign the values and to sum up
the values.
3. RELATIONAL OPERATORS IN C:
Relational operators are used to find the relation between two variables. i.e. to compare the values of two
variables in a C program.
Operators Example/Description
> x > y (x is greater than y)
< x < y (x is less than y)
>= x >= y (x is greater than or equal to y)
<= x <= y (x is less than or equal to y)
== x == y (x is equal to y)
!= x != y (x is not equal to y)
(x>=10)||(y>=10)
|| (logical OR) It returns true when at-least one of the condition is true
!((x>5)&&(y<5))
It reverses the state of the operand “((x>5) && (y<5))”
! (logical NOT) If “((x>5) && (y<5))” is true, logical NOT operator makes it false
In this program, operators (&&, || and !) are used to perform logical operations on the given
expressions.
&& operator – “if clause” becomes true only when both conditions (m>n and m! =0) is true.
Else, it becomes false.
|| Operator – “if clause” becomes true when any one of the condition (o>p || p!=20) is true. It
becomes false when none of the condition is true.
! Operator – It is used to reverses the state of the operand.
If the conditions (m>n && m!=0) is true, true (1) is returned. This value is inverted by “!”
operator.
So, “! (m>n and m! =0)” returns false (0).
5. BIT WISE OPERATORS:
These operators are used to perform bit operations. Decimal values are converted into binary
values which are the sequence of bits and bit wise operators work on these bits.
Bit wise operators in C language are & (bitwise AND), | (bitwise OR), ~ (bitwise NOT), ^ (XOR),
<< (left shift) and >> (right shift).
Consider x=40 and y=80. Binary form of these values are given below.
x = 00101000
y= 01010000
NOTE:
Bit wise NOT : Value of 40 in binary is 00000000000000000000000000000000
00000000000000000010100000000000. So, all 0’s are converted into 1’s in bit wise NOT
operation.
Bit wise left shift and right shift : In left shift operation “x << 1 “, 1 means that the bits will be
left shifted by one place. If we use it as “x << 2 “, then, it means that the bits will be left shifted by
2 places.
In above example, if A is greater than 100, 0 is returned else 1 is returned. This is equal to if
else conditional statements.
8. SPECIAL OPERATORS:
Below are some of the special operators that the C programming language offers.
Operators Description
In decision control statements (if-else and nested if), group of statements are executed when
condition is true. If condition is false, then else part statements are executed.
3 types of decision making control statements:
1. if statements
2. if else statements
3. nested if statements
“IF”, “ELSE” AND “NESTED IF” DECISION CONTROL STATEMENTS:
Syntax for each C decision control statements are given in below table with description.
Decision control Syntax/Description
statements
if Syntax:
if (condition)
if…else Syntax:
if (condition)
{ Statement1; Statement2; }
else
{ Statement3; Statement4; }Description:
In these type of statements, group of statements are executed
when condition is true. If condition is false, then else part
statements are executed.
nested if Syntax:
if (condition1){ Statement1; }
else_if(condition2)
{ Statement2; }
else Statement 3;Description:
If condition 1 is false, then condition 2 is checked and statements
are executed if it is true. If condition 2 also gets failure, then else
part is executed.
EXAMPLE PROGRAM FOR IF STATEMENT:
In C if else control statement, group of statements are executed when condition is true. If condition is
false, then else part statements are executed.
In “nested if” control statement, if condition 1 is false, then condition 2 is checked and
statements are executed if it is true.
If condition 2 also gets failure, then else part is executed.
Syntax for each loop control statements are given in below table with description.
Loop Name Syntax
do while do { statements; }
while (condition);where,
condition might be a>5, i<10
In for loop control statement, loop is executed until condition becomes false.
In while loop control statement, loop is executed until condition becomes false.
In do..while loop control statement, while loop is executed irrespective of the condition for first time.
Then 2nd time onwards, loop is executed until condition becomes false.
1. BREAK STATEMENT:
Break statement is used to terminate the while loops, switch case loops and for loops from the
subsequent execution.
Syntax: break;
3. CONTINUE STATEMENT:
Continue statement is used to continue the next iteration of for loop, while loop and do-while
loops. So, the remaining statements are skipped within the loop for that particular iteration.
Syntax : continue;
EXAMPLE PROGRAM FOR CONTINUE STATEMENT IN C:
4. GOTO STATEMENT:
goto statements is used to transfer the normal flow of a program to the specified label in the
program.
Array in C
Array is a collection of variables belongings to the same data type. You can store group of data of same
data type in an array.
a) Array might be belonging to any of the data types
b) Array size must be a constant value.
Character array example:
char str[10];
char str[10]={‘H’,‘a’,‘i’};
(or)
char str[0] = ‘H’;
char str[1] = ‘a’;
char str[2] = ‘i;
1.2. State whether each of the following is true or false. If false, explain why.
a) When the printf function is called, it always begins printing at the beginning of a new line.
b) Comments cause the computer to print the text enclosed between /* and */ on the screen when the
program is executed.
c) The escape sequence \n when used in a printf format control string causes the cursor to position to the
beginning of the next line on the screen.
d) All variables must be defined before they are used.
e) All variables must be given a type when they are defined.
f) C considers the variables number and NuMbEr to be identical.
g) Definitions can appear anywhere in the body of a function.
h) All arguments following the format control string in a printf function must be preceded by an ampersand
(&).
i) The remainder operator (%) can be used only with integer operands.
j) The arithmetic operators *, /, %, + and – all have the same level of precedence.
1.5. Using the statements you wrote in Exercise 2.4, write a complete program that calculates the product of
three numbers.
1.6. Write a program that determines if the input number is ODD or EVEN number.
3.1. Write four different C statements that each add 1 to integer variable m.
3.4. Combine the statements that you wrote in Exercise 3.3 into a program that calculates the sum of the
integers from 1 to 10. Use the while statement to loop through the calculation and increment statements.
The loop should terminate the value of x becomes 11.
3.5. Determine the values of variables product and x after the following calculation is performed. Assume that
product and x each have the value 5 when the statement begins executing.
product *= x++;
3.7. Write a C program that uses the statements in Exercise 3.6 to calculate x raised to the y power. The program
should have a while repetition control statement.
3.8. Identify and correct the errors in each of the following:
a) while (c <= 5) {
product * = c;
++c;
b) scanf(“%.4f”, &value);
c) if (gender ==1)
printf(“Woman\n”);
else;
printf(“Man\n”);
3.9. What is wrong with the following while repetition statement (assume z has value 100), which is supposed to
calculate the sum of the integers from 100 down to 1:
while (z >= 0)
sum += z;
4.2. State whether the following are true or false. If the answer is false, explain why.
a.) The default case is required in the switch selection statement.
b.) The break statement is required in the default case of a switch election statement.
c.) The expression (x > y && a < b) is true if either x > y is true or a < b is true.
d.) The expression containing the || operator is true if either or both of its operands is true.
e.) The specific element in an array is accessed by an index.
4.3. Find the error in each of the following code segments and explain how to correct it.
a) x = 1;
while (x <= 10);
x++;
}
b) for (y = .1; y != 1.0; y += .1)
printf(“%f\n”, y);
c) switch (n) {
case 1:
printf(“The number is 1\n”);
case 2:
printf(“The number is 2|n”);
default:
printf(“The number is not 1 or 2\n”);
break:
}
d) The following code should print the values 1 to 10.
n = 1;
while (n < 10)
printf(“%d”, n++);