You are on page 1of 7

Algorithm

It is a list of instructions specifying a precise description of a step by step process that


terminates after a finite number of steps for solving an algorithm problem producing the
correct answer in the end.
It is a recipe for solving problems.
A finite set of an instruction that specifies a sequence of operation to be carried out in
order to solve a specific problem.
An unambiguous procedure specifying a finite number of steps to be taken.
Methods of Specifying Algorithm
1. Pseudocode - specifies the steps of algorithm using essentially natural language of
superimposed control structure.
2. Flowchart - a traditional graphical tool with standardized symbols. Show the sequence of
steps in an algorithm.
C. Properties of Algorithm
1. Finiteness - there is an exact number of steps to be taken and has an end.
2. Absence of Ambiguity - means that every instruction is precisely described and clearly
specified.
3. Sequence of Execution - instructions are performed from top to bottom.
4. Input and Output - defined the unknowns of the problem is specified and with the expected
outcome.
5. Effectiveness - the solution prescribed is guaranteed to give a correct answer and that the
specified process is faithfully carried out.
6. Scope Definition - applies to a specific problem or class of problem.
Steps in Program Development
1. State the problem clearly- a problem cannot be solved correctly unless it is being understood.
2. Plan and Write the Logical Order of Instructions - the computer follows the direction exactly at
the given sequence.
3. Code the Program - write the programming statements in the desired language.
4. Enter the program into the computer - key in or type the statement into the computer.
5. Run and Debug the program - check if you have the desired output; if not, trace the possible
error.
Flowcharting Guidelines
1.
2.
3.
4.

The flowchart should flow from top to bottom


If the chart becomes complex, utilize connecting blocks
Avoid intersecting flow lines
Use meaningful description in the symbol

SYMBOL

NAME

DESCRIPTION

TERMINAL

Defines the starting and ending


point of a flowchart.

INITIALIZATION

The preparation or initialization of


memory
space
for
data

processing.
INPUT/OUTPUT

PROCESS

The
inputting
of
data
for
processing, and printing out of
processed data.
Manipulation of data(assignments
and mathematical computations)

FLOW LINES

Defines logical sequence of the


program. Its points to the ext
symbol to be performed

ON-PAGE CONNECTOR

Connects to the flowchart to


avoid spaghetti connection on the
same page

OFF-PAGE CONNECTOR

Connects
the
flowchart
on
different page to avoid spaghetti
connection

DECISION

Process
conditions
using
relational operators.
Used for
trapping and filtering data.

C Programming: Introduction

C is a programming language designed by Dennis Ritchie in 1972 at AT&T Bell


Laboratories.
C is a simple programming language with few keywords and a relatively simple to
understand syntax.
Although C was developed as a systems programming language, today it is one of the
standard "workhorse" programming languages and is commonly used to develop
commercial programs.
C is also useless. C itself has no input/output commands, doesn't have support for strings
as a fundamental (atomic) data type. No useful math functions built in.
Because C is useless by itself, it requires the use of libraries. This increases the
complexity of C. The issue of standard libraries is resolved through the use of ANSI
libraries and other methods.
Case Sensitive

How to Execute a Program in C

Enter each line of the source code into the memory, and save it on disk as a source file,
using an editor, or a word-processor.

Compile the source code. The compiler will read the entire' source code and convert it to
an object code

***0bject code - is the translation of the source code of a program to machine


language, which the computer can read and execute directly. Object code is the
input to the linker.

After compiling, the compiler displays syntax errors in the monitor.


editing and re-saving it using editor.

The compiler then saves its machine-language translation as an object code, when the
source program is free of syntax errors.

The Linker will combine separate compiled functions together into one program. It
combines the functions in the standard C library with the code you wrote. The output of
the Linker is an executable program.

Correct errors by

Use the loader program to place the executable file back in the memory, ready for execution.
The C Environment
All operations of Turbo C are selected from the main menu. After an operation is completed, the
main menu is redisplayed to allow you to choose another operation. The operations displayed on
the main menu are:
FILE - Load or save C source code files. This selection also allows you to display a directory,
execute a DOS command, or exit from Turbo C.
EDIT - Start the editor. The contents of the currently specified word file are displayed. It is
where you edit the source program.
RUN - Run the most recently compiled program. If the current file has not yet been compiled,
Turbo C will automatically compile the program before it runs it.
COMPILE - Compile the currently specified file.
PROJECT - Allows you to make one large program from several C source-code files.
OPTIONS - Allows you to specify technical aspects of the Turbo C compiler, linker or
environment.
DEBUG - Aids in debugging your Turbo C program.
Path and Filename - indicates the filename of the current source program that is loaded on the
edit window. It also indicates the drive where it was loaded or the drive
where it will be saved.
Trace Window - this window is usually used to trace the value of a certain variable during
program tracing. It also serves as a message window during compiling. It
displays all the error messages, warnings and the type of error that occurred.
Status Bar
mode.

- indicates if the Num Lock, Caps Lock, and Scroll Lock keys are in the on or off

Function Menus pressing the function keys accesses these menus:


Fl - Help

F5 - Zoom
F6 - Switch Window
F7 - Trace Window
F8 - Step
F9 - Make EXE File
F10-Access the Menu Bar
Cursor Locator - tells you the current location of the cursor and the line number.
Comment
Comments are arbitrary strings placed between the delimiters /* */. Any line of text placed
between /* and */ is ignored by the compiler. It is commonly used for program documentation.
Here is an example of placing comments in a C program:
/******
****
A very long comment
****
can be placed using
****
this format
******/

The main functions consist of punctuation and special symbols (*, =). Commas
separate items in a list; a semicolon appears at the end of several lines, and braces
{[,]} mark the beginning and end of the body of a function.
General- Form of C program
preprocessor directives <header file>, constant declarations
global variable declarations;
main()
{4
local variables;
statement sequence;
}
F1()
{
local variables;
statement sequence;
}
fN()
{
local variables;
statement sequence;
}
The two most common preprocessor directives are:
a) #include. The directive like #include gives a program access to a library that causes the
preprocessor to insert definitions from a standard header file into a program before compilation.
The directives notify the preprocessor that some names used in the program like scanf() or
printf() are found in the standard header file <stdio.h>.

b) #define. Data values that never change or change rarely should be given names using
#define. Executing the C program cannot change the value of a name defined as a constant
macro.
The Standard Library Functions

stdio.h: I/O functions:


o

getchar() returns the next character typed on the keyboard.

putchar() outputs a single character to the screen.

printf() - ouput

scanf() - input

string.h: String functions


o

strcat() concatenates a copy of str2 to str1

strcmp() compares two strings

strcpy() copys contents of str2 to str1

ctype.h: Character functions


o

isdigit() returns non-0 if arg is digit 0 to 9

isalpha() returns non-0 if arg is a letter of the alphabet

isalnum() returns non-0 if arg is a letter or digit

islower() returns non-0 if arg is lowercase letter

isupper() returns non-0 if arg is uppercase letter

math.h: Mathematics functions


o

acos() returns arc cosine of arg

asin() returns arc sine of arg

atan() returns arc tangent of arg

cos() returns cosine of arg

exp() returns natural logarithm e

fabs() returns absolute value of num

sqrt() returns square root of num

time.h: Time and Date functions


o

time() returns current calender time of system

difftime() returns difference in secs between two times

clock() returns number of system clock cycles since program execution

stdlib.h:Miscellaneous functions
o

malloc() provides dynamic memory allocation, covered in future sections

rand() as already described previously

srand() used to set the starting point for rand()

Basic Data Types of C


Type

Format

Meaning

(Example(s))

int
1000000)

%d or %i

a whole number

(3,7,

float

%f

a number with decimal point(5.8,9.0,-5.6789, +0.0032)

char

%c

a single letter, symbol, or number (B, r, *, 3)


enclosed within two single quotes

char
Love)

%s

series of characters

(ms., Hi, 143, I

double

%f

for bigger or larger number with


a decimal point

(123456789.123)

+8,

-9,

Variables can be declared as either local variables which can be used inside the function it has
been declared in or global variables which are known throughout the entire program.
Variable names:

should be lowercase for local variables

should be UPPERCASE for symbolic constants (to be discussed later)

only the first 31 characters of a variables name are significant

must begin with a letter or _ (under score) character

Basic Operators of C
Arithmetic operators
Symbol

Meaning

+
*
/
%

Addition
Subtraction
Multiplication
Division
Modulus/Remainder

Relational Operators
Symbols

Meaning

>
>=
<
<=
==
!=

greater than
greater than or equal to
less than
less than or equal to
equal to
not equal to

Logical Operators
Symbol

Meaning

&&
||
!

and
or
not

Other Assignment Operator


Symbols
+=
-=
*=
/=
%=
-++

Meaning
plus equal
minus equal
multiply equal
divide equal
modulus equal
minus minus (decrement)
plus plus (increment)

Examples:
The following equation below can be written in compressed form:
Normal form
i=i+2
p=p*b
r=r/4
j=j-3
n=n%2

compressed form
i+=2
p*=b
r/=4
j-=3
n%=2

You might also like