TA ZC142 Computer Programming Lecture 1 Date: 09/01/2013
Objectives of learning this course
The primary goals of the course are to introduce:
1. Basic representation of data and how to process data using the representation inside a computer. 2. Techniques for specifying data and operations on data using a programming language 3. Systematic techniques and approaches for constructing programs.
Components of a Computer
Computer Hardware Computer Software Memory
Primary Memory Secondary Memory
Components of a Computer
Figure 1.4 1000 Memory Cells in Main Memory
Computer Software
Its a collection of programs which together does a
specific task.
System Software Application Software
System Software
Collection of programs which helps user
to interact with the computer hardware. e.g. Operating Systems, Compilers
Operating System
Collection of programs that control the interaction
between the user and the computer hardware. Loaded into ROM when the computer is powered on Booting a Computer Operating Systems responsibilities:
Communicating with the computer user Managing the resources like memory, processor etc Reading or writing data to secondary storage devices
Widely used Operating Systems
Graphical User Interface
Windows Macintosh OS
Command-Line Interface
Unix OS or Linux OS MS-DOS
Computer Software
System Software
E.g. Operating Systems, Compilers
Application Software
Application Software
Collection of programs developed to
assist computer user in accomplishing specific task or application.
e.g. Railway Reservation, Inventory Control System, Word processors
Computer Language
Computer Languages
Machine Language
Collection of binary number codes directly understood by the computer.
Limitations Difficult to understand Different for every type of processor
Computer Languages
Assembly language
Collection of mnemonic codes
Limitations Depends on the ISA of the processor E.g. Assemble language instructions of AMD(Advanced Micro Device) processor will be different from Pentium processor. Different for every type of processor Also difficult to remember ISA of every processor.
Computer Languages
High Level Language (HLL)
Instructions here are constructed by combining algebraic expressions and symbols taken from English language. Advantage
Independent
of the processor
Needs a System program (Compiler) to translate the HLL program into machine language.
Widely used High-Level languages
Language
FORTRAN COBOL C Ada C++ Java
Application Area
Scientific programming Business data processing System programming Real-time distributed systems Object-Oriented Programming Web programming
Origin of Names/
Formula translation Common BusinessOriented Language Predecessor language was named B Incremental modification of C Originally named Oak
#include<stdio.h> int main(void) { printf("Hello World\n"); return(0); }
Hello.c gcc -c Hello.c Hello.o
ELF???????????? ??????????????? ??4?????(? ??L$qUQ $????Y]a ??Hell
gcc o exe Hello.o
./exe
Translating a High-Level Language Program
C Programming Language
C Programming Language
Developed by Dennis Ritchie at AT&T Bell Laboratories. Initially designed to write UNIX OS. Due to its powerful language constructs, flexibility and high quality C compilers it has become a popular language in industry for wide variety of applications and in academics for learning the concepts of programming languages.
Structure of a C Language Program
/* Program to compute .
*/ #include<stdio.h> /*Library files inclusion */
Void indicate that main( ) receives no data from OS
int main(void) {
/* local variable declarations */ /* initialization */ /* statements */
return(0); }
Returns 0 to the OS after execution.
Simple C Program
Standard Header File #include<stdio.h> #define KMS_PER_MILE 1.609 Preprocessor directive
constant
Reserved Words
int main (void) { float miles, kms; /* Read distance in miles */ printf( Enter distance in miles \n); scanf (%f, &miles); /*Convert the distance to kms*/ kms = KMS_PER_MILES * miles;
Variables or User defined identifiers
Comments
Standard Identifier
printf( Distance in kms = \t%f ,kms ) ; // Display the resultant distance in Kms return(0); }
Effect of scanf("%lf", &miles);
22
Memory(a) Before and (b) After Execution of a Program
23
Effect of kms = KMS_PER_MILE * miles;
24
Programming Attributes
Structure of C program C Character Set
Variables & Constants
Data Types Operators
Arithmetic Operators Relational Operators Logical Operators Assignment Operators Increment and Decrement Operators
Structure of a C Language Program
/* Program to compute .
*/ #include<stdio.h> /*Library files inclusion */
Void indicate that main( ) receives no data from OS
int main(void) {
/* local variable declarations */ /* initialization */ /* statements */
return(0); }
Returns 0 to the OS after execution.
Simple C Program
Standard Header File #include<stdio.h> #define KMS_PER_MILE 1.609 Preprocessor directive
constant
Reserved Words
int main (void) { float miles, kms; /* Read distance in miles */ printf( Enter distance in miles \n); scanf (%f, &miles); /*Convert the distance to kms*/ kms = KMS_PER_MILES * miles;
Variables or User defined identifiers
Comments
Standard Identifier
printf( Distance in kms = \t %f ,kms ) ; // Display the resultant distance in Kms return(0); }
C Character Set
Uppercase letters A to Z , lower case letters a to
z, digits 0 to 9 Special characters.
e.g. + , - , *, &, ?, /, \, #, (, ), {, },[, ], <, >, ==, =, ;, :, , etc e.g. \n (new line character), \t (horizontal tab), \v (vertical tab)
Escape sequences
Variables & Constants in Programming Language
Variable
Its a name associated with a memory cell whose value can be changed. All variables must be declared before they can appear in executable statements It consists of a data type, followed by one or more variables. E.g. double miles, kms;
Variables
Invalid
5thplayer x Order-no double count value
Remark
First char is number First char is not letter - is not allowed Double is a reserved word Blank is not permitted
Valid
player5 x Order_no doubletype Count_value
Variables & Constants in Programming Language
Constant
Its a name associated with a memory cell whose value can not be changed. Declared by #define directive. E.g. #define KMS_PER_MILE 1.609
const
int x = 300; const char c = A;
Data Types
It is a set of values and a set of operations on
those values.
Primary Data types Derived Data types (discussed later) User defined data types (discussed later)
Primary Data Type
Data type int char float double Description Integer quantity Single character Floating point number Double precision Memory Requirement 2 bytes 1 byte 4 bytes 8 bytes
Assignment Statement
It stores a values or a computation result
in a variable It is used to perform arithmetic operations in a program e.g. kms = KMS_PER_MILES * miles;
Assignment operator
Multiply operator
printf() Function
Escape sequence function arguments placeholder
Function name
printf( Distance in kms = \t %f ,kms ) ;
Format String printlist
scanf() Function
Addressof operator
Function name
scanf ( %d
%d, &age, &year);
Format String
Variables
Sample Program
#include<stdio.h> /* program to display use of I/O and data types */ int main() Input { int marks; Enter your marks: float average; 70 Enter average marks: char grade; 50.5 printf(Enter your marks:\n); Enter your grade: scanf(%d, &marks); printf(Enter average marks:\n); A scanf(%f, &average); Output printf(Enter your grade:\n); Marks = 70 scanf(%c, &grade); Average = 50.5 printf(Marks = %d\n,marks); printf(Average = %f\n,average); Grade = A printf(Grade = %c\n,grade); return (0); }
Arithmetic Operators
Operator + * / % Meaning Addition or unary plus Subtraction or unary minus Multiplication Division Modulo Division
Arithmetic Expression
Let int x = 15; int y = 6;int z;
Example:
z z z z z
= = = = =
x+y x-y x*y x/y (decimal part truncated) x%y (remainder of the division)
Q. If x and y are declared as float then? Q. If x is integer and y is float then?
Example 2 :Program to compute area and circumference of a circle #include<stdio.h> #define PI 3.14 int main(void) { int radius; float area,circum; printf("Enter radius of a circle\n"); scanf("%d", &radius); area = PI * radius * radius; circum = 2 * PI * radius; printf("Area = %f \t Circumference = %f ",area, circum); return(0); }
Text Book and Reference Book
Prescribed Text Book
J.R. Hanly and E.B. Koffman, Problem Solving and Program Design in C., 5th Edition. Pearson Education 2007.
Reference Books
R.J. Dromey. Problem Solving using Computer. Prentice Hall India. Eastern Economy Edition. 2002. Brian W. Kernighan, Dennis Ritchie. The C Programming Language, Prentice Hall. 2nd Edition.
Any Doubts or Questions?