Date post: | 12-Jan-2016 |
Category: |
Documents |
Upload: | kartik-choudhary |
View: | 224 times |
Download: | 0 times |
COL 100
Introduction to Computer Science
Instructor
Prof K. K. Biswas Deptt of Computer Science & EngineeringRoom no. 423, Bharti [email protected]
Course Contents
• Introduction to computer Programming using ‘C’ language
• Algorithms• Problem solving using computers
Reference Books
1. Programming with C (Schaum’s Outlines Series, )– Byron Gottfried, McGraw Hill
2. The C Programming LanguageBrian W Kernighan, Dennis M Ritchie
Lab Work
• Labs will be held in Computer Services Centre • TAs will be around to look into your problems• Try to visit the lab at other times as well• More you practice, better you will learn programming
• Cheating Policy
Brief Introduction to Computers
7
What does a Computer do?
Computers can perform four general operations,which comprise the information processing cycle.
InputProcess DataOutputStorage (Programs and Data)
8
Why is a Computer so Powerful?
• The ability to perform the information processing cycle with amazing speed.
• Reliability (low failure rate).• Accuracy.• Ability to store huge amounts of data and information.
• Ability to communicate with other computers.
9
How does a Computer knowwhat to do?
• It must be given a detailed list of instructions, called a compute program or software, that tells it exactly what to do.
• The computer program corresponding to that job must be stored in memory.
• The computer can start the operation by executing the program instructions in the specified sequence.
Primary Components Of A Computer ?
Input devices.Central Processing UnitMemory.Output devices.Storage devices.
Data Representation
• Data is stored inside the memory in the form of 1’s and 0s, Bits (Binary Digits) as shown below (ON/OFF positions)
1100 0111 (referred to as a byte )0011 11110101 1110
Units of Memory
• 1 KB (1 KiloByte) contains 1024 Bytes. • 1 MB (1 MegaByte) consists of 1024 KB.• 1 GB (1 GigaByte) has 1024 MB.• 1 TB (1 Terabyte) consists of 1024 GB.
Secondary Storage Devices
• Secondary storage devices are used to store data when they are not being used in memory. The most common types of secondary storage used on personal computers PCs are
• hard disks• CD‐ROM drives• Flash Drives (USB, pen drives)
CPU
• Central Processing Unit (CPU)– it executes computer programs. – Instructions in the program tell the processor when and
what to read from a keyboard; – what to display on a screen; – what to store and retrieve from a disk drive and so on.
• It can do arithmetic, compare numbers and perform input/output. (read information and display or store it).
CPU• Central Processing Unit (CPU)
– it executes computer programs. – Instructions in the program tell the processor when and what to
read from a keyboard; – what to display on a screen; – what to store and retrieve from a disk drive and so on.
• It can do arithmetic, compare numbers and perform input/output. (read information and display or store it).
• It has no magical powers. It is instructive to bear in mind that all computer programs are constructed from sequences of instructions based on primitive operations
CPU operation
• The CPU can read from and write to a specified memory location.
• While writing a byte into a given location, the previous bit pattern is destroyed and the new contents (bit pattern) are saved for future use.
• While reading a byte the contents of that location are NOT changed.
• CPU has number of on chip storage locations called registers, usually 8,16, or 32 bits long
• The greater the number, the more powerful the CPU
CPU operation
• The CPU can read from and write to a specified memory location.
• While writing a byte into a given location, the previous bit pattern is destroyed and the new contents (bit pattern) are saved for future use.
• While reading a byte the contents of that location are NOT changed.
• CPU has number of on chip storage locations called registers, usually 8,16, or 32 bits long
• greater the number, the more powerful the CPU
Memory speeds
• on chip registers – Fastest• Main memory • Secondary memory – slowest
Software• Software is the set of programs and applications that tell the
computer what to do.
• Computer programmers write the codes/instructions that make‐up software applications/programs.
• Programs and Data are saved in the same manner in the computer
• Set of instructions is also referred to as lines of code.
Two Types of Software
Application Software
Operating system Software
Operating System Software
• Complex set of programs which directs all the activities and sets all the rules for how the hardware and software will work together.
• Manages input devices, output devices, memory units, CPU
• The OS is always present when the computer is running• Responsible for running application programs
Examples : Windows XP, VistaSolarisUnix, Linux
Application Software
1. Business software: word processors, spreadsheets, and database programs.
2. Communication software: allows computers to communicate with other computers: fax software, Novell NetWare, AOL, Modem Software.
3. Graphics software: software that allows users to create and manipulate graphics...Photoshop, Print Shop, etc.
4. Education and Reference software: Programs that help teach new material and ideas, and programs that can be used to find information
5. Entertainment and Leisure software… games
6. Scientific software: Programs to solve scientific problems
Introduction to ‘C’ Programming
26
Variables
• Very important concept for programming• An entity that has a value and is known to the program by a
name • Can store any temporary result while executing a program• Can have only one value assigned to it at any given time
during execution• The value of a variable can be changed during the
execution
27
Contd.• Variables stored in memory• Remember that memory is a list of storage locations, each having a unique address
• A variable is like a bin– The contents of the bin is the value of the variable– The variable name is used to refer to the value of the variable
– A variable is mapped to a location of the memory, called its address
28
Example of variables#include <stdio.h>void main( ){
int x;int y;x = 1;y = 3;printf("x = %d, y= %d\n", x, y);
}
29
Variables in Memory
Instruction executed Memory location allocatedto a variable X
Ti
me
X = 10
10X = 20
X = X +1
X = X*5
30
Variables in Memory
Instruction executed Memory location allocated to a variable X
Ti
me
X = 10
20X = 20
X = X +1
X = X*5
31
Variables in Memory
Instruction executed Memory location allocated to a variable X
Ti
me
X = 10
21X = 20
X = X +1
X = X*5
32
Variables in Memory
Instruction executed Memory location allocated to a variable X
Ti
me
X = 10
105X = 20
X = X +1
X = X*5
33
Variables (contd.)
20
?
X
Y
X = 20
Y=15
X = Y+3
Y=X/6
34
Variables (contd.)
20
15
X
Y
X = 20
Y=15
X = Y+3
Y=X/6
35
Variables (contd.)
18
15
X
Y
X = 20
Y=15
X = Y+3
Y=X/6
36
Variables (contd.)
18
3
X
Y
X = 20
Y=15
X = Y+3
Y=X/6
37
Data Types
• Each variable has a type, indicates what type of values the variable can hold
• Four common data types in C– int ‐ can store integers (usually 4 bytes)– float ‐ can store floating point numbers (54.27, 0.05, etc.)
– double ‐ can store double‐precision floating point numbers
– char ‐ can store a character (1 byte)
38
Contd.
• Must declare a variable (specify its type and name) before using it anywhere in your program
• All variable declarations should be at the beginning
39
Variable Names
• Sequence of letters and digits• First character must be a letter or ‘_’• No special characters other than ‘_’• No blanks in between• Names are case‐sensitive (max, MAX and Max are different names)
• Examples of valid names: k rank1 MAX max Min class_rank
• Examples of invalid names:a’s class rank 2sqroot class,rank
More Valid and Invalid Identifiers
• Valid identifiersXabcsimple_interesta123LISTstud_nameEmpl_1Empl_2avg_empl_salary
• Invalid identifiers10abcmy‐name“hello”simple interest(area)rate%
C Keywords
• Used by the C language, cannot be used as variable names
• Examples:– int, float, char, double, main, if else, for, while. do, struct, union, typedef, enum, void, return, signed, unsigned, case, break, sizeof,….
– There are others, see textbook…
42
Constants• Integer constants
– Consists of a sequence of digits, with possibly a minus sign before it
• Floating point constants– Two different notations:– Decimal notation: 25.0, 0.0034, .84, ‐2.234– Exponential (scientific) notation
3.45e23, 0.123e‐12, 123e2
e means “10 to the power of”
#include <stdio.h>
int main(void) {
printf(“My name is Kanad Biswas.")
return 0;}
Compiler
A "translator" which converts a program writtenin a high‐level computer language
into machine language, 001101001010
understood by the computer.
1) Edit your program2) Compile your program3) Execute your program4) Go to step 1, if you are not satisfied withyour results.
#include <stdio.h>
int main(void) {
printf(“My name is Kanad Biswas.");
return 0;}
Execution:
My name is Kanad Biswas
UNDERSTANDING A PROGRAMLINE BY LINE
#include <stdio.h>
A directive to include a standard C header file. There are some pre-written functions in C that we commonly use.
All the functions that control standard input and output are included in the file stdio.h.
When we include this file, we are allowed to use these functions.
These functions allow us to print information to the screen and read in information from the user.
All of your programs will include this line.
int main(void) {
All code in C resides in functions.This line signifies the beginning of the function main.
main is a special function.
A program may be made up on many functions, but the computer only DIRECTLY executes main.
int main(void) {printf(“My name is Kanad Biswas.")return 0; }
Main calls other functions.
All functions are signified by
-a return type, (which is int for this function),
-a name (which is main for this function)
-a parameter list in parentheses (void here)
-curly brace { signifies the beginning of the function.
curly brace } ends the definition of the function.
printf(“My name is Kanad Biswas.")
This line calls a function printf which resides in stdio.h.
It prints out any string contained in between two sets of double quotes.
Some special characters are also used in printf.
All such characters start with a backslash: \
The code \n stands for a newline character.
This simply tells the computer to advance the cursor to the new line.
/* My 1st C Program */ COMMENT STATEMENT
#include <stdio.h>int main(void) {
printf("This is my first C Program.");
printf(“ My name is Kanad Biswas.");
return 0;}
This is my first C Program. My name is Kanad Biswas
/* My 2nd C Program – using new line*/
#include <stdio.h>int main(void) {
printf("This is my first C Program.\n");
printf(“ My name is Kanad Biswas.\n");
return 0;}
This is my first C Program. My name is Kanad Biswas.