Chapter 1 and 2 (Programming 1)
Chapter 1 and 2 (Programming 1)
Fundamentals of Programming
II. Overview
A. Introduction/Rationale
This instructional material covers eight chapters and is ideal for a one semester
period. It introduces computer programming in Turbo C language using basic control
structures in problem solving, logic building. It discusses the syntax of the most common
functions and technicalities of the language. Topics should cover problems involving simple
problems to problems involving files and some simple dynamic data structures.
B. Objectives
C. Learners
D. Time Frame
First Semester
Computer Program – set of instructions that instruct computers what to do to solve the
problem.
Programmer – is a person who understands how the computer solves the problem by defining
the problem requirement and developing methods on how problem solutions will be
communicated to the computer.
Programming – an act of developing the program.
Programming Language – is a set of rules that tells the computer what operation to perform.
1. Problem Analysis – is defined by the American National Standard Institute (ANSI) as the
“methodical investigation of a problem and the separation of the problems into smaller related
units for further detailed study.”
The following are guidelines when analyzing problems:
1. Define the problem.
2. Determine the input data.
3. Determine the required output.
4. Determine the operations involved.
2. Program Design
The second step in the program development process describes the algorithm or the set of
well-defined rules for the solution of a problem in a finite number of steps. Algorithms specify
what actions and operations of a program will be used. For example, to find the average value of
three numbers, the algorithm will be:
There are several design tools to choose from in formulating an algorithm to name a few we
have flowcharting, decision table, pseudo-code.
4. Program Desk Checking – means manually testing the solution design with a set of test data
and debugging such, if errors are present.
The coded program is manually reviewed for obvious errors( like grammar and spelling
errors) on syntax and logic. In this phase, the programmer plays the role of the computer,
reading the program line by line, going through each process seeing where the flaws are so
that they can be corrected.
Testing – refers to the process of running a program in the computer for the purpose of
verifying the accuracy and workability of the solution.
Debugging – refers to the process of identifying, locating and correcting the error in the
solution.
Types of Errors:
1. Syntax Error – are misspelled words, incorrect symbols, omitted punctuation marks or
unmatched parentheses.
3. Run Time error, A software error that occurs while a program is being executed, as
detected by a complier 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 improper input, such as when a user entered a character data where an integer
value is expected.
1. Program Documentation. This means documenting the program for later application.
Variables are to be defined and comments are made to be informative.
Chapter 2
C Programming: An Introduction
C is a programming language designed by Dennis Ritchie in 1972 at AT & T Bell Laboratories. C was
designed as a language for systems programming, as a way to fully access the computer’s power
without becoming caught up in the tedious writing of assembly language. 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.
In C, the program statements can refer to functions, subprograms, which perform, specialized tasks.
The programmer can write the main program that makes reference to various functions before the
functions are actually written, and then the programmer can fill in the required functions. This
approach to programming is called top-down design.
Enter each line of the source code into the memory, and save it on disk as 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.
***Object 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. Correct errors by 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 complied 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.
Use the loader program to place the executable file back in the memory, ready for execution.
The C Environment
Turbo C is the most commonly used programming language and is known for its flexibility. The Turbo
C environment is shown below:
Menu Bar - can be accessed by pressing the Alt key plus the first letter of the Menu. If you want to
access the File menu, you need to press Alt + F.
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.
RUN. Run the most recently complied program. If the current file has not yet been complied, 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 indicates if the Num Lock, Caps Lock, and Scroll Lock keys are in the on or off mode.
Functions Menus pressing the function keys accesses these menus:
F1- 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.
C Data Types
Before a variable name (identifier) can be used used a C program, it must be declared explicitly,
together with its corresponding data type. There are some restrictions in giving a name to a variables
and constants.
Note: C programming language is a case –sensitive. This means that all keywords, commands and
standard functions of C language must be written in lowercase letters.
Each variable in C has an associated data type. Each data type requires different amounts of memory
and has some specific operations which can be performed over it. Let us briefly describe them one by
one:
In C programming, variables which are to be used later in different parts of the functions have to be
declared. Variable declaration tells the compiler two things:
The name of the variable
The type of data the variable will hold
A variable can store any data type in C programming. The name of the variable has nothing to do with
its type.
data_type var1,var2,...varn;
Here, var1, var2,...varn are the names of valid variables.
Variables can also be defined in multiple lines instead of on the same line.
data_type var1;
data_type var2;
data_type varn;
When the variables are declared in single line, then the variables must be separated by commas.
Note: All declaration statements must end with a semi-colon(;).
For example:
int age;
float weight;
char gender;
In these examples, age, weight and gender are variables which are declared as integer data type,
floating data type and character data type respectively.