CC102 Lesson Proper For Week 1 To 5
CC102 Lesson Proper For Week 1 To 5
CC102 Lesson Proper For Week 1 To 5
Yes, computers
are fast. They are able to calculate complex
computations in mere seconds. However, being
fast does not make computers intelligent. It only
makes them highspeed followers, nothing more.
In reality, computers are only as good as
instructions or programs you feed them.
This is a very important point to consider in
programming.
What it represents
Symbols
- Used to signify the beginning
and end of the flowchart.
Terminal
Preparation/Initialization
Input / Output
Processing
- Signifies any decisions that are
to be done.
- “Two alternative execution paths
are possible. The path to be
followed is selected during the
execution by testing whether or
not the condition specified within
the outline is fulfilled”
(SWAZ89).
Decision
Off-page connector
Flowlines
- Used as an input/output symbol
specifically for data moving in
and out of secondary storage.
Secondary Storage
3. Do a structured walkthrough.
1. Design a flowchart that will accept and display a number. Write its equivalent
algorithm
Algorithm Start
Print N
End
Figure 1.2
Compilations
Compile Object code
Source code
(Machine
language)
(Programming
language
During compilation, some program errors (called bugs) may be encountered, causing
the program’s failure to produce the desired output.
The programmer needs to go through the process of
detecting, locating, and removing these errors (also
called debugging) in order for the program to be able
to solve the problem at hand. There are three types of
program errors: syntax error, run-time error, and logic
error.
Syntax error is the most common error that you will
encounter in programming. It is caused by
typographical errors (such as misspelled words or
incorrect symbols) and/or failure to strictly follow the
syntax of the programming language (such as omitted
punctuation or unmatched parenthesis).
Run-time error is a software error that occurs while a program is being executed, as
detected by a compiler or other supervisory programs.
Mostly, run-time errors occur in numeric calculations,
such as dividing a number by zero (resulting to “divide
error”); in evaluating an expression that would result in
an illegal data, such as assigning to an integer type
variable a value greater than 32 767 (which results to an “overflow”); or in giving an
improper input, such as when a user entered a character data where an integer vale is
expected.
Logic errors are caused by incorrect use of control
structures. These errors are much more difficult to
detect than syntax errors because the computer
cannot tell an error in logic in a program.
3. Run real data
After desk-checking and
debugging, the
program, then needs to be tested with real
data, called beta-testing. There is a need to
also test a program with bad data—data that is
faulty, incomplete, or in overwhelming
quantities—to see if this can make the system
crash. In programming, programmers should
consider the fact that there could be users who are far more heavy-handed,
ignorant, and careless.
The Fifth Step: Program Documentation and
Maintenance Documentation is the set of
instructions shipped with a program or a piece of
hardware. Documentation usually includes necessary
instructions on the use and maintenance of the
product. It is not just an end-stage process of
programming. In fact, it needs to be done throughout
the entire programming steps. It is needed for people
—users, computer operators, and programmers—who
will be using the program or who will be involved with
the program in the future.
Program documentation and maintenance consists of the following steps:
1. Prepare user documentation.
The printed, on disk, or online manuals in commercial software
packages are user documentation which can help nonprogrammers use the
software.
2. Prepare operator documentation.
The people who run computers are called computer operators. They
are not necessarily programmers; hence, they also need information (the
documentation) on what to do when the program malfunctions.
3. Prepare program documentation.
Program documentation consists of written, graphic, and electronic
Descriptions of what a program is and how to use it. This makes the program
understandable even at a later time. The variables are defined and the
comments are made to be informative. New programmers can maintain an
existing system because of program documentation. Good documentation
reduces effort on program maintenance and major program overhaul.
4. Maintain the program.
Maintenance is any activity designed to keep programs error-free, up-
to-date, and in good working condition. It includes tests, adjustments,
replacements, repairs, and so on. Since organizations change their products,
marketing strategies, accounting systems, etc., these changes should be
documented to help programmers make adjustments in existing systems.
• Easy to learn
• Structured language
• It produces efficient programs
• It can handle low-level activities
• It can be compiled on a variety of computer platforms
2.3 Uses of C
C was initially used for system development work, particularly the programs that make-up the
operating system. C was adopted as a system development language because it produces code that
runs nearly as fast as the code written in assembly language. Some examples of the use of C might be −
• Operating Systems
• Language Compilers
• Assemblers
• Text Editors
• Print Spoolers
• Network Drivers
• Modern Programs
• Databases
• Language Interpreters
• Utilities
Programs
A C program can vary from 3 lines to millions of lines
and it should be written into one or more text files with
extension ".c"; for example, hello.c.
UNIX is the major interactive operating system on workstations, servers, and mainframes. C is the
standard development language for personal computers. Most Windows applications, database
programs, graphics libraries, and other large application are written in C.
This means, C is capable of running on more than one computer system or under more than one
operating system. It is easy to get a program written in C on one machine to run on a different machine.
You can easily transfer application programs written in C from one system to another. C is portable
mainly because the actual language definition is very small. There are very few features built into C, but
using functions, you can expand the language. The C language does not even have built-in routines for
printing to the screen or reading from the keyboard. However, there are standard functions for these
and other tasks which are supplied with the C compiler. C programs use a standard library that works
the same in all machines. These libraries allow programmers to build programs that can run in any
machine that has these libraries.
C is a structured language.
C is a “middle-level language”.
Although C cannot beat assembly language at producing code that is highly compact and tuned, it is the
language that probably comes closest to keeping up with assembly language. C has a powerful set of
operators that allows the direct manipulation of bit, bytes, words, and pointers. Using C, a programmer
can produce programs that are as efficient as those written in assembly language while still enjoying the
usefulness of high-level language.
Though C is often criticized to have a complicated syntax since it can allow the multiple use of some
symbols such as “*” and “=”, yet this same feature is what makes C very attractive to power
programmers. C is an elegant language. Many of the constructs and methodologies that are routinely
used in C are also used by C++ and Java programs. Thus, learning C can be considered a first step in
learning C++ and Java, which both support object-oriented programming (OOP).
Debugger. A debugger is used for testing a program and locating programming errors.
Run-Time Environment. A run-time environment is an environment with the capability of
running programs within the Turbo C system.
User Interface. The user interface is the portion of a program where a user interacts. The various
features of Turbo C are integrated into a single program that allows you to smoothly proceed from
sourcecode entry to compilation to debugging and to running
without ever leaving the Turbo C environment.
To make things easier, you can use the Turbo C integrated development environment (IDE). As
previously mentioned, the Turbo C IDE, complete with a text editor and compiler, makes it possible to
edit, compile, link, and run a C program without even leaving the Turbo C environment. This result in an
extremely rapid compilation process and recompilation cycles, thus making the creation, testing, and
debugging of your C program easier and faster.
4. The main Turbo C appears (see figure 3.2), with the main menu active, showing its File option
highlighted.
To choose a Main menu item where at he Main menu bar:
Use arrow keys to highlight the item, then press Enter; or
Type the first letter of the menu item you want (e.g, press E or e to select
the Edit
Edit Window
Message
Window
“Hotkey”
Reference
line
The Edit window is used by Turbo C’s Editor. It is the area where you enter and edit your C program. It is
activated by pressing Alt + E or F10 while in the Main menu. The top line of the Edit window is the Edit
status line, which tells you various things about the state of the Editor and the file you are editing. Line
and Col tells of the location of the cursor (_) on the Edit window. When you start a new program by
choosing File à New, C calls it NONAME.C.
The Message window is used to display various compiler or linker messages. The “Hotkey” reference
line contains a list of useful shortcuts or key combination used to immediately execute a defined option
or function. The Help menu, for example, can be activated by pressing F1.
5. To quit Turbo C, choose Fileà Quit or simply press Alt + X. If your current file on the Edit window has
been modified since it was saved, Turbo C will ask whether you wish to save your file or not.
Program Structure
1. Headers
A constant macro is a name that is replaced by a particular constant value before the program is
sent to the compiler.
Example
Note: The contents of a named constant memory location cannot change during program execution.
Commonly used include files in C Language
2. Declaration
All functions, variables, units or objects that contain values or group of values must
be made known to the computer so that memory spaces will be allocated for
each of them
3. Functions
These are parts of a C program that performs a specific task. The task assigned to a
function is performed whenever C encounters the function name. In C, there
should be at least one function and it is the function main.
The main () in C language declares the main program and call other functions.
4. Comments
Types of comments:
• Multiple line comments – are comments enclosed by /* and */.
• Single line comments – are comments placed after // symbols.
5. Identifiers
Identifiers are names given to various program elements, such as variables,
functions and arrays. The following are the guidelines in creating identifiers:
• It consists of letters and digits without space, in any order, except that
the first character must be a letter.
• Uppercase and lowercase are permitted but they are not
interchangeable. As an exception, an underscore may be used as the
first character of the identifier.
6. Reserved words
Reserved words are keywords that have standard predefined meaning in C. These
reserved words can be used only for intended purpose and they cannot be used
as programmer’s defined identifiers.
Table 4.1 The 32 Turbo C keywords as defined by the ANSI standard
Do If static While
(char), integer (int), and floating point, double floating point and
void. Values of type char are used to hold ASCII characters or any 8-
bit quantity. Values of type int are used to hold integer quantities.
Values of type double and float are used to hold real numbers. Real
numbers have both an integer and fractional component. The type
void has three uses: The first is to declare explicitly a function as
returning no value. The second is to declare explicitly a function
having no parameters. The third is to create generic pointers.
Char 8 0 to 255
Int 16 -32768 to 32767
Float 32 3.4E-38 to 3.4E+38
Double 64 1.7E-308 to 1.7E+308
Void 0 Value less
Table 5.1 Size and Range of Turbo C’s Basic Data Types
Type Modifiers
Except type void, the basic data types may have various modifiers preceding them. A Modifier is
used to alter the meaning of the base type to fit the needs of various situations more precisely. The
list of modifiers includes the following:
1. Signed 3. long
2. Unsigned 4. Short
Type Storage size Value range
Variables are identifiers that can store a changeable value. These can be
different data types.
1.2.1 Variable declaration
All variables must be declared before they may be used. The general form
of declaration is shown here:
Note: - before declaring variable, specify first the data type of the variable/s .
A. Local variables
Variables that are declared inside a function are called local variables. It can only be
referenced by statements that are inside the block in which the variables are declared.
Example:
#include <stdio.h>
main()
{
int a, b, c; local variables
__________;
__________;
__________;
}
B. Global Declarations
Global variables are known throughout the entire program and may be used by any piece of
code. Global variables are created by declaring them outside of any function.
Example:
#include <stdio.h>
int a, b, c; global declarations
main()
{
__________;
__________;
__________;
}
Operator Action
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus Divisor
-- Decrement a value
++ Increment a value
5.4.2 Relational Operators
Relational operators used to determine the relationship of one quantity to another. They always
return 1 (true) or (false) depending upon the outcome of the test. It refers to the
relationships that values can have with one another.
== Equal to
!= Not equal to
Operator Action
&& And
||
or
! not
& AND
| OR
^ Exclusive OR(XOR)
~ One’s complement
y = 100;
else y = 200;
Examples:
A. Evaluate the following:
Expression: z – (a*b / 2) + w * y
5 3 9 2 -5
|__| |___|
27 -10
|____|
13
|_____|
-8
|____________|
-18
B. Evaluate the following expressions and determine whether they yield a value of TRUE or
FALSE
1. Given: dei = 0; y = 4.0; z = 2.0; x =3.0
|________|
1
|___________| 1 = TRUE We had just finished the
discussion on Data types,