Republic of The Philippines Gordon College College of Computer Studies
Republic of The Philippines Gordon College College of Computer Studies
GORDON COLLEGE
College of Computer Studies
LABORATORY WORKBOOK
IT112/L COMPUTER PROGRAMMING 1
NAME:_______________________________________________
TIME/DAY:__________________________________
YEAR/SECTION:__________________________________
INSTRUCTOR:_______________________________
INTRODUCTION
COURSE SYLLABUS
Course Name
Course Credits
Contact Hours
Pre-Requisite
:
:
:
:
Description : Students are expected to learn the Basic syntax and semantics of a
higher-level language; Variables and primitive data types; Expressions and
assignments; Simple I/O including file I/O; Conditional and iterative control
structures; Functions and parameter passing; the concept of recursion; Program
correctness; Debugging strategies; Documentation and program style.
Learning Outcomes:
LO2
Required Output
Due Date
Grading System:
Assignment / Seatwork:
Quiz / Project:
Recitation:
Attendance:
Major Exam:
10%
30%
10%
10%
40%
100%
To pass this course, one must accumulate at least ____ points through the course
requirements discussed above. The maximum points that a students can obtain
through each requirement are shown below.
Requirement/Assessment Task
Major Exams
Accurate and Functional Program for each term
Final Project and Presentation
TOTAL
Maximum Points
50
50
100
200
Solution
Program
Design
User Interface
Unacceptable
0
An incomplete
solution is
implemented on
the required
platform. It does
not compile
and/or run.
Few of the
selected
structures are
appropriate.
Program
elements are not
well designed.
User interaction
is incomplete and
does not meet
Poor
2
A completed
solution is
implemented on
the required
platform, and
uses the
compiler
specified. It
runs, but has
logical errors.
Not all of the
selected
structures are
appropriate.
Some of the
program
elements are
appropriately
designed
User interaction
minimally meets
the
Good
Excellent
3
5
A completed
A completed
solution is tested
solution runs
and runs but
without errors. It
does not meet all
meets all the
the specifications
specifications
and/or work for and works for all
all test data.
test data.
The program
design generally
uses appropriate
structures.
Program
elements exhibit
good design.
The program
design uses
appropriate
structures. The
overall program
design is
appropriate.
User interaction
generally meets
the specifications
User interaction
is as specified
and is natural to
specifications
Code
Readability
Insufficient
program
documentation,
incorrect
indentation,
and/or poor
identifier
selection
specifications,
but does not
increase the
usability of the
program.
Program is
minimally
documented,
some identifiers
are inappropriate
or inconsistent
indentation.
TABLE OF CONTENTS
and is acceptable
to the user.
the user.
Some required
documentation is
missing, or
identifiers are
inappropriate, or
statements are
not indented
correctly.
All required
documentation is
present, the
program is
correctly
indented, and
appropriate
identifiers are
selected.
I.
II.
III.
IV.
V.
VI.
VII.
VIII.
IX.
X.
XI.
XII.
XIII.
XIV.
CHAPTER 1
INTRODUCTION TO PROGRAMMING LANGUAGE
BRIEF HISTORY OF C LANGUAGE
The C Programming language is a general purpose high level language that
was originally developed by Dennis M. Ritchie to develop the Unix operating system
at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in
1972.
In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly
available description of C, now known as the K&R standard.
The UNIX operating system, the C compiler, and essentially all UNIX
application programs have been written in C. The C has now become widely used
professional language for various reasons.
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
FACTS ABOUT C
Institute (ANSI)
The UNIX OS was totally written in C in 1973
Today C is the most widely used and popular System Programming Language
Most of the state of the art software have been implementing using C
Todays most popular Linux OS and RBDMS MySQL have been written in C.
WHY
LABORATORY SESSION 1
OBJECTIVE: To familiarize programming environment using Turbo C and the
Fundamentals of Programming Language.
Before you start doing programming using C programming language, you need the
following two software's available on your computer, (a) Text Editor and (b) The
Compiler.
Text Editor
This will be used to type your program. Examples of few editors include Windows
Notepad, OS Edit command, Brief, Epsilon, EMACS, and vim or vi
Name and version of text editor can vary on different operating systems. For example
Notepad will be used on Windows and vim or vi can be used on windows as well as
Linux, or Unix.
The files you create with your editor are called source files and contain program
source code. The source files for C programs are typically named with the
extension .c.
Before starting your programming, make sure you have one text editor in place and
you have enough experience to write a computer program, save it in a file, compile it
and finally execute it.
The C Compiler
The source code written in source file is the human readable source for your program.
It needs to be "compiled", to turn into machine language so that your CPU can
actually execute the program as per instructions given.
This C programming language compiler will be used to compile your source code into
final executable program. I assume you have basic knowledge about a programming
language compiler.
The source file is required to be turned into an executable file. This is called
Making of the .exe file. The steps required to create an executable file are:
1. Create a source file, with a .cpp extension.
2. Compile the source code into a file with the .obj extension.
3. Link your .obj file with any needed libraries to produce an executable program.
Compiling the Source Code
Although the source code in your file is somewhat cryptic, and anyone who doesn't
know C will struggle to understand what it is for, it is still in what we call humanreadable form. But, for the computer to understand this source code, it must be
converted into machine-readable form. This is done by using a compiler. Hence,
compiling is the process in which source code is translated into machine
understandable language.
Creating an Executable File with the Linker
After your source code is compiled, an object file is produced. This file is often
named with the extension .OBJ. This is still not an executable program, however. To
turn this into an executable program, you must run your linker. C programs are
typically created by linking together one or more OBJ files with one or more libraries.
A library is a collection of linkable files that were supplied with your compiler.
Project/Make
Before compiling and linking a file, a part of the IDE called Project/Make checks the
time and date on the file you are going to compile.
Compiling and linking in the IDE
In the Turbo C IDE, compiling and linking can be performed together in one step.
There are two ways to do this: you can select Make EXE from the compile menu, or
you can press the [F9] key.
Executing a Program
If the program is compiled and linked without errors, the program is executed by
selecting Run from the Run Menu or by pressing the [Ctrl+F9] key combination.
The Development Cycle
If every program worked the first time you tried it, that would be the complete
development cycle: Write the program, compile the source code, link the program,
and run it. Unfortunately, almost every program, no matter how trivial, can and will
have errors, or bugs, in the program. Some bugs will cause the compile to fail, some
will cause the link to fail, and some will only show up when you run the program.
Whatever type of bug you find, you must fix it, and that involves editing your source
code, recompiling and relinking, and then rerunning the program.
Correcting Errors
If the compiler recognizes some error, it will let you know through the Compiler
window. Youll see that the number of errors is not listed as 0, and the word Error
appears instead of the word Success at the bottom of the window. The errors are to
be removed by returning to the edit window. Usually these errors are a result of a
typing mistake. The compiler will not only tell you what you did wrong; theyll point
you to the exact place in your code where you made the mistake.
Exiting IDE
An Edit window may be closed in a number of different ways. You can click on the
small square in the upper left corner, you can select close from the window menu, or
you can press the [Alt][F3] combination. To exit from the IDE select Exit from the
File menu or press [Alt][X] combination.
Building Blocks of Programming Language:
In any language there are certain building blocks:
Constants
Variables
Operators
Methods to get input from user (scanf( ), getch( ) etc.)
Methods to display output (Format Specifier, Escape Sequences etc.) and so on.
Variables and Constants
If the value of an item can be changed in the program then it is a variable. If it will not
change then that item is a constant. The various variable types (also called data type)
in C are: int, float, char, long etc. For constants, the keyword const is added before
declaration.
Operators
There are various types of operators that may be placed in three categories:
Basic: + - * / %
Assignment: = += = *= /= %=
(++, may also be considered as assignment operators)
Relational: < > <= >= == !=
Format Specifiers
Format Specifiers tell the printf statement where to put the text and how to display the
text.
The various format specifiers are:
%d => integer
%c => character
%f => floating point etc.
Field Width Specifiers
They are used with % to limit precision in floating point number. The number
showing limit
follows the radix point.
Escape Sequences
Escape Sequence causes the program to escape from the normal interpretation of a
string, so
that the next character is recognized as having a special meaning. The back slash \
character is called the Escape Character. The escape sequence includes the
following:
\n => new line
\b => back space
\r => carriage return
\ => double quotations
\\ => back slash etc.
Getting Input From the User
The input from the user can be taken by the following techniques: scanf( ), getch( ),
getche( ), getchar( ) etc.
Examples
1. Implementing a Simple C Program
#include<conio.h>
#include<stdio.h>
void main(void)
{
clrscr();
printf(\n Hello World);
getch();
}
2. Demonstrating the fundamentals of C Language
#include<conio.h>
#include<stdio.h>
void main(void)
{
clrscr();
int num1,num2,sum,product;
printf(\tThe program takes two numbers as input and
prints their sum and product);
printf(\n Enter first number:);
scanf(%d,&num1);
printf(\n Enter second number:);
scanf(%d,&num2);
sum=num1+num2;
product=num1*num2;
printf(\n%d+%d=%d,num1,num2,sum);
printf(\n%d*%d=%d,num1,num2,product);
getch();
}
EXERCISES
1. Type the following program in C Editor and execute it. Mention the Error (if
any).
void main(void)
{
printf(This is my first program in C);
}
_____________________________________________________________________
______
_____________________________________________________________________
______
_____________________________________________________________________
______
2. Add the following line at the beginning of the above program. Recompile the
program. What is the output?
#include<stdio.h>
_____________________________________________________________________
______
_____________________________________________________________________
______
3. Make the following changes to the program. Mention the Errors observed, in your
own words:
i. Write Void instead of void.
_____________________________________________________________________
______
_____________________________________________________________________
______
ii. Remove the semi colon ;.
_____________________________________________________________________
______
_____________________________________________________________________
______
iii. Erase any one of brace { or } .
_____________________________________________________________________
______
_____________________________________________________________________
______
4. Write a program to calculate the Area (A= r2) and circumference of a circle
(C=2r),
where r = radius is taken as input and is declared as a constant. The precision of
should be the number of characters in your name. Display the result to 4 decimal
places.
_____________________________________________________________________
______
_____________________________________________________________________
______
_____________________________________________________________________
______
_____________________________________________________________________
______
_____________________________________________________________________
______
_____________________________________________________________________
______
_____________________________________________________________________
______
_____________________________________________________________________
______
_____________________________________________________________________
_____
5. Write a single C statement to output the following on the screen:
My name is Your Name
And my roll number is 00Your_roll_no
I am a student of Computer and Information System Department
_____________________________________________________________________
______
_____________________________________________________________________
______
_____________________________________________________________________
______
Programming Languages Lab Session 02