Introduction To Computer Programming: Course 1 29-09-2011
Introduction To Computer Programming: Course 1 29-09-2011
Course 1 29-09-2011
Who am I?
Adriana ALBU Lecturer at Automation and Applied Informatics Department from Automation and Computers Faculty [email protected] www.aut.upt.ro/~adrianaa
Etc intranet:
https://intranet.etc.upt.ro/?page=english
Course 2
A first C program Input/Output functions Mathematical and other functions Possible errors
Course 4
Arrays Strings
Course 5
User defined functions
Course 7
Handling with files in C (first part)
Course 8
Handling with files in C (second part)
Course 9
Graphics programming in C
Content
Introduction
Logical schemes Constants, variables, expressions Data types (integer, real, char, logical)
10
Introduction
Why do you need this course? Some of you to learn basic knowledge about informatics and structured programming The others to consolidate and to order the knowledge that they already have
11
Introduction
What will you learn at this course? Fundamental notions about informatics Computer programming using C language
12
Introduction
Information concept with a diversity of meanings closely related to: control, data, communication, instruction, knowledge, meaning, perception, representation, pattern In the computer world, data and knowledge are not really meanings of information; they are connected to information
13
Introduction
1.Data the raw material
(body temperature of a patient is 40)
3 2 1
14
Introduction
Informatics - studies the structure, behavior, and interactions of natural and artificial systems that store, process and communicate information Since computers, individuals and organizations all process information, informatics has technological, cognitive and social aspects
15
Introduction
Communication a very important concept:
between people - language
16
Introduction
Programming language
used to tell to a computer how to do a special job like a human language, a programming language has words, sentences, and well defined syntactical rules the sentences are combined in order to make a program which will be responsible for the communication between a person and a computer
17
Introduction
One of the major programming paradigms is structured programming It states that three ways of combining elements of a program are sufficient to express any computable function:
sequencing, selection, and iteration
18
Introduction
There are three steps which have to be made in order to solve a problem:
analyzing the problem to establish exactly what are the requirements representing the problem in an adequate way for computer aid solving implementing writing the program that will solve the problem (in a specific language)
Analysis Representation Implementation
19
Introduction
Algorithm - a finite list of well-defined steps for accomplishing some tasks Example: Euclids algorithm
Logical diagrams
the simplest way to represent an algorithm are made of graphical elements
20
STOP
21
Example
A program that does nothing
START
STOP
22
Example
A program that prints the message Hello world!
START
Hello world!
STOP
24
Example
A program that reads and writes a number
START
read a
write a
STOP
25
x=7*5
26
Example
START
read a, b
x=(-b)/a
write x
STOP
NO
YES
NO
a0
YES
28
ax+b=0
read a, b
b=0
YES
a=0
NO
NO
b0 No solutions
b=0
YES
x=(-b)/a write x
STOP
29
A=L2
write A
STOP
30
NO
L>0
YES
A=L2
write A
STOP
31
read L
L>0
YES
Data types
Integer, Real, Char, Logical
Data types
Data = variables or constants Each data has a type Type = a multitude of values which has a name Types could be:
Standard
int for integer values float or double - for real values char for characters
Defined by user
Modifiers
Defines the length of the memory space allocated for a variable:
Short Long Signed Unsigned
3.4*(10^-38) 3.4*(10^38)
1.7*(10^-308) 1.7*(10^308) 3.4*(10^-4932) 1.1*(10^4932)
What is a bit?
Bit
is a binary digit, taking a value of either 0 or 1 a basic unit of information storage and communication in digital computing and digital information theory 8 bits = 1 byte
Integer type
Each integer type is a subset of integer numbers multitude (Z)
Type
int, short int unsigned int long int
Representation
16 bits with sign
Values (range)
-32.768 32.767
Integer type
Standard constants connected with integer type:
INT_MAX (maximum value of int type): 32767 INT_MIN (minimum value of int type): -32768 LONG_MAX (maximum value of long int type): 2.147.483.647 LONG_MIN (minimum value of long int type): 2.147.483.648 UINT_MAX (maximum value of unsigned int type): 65.535 ULONG_MAX (maximum value of unsigned long int type): 4.294.967.295
Arithmetical operations:
+, -, *, /, %
Logical operations
for variables: and &&, or ||
Example
x=7; a=x++; result: a=7, x=8 x=7; a=++x; result: a=8, x=8
Real type
Real numbers are written in floating point format
Type float double long double Represen- Values (range) tation 32 bits 3.4E-38 3.4E38 64 bits 80 bits 1.7E-308 1.7E308 3.4E-4932 1.1E4932
Arithmetical
Operators: + , - , * , / Real operands and real result e.g. 7/2 = 3, but 7.0/2.0 = 3.5.
Char type
It is the multitude of all possible chars
Type Representation Values (range) 0 255 -128 127 unsigned char 8 bits char 8 bits
All characters are converted in integer numbers American Standard Code for Information Interchange (ASCII)
ASCII
SP 32 0 48 @ 64 P ! 33 1 49 A 65 Q 34 2 50 B 66 R # 35 3 51 C 67 S $ 36 4 52 D 68 T % 37 5 53 E 69 U & 38 6 54 F 70 V 39 7 55 G 71 W ( 40 8 56 H 72 X ) 41 9 57 I 73 Y * 42 : 58 J 74 Z + 43 ; 59 K 75 [ , 44 < 60 L 76 \ 45 = 61 M 77 ] . 46 > 62 N 78 ^ / 47 ? 63 O 79 _
80
` 96 p 112
81
a 97 q 113
82
b 98 r 114
83
c 99 s 115
84
d 100 t 116
85
e 101 u 117
86
f 102 v 118
87
g 103 w 119
88
h 104 x 120
89
I 105 y 121
90
j 106 z 122
91
k 107 { 123
92
l 108 124
93
m 109 } 125
94
n 110 ~ 126
95
o 111
127
Char type
c1 = A; c2 = 7; c3 = \n; /* newline */ c4 = \t; /* tab */
Standard operators and functions
c1+c2 x
Logical type
This is not a standard type in C There are used integer values in order to represent logical aspects
False is 0 True anything else
Logical operations:
not ! or || and &&
Constants
Constants can be: Defined by the user
Numerical Alphanumerical Example of constants defined by the user const m=-3; const n=fabs(m)-1;
Variables
In a C program it has to be declared any variable that appear in that program Declaration connection between the symbolic name of the variable and its type int m,n; float v,w; char ch1,ch2;
Variables
A variable is in fact the symbolic name of a memory location Using a declaration, that memory location will have:
a name and a type
Variables declaration
type_var name_var;
type_var is a the type of the variable which I declare name_var is the name of a variable or a list with the names of more variables The variable can receive an initial value when it is declared
Variables declaration
Integers variables are used to store integer numbers
int number; int no_students = 165;
Expressions
It is represented by some operations that are made with operands (constants, variables, functions) The result is a value The type of the result depends of the type of the operands
Expressions
E = simple expression [relational operators simple expression] relational operators: <, <=, >, >=, ==,!= A simple expression has:
terms additive operators (+, -, OR, XOR)
A term has
factors multiplicative operators (*,/,%, AND)
Expressions
A factor can be:
a constant a variable (represented by its value) & followed by a variable NOT other factor + or - other factor call of a function type conversion other expression written between ()
Operators
Multiplicative operators: *, /, % Additive operators: +, - (binary and unary) Composed operators: +=, -=, *=, /=, %=
(e.g. x+=3 x=x+3)
& operator
placed in front of a variable (&x) represents the memory address of a variable
Next time
The first steps in programming
C environment A first program The structure of a program Input/output functions Mathematical and other functions
65
2. The surface of a triangle whose sides have lengths a, b, and c using Herons formula: A s( s a )( s b )( s c )
s the semiperimeter of the triangle
3. Euclids algorithm
Greatest common divisor (GCD) of two natural numbers