C_Tutorial_Day_1
C_Tutorial_Day_1
1. Problem definition
A problem definition involves the clear identification of the problem in terms of available input parameters and desired
solutions.
What is an algorithm?
An algorithm is a step-by-step procedure for solving a stated problem using a reasonable amount of time and storage.
Every problem has some input information and some desired results to be obtained. The important aspect is to determine
if the information available is really sufficient to solve the problem at hand, and if it is so what are the procedures that
should be applied to the given information or data so that the required result can be obtained. The development of a
proper procedure to solve the problem is called an algorithm.
The operations that can be included in an algorithm are constrained by the possibility of a computer carrying them out.
Each operation must be unambiguous about the process to solve the problem. The steps in the process must be effective
enough to be theoretically done in a finite amount of time and memory. An algorithm terminates in a reasonably short
time.
What is pseudo code?
Pseudo code consists of short, English phrases used to explain specific tasks within a program's algorithm. Pseudo code
should not include keywords in any specific computer language. It should be written as a list of consecutive phrases.
You should not use flowcharting symbols but you can draw arrows to show looping processes. Indentation can also be
used in pseudo code to show the logic.
Sample Algorithm
What is a flowchart?
A flowchart is a pictorial representation of the steps that are involved in the procedure. A flowchart also shows the logi-
cal sequence in which the steps are to be performed. A flowchart consists of boxes called the symbols and arrows called
the flow lines. The box depicts the process and the flow line indicates the next step to be performed.
Terminal Box
This symbol is used to indicate the beginning or the end of a flowchart. When this symbol is used for START, no flow
lines can enter it. Only one flow line can leave this box. When the terminal box is used for STOP, no flow lines can
leave this box and any number of flow lines can enter this box. There can be only one START symbol in each flowchart.
However there can be more than one STOP symbol in a flowchart.
Process Box
The shape of the processing box is a rectangle. This symbol represents one or more instructions that perform a
processing function in a program. Examples of processing functions are addition, subtraction, multiplication, division or
moving data to storage or assigning a value.
Input/Output Box
The input/output box is represented by a parallelogram. This symbol indicates any function of an input/output device
such as keyboard or printer. An input/output box makes data available for processing or displays the result of processing
on the screen.
Decision Box
The shape of a decision box is a rhombus. This box is used when two quantities need to be compared. The decision box
is also used for a testing condition. A decision box results in two alternative answers to the condition: true or false. The
equality, less than, less than or equal to, greater than, greater than or equal to, and not equal to operators can be used in
the decision box.
Programming language
A programming language is a set of symbols, grammars and rules with the help of which one is able to translate algo-
rithms to programs that will be executed by the computer.
High level language is a language that supports the human and the application sides of the programming (typical fea-
tures: ability to logic structuring of the algorithm, cross-platform independence)
So called high level computer languages are the solution. A language is a machine-independent way to specify the se-
quence of operations necessary to accomplish a task. A language can be designed to be express in a concise way, a
common sequence of operations. A line in a high level language can execute powerful operations, and correspond to
tens, or hundreds, of instructions at the machine level.
In a pure assembly language, each statement produces exactly one machine instruction (one-to-one correspondence be-
tween machine instructions and statements in assembly language). So an on-line assembly language will produce an n-
word machine language program. Assembly language which uses mnemonic codes is easier than machine language us-
ing binary or hexadecimal codes. It is easier to remember ADD, SUB, MUL, or DIV, than their corresponding numerical
values in machine language. Assembly language uses symbolic names for memory locations while machine language
needs numerical values. The assembly language can directly test if there is an overflow bit while a higher level language
cannot. An assembly language can only run on one family of machines (each machine has its own assembly language)
while a higher level language can run on many machines.
Machine level language is a language that supports the machine side of the programming or does not provide human side
of the programming. It consists of (binary) zeros and ones.
What is "compilation"?
The compiler is the tool to convert a program written in a high level language into the sequence of machine instructions
required by a specific computer to accomplish the task. Users typically control details of the operation of compilers by
means of options supplied on the command line, or directives embedded in the program source, but they seldom need to
examine the resulting machine language code. This process of converting high level language to machine level is called
compilation.
What is "linking"?
After all of the files are compiled, they must be "merged together" to produce a single executable file that you use to run
the program. In C, most compiled programs produce results only with the help of some standard programs, known as li-
brary files, that reside in the computer. This process is called linking. The result obtained after linking is called the ex-
ecutable file.
What is "loading"?
After the files are compiled and linked the executable is loaded in the the computer's memory for executing by the load-
er. This process is called Loading.
Compilation Errors
Syntax errors -- The compiler cannot understand your program because it does not follow the syntax of your program.
Common syntax errors include:
Missing or misplaced ; or }
Missing return type for a procedure
Missing or duplicate variable declarations
Type errors -- These errors include type mismatch when you assign a value to a variable and type mismatch between ac-
tual and formal parameters.
Runtime Errors
Output errors -- These errors result when the program runs but produces an incorrect result. An output error indicates an
error in the meaning or logic of the program.
Exceptions -- These errors occur when the program terminates abnormally. Examples include division by zero and out of
memory. Exceptions indicate an error in the semantics or the logic of a program.
Non-termination errors -- These errors occur when the program does not terminate as expected, but continues running
endlessly.
Testing and debugging are necessary stages in the development cycle, and they are best incorporated early in the cycle.
Thoroughly testing and debugging individual components makes testing and debugging integrated applications much
easier.
What is Documentation?
The written text and comments that make a program easier for others to understand, use and modify. Comments are ig-
nored by the compiler in the compilation process. It provides information about the steps and procedures.
Algorithms
To exchange the values of two variables using a temporary variable
Step 1 :
Input the two variables whose values have to be swapped: A, B
Step 2 :
Initialise C = 0
Step 3:
CA
AB
BC
Step 4 :
Output the values of variables A, B.