Computers and Programming CPC
Computers and Programming CPC
st
The 1 lecture
Jiří Šebesta Sumber dari : www.urel.feec.vutbr.cz/~sebestaj/CPC/LE01.ppt
- Complete sources are available on the Web pages:
1. Numeric systems
2. Basic structure of the program
3. Algorithms
4. Variables and data in C language General expression of integer number : Used systems :
Used symbols are 0 – 9 and letters from alphabet for systems with basis over 10: e.g. for hexadecimal system:
A = 10 B = 11 C = 12 D = 13 E = 14 F = 15
Example for hexadecimal number expression: Hex. number 0x3CF0 represents decimal value:
3 · 4096 + 12 · 256 + 15 · 16 = 15600
- storage in memory (generally according to standard IEEE754)
m E S
M x e
2
1
2
1 1 2 1 S is sign bit (+ = 0; - = 1) E is binary value of exponent M is binary value of mantissa
General expression of rational (floating point) number:
How to create a computer What is a computer program? program?
Assembler = readable machine code using name abbreviations derived from instruction meaning, e.g. MOV, INC, ADD, and their operands (name of register, address in memory, inserted header files (libraries) notices header of
#include <math.h> // precompiled header files
main
#include <stdio.h>
function
int main( void ) // header of main function
body of
{ // body of main function
function
char c; // variable declaration
variable
printf( "ahoj" ); // printing fuct. (stdio.h)
declaration
scanf( "%c" , &c );// wait until key pressed (stdio.h) return 0;
calling of
}
library functions command for comeback from
- Application solutions i
- CONSOLE APPLICATION (independent on platform) – ANSI C
• APPLICATION DEPENDENT ON PLATFORM (for given OS with utilize
offered functions, for given microcontroller with utilize its peripheral) –
extensive libraries, e.g. API, MFC, Active X- Integrated Development Environment IDE
– complete set of tools for application creation
including wizards for project setting, e.g. MS Visual
Studio open source environments, e.g. Code::Blocks or -
Eclipse or CodeLite (license is not required)
- Code::Blocks – our programming tool
- Project
- Compiler language setting
- Project name setting
- Kind of compiler setting
- Debug version vs. release version
- debugging and executing of an application – menu DEBUG
- Where you can obtain Code::Blocks ??
- Algorithms defines a procedure of operations with data
- Fundamental aspects of algorithm:
- Basic entities of algorithm:
- • Variables • Start of algorithm • End of algorithm • Steps of algorithm • Subroutines • Conditions (binary relations) • Decision making • Sequences • Cycles
- Lexical
- – native form: textual (intellectual) guide, order,
- Graphical
- – flowchart – transparent visualization of complex algorithms including branching by using defined flowchart symbols with description and oriented flowlines
- Symbolic
- – algorithm description by exactly defined syntax (key word of programming language; meta-
• Program that compute roots of
any quadratic equation:- Data
- – I (Input), – O (Output) , auxiliary (used for internal results)
- Data sources a targets
- – file (I/O – binary, text), keyboard (I), console (O), printer (O), communication port (I/O) – e.g. serial port, USB, LPT
- often the functions for programming with data sources and targets are defined as functions for files
- Kind of data (variables)
- – numerical (integer or rational), characters and strings (string is array of characters terminated by character NULL), arrays (one-dimensional = vectors, multidimensional = matrix, 3D matrix, tensor …), pointer (reference to position in memory), structure (defined
- Named place in memory with the desired size (in bytes)
- GLOBAL
- – valid in whole program
- LOCAL – valid in body of function (only)
- Types of variables in ANSI C language
- – numeric types
- -38 < |X| < 3,40 10 +38
- -308 < |X| < 1,79 10 +308
- -4932 < |X| < 1,18 10 +4932
• Numeric types – fixed point for integral numbers
- unsigned number - storing in memory
- signed number - storing in memory
- Example
- Example
- Complementary code
- Example for
- One is subtracted from absolu-
- Simpler hardware
- Numerical floating point types
- storage in memory (generally according to IEEE754 standard)
- Numerical floating point types
- Float (4 bytes)
- Double (8 bytes)
- Types of variables
- – array
- String: an array of characters
- NTS
- Array elements: pointers to characters
• String as an array of ASCII codes of characters in
memory:- Special characters:
- Types of variables
- – pointer (= address of variable in memory)
new project open a previously created project
console application = text output in
projects for microcontrollers AVR 8 bits
ARM 32 bits
GNU – open free software
DEBUG – excellent debugging, final code is not optimized
start of debugging, program stops at breakpoints stepping in program including jumps to functions, by Step out stepping in program according to the source code without jumps to functions breakpoint inserting to given row (where the cursor actually is placed) stop of debugging
(program can be in undefined cycle state)
Finiteness – algorithm consists of elementary steps (of
procedure or process), whereas their number must be
finite
Necessatarianism – algorithm must be punctually and
clearly defined Inputs/outputs – algorithm has to defined unambiguousinputs and outputs (data, e.g. given structure of a file)
Effectiveness – algorithm must be effective and optimally used system capability (effectiveness in point of view operation time vs. occupation of operation
Versatility – algorithm has to be able to work with required data set in required range
Resources for algorithm definition (description):
law – often not too exact definition, then transcription to
programming language is impossibleFlowchart - symbols: Algorithm start or Manual input (e.g. algorithm end from keyboard) Output displaying Common statement File processing Conditional branching Data saving to a file Cycle with defined number of repetitions Subroutine Cycle with condition Joiner in the end Cycle with condition Oriented flowline in the beginning
Flowchart - example:
2
ax + bx + c =
Read coefficients from
keyboard. If roots are complex,
print error message.2 b b 4 ac
y
1 ,
2
float a, b, c, y1, y2, r; printf( "a = " ); scanf( "%f" , &a); printf( "b = " ); scanf( "%f" , &b); printf( "c = " ); scanf( "%f" , &c); getchar(); r = b*b-4*a*c; if (r>=0) { y1 = (-b+sqrt(fabs(r)))/(2*a); y2 = (-b-sqrt(fabs(r)))/(2*a);
printf( "\n 1st root: %.3f" , y1);
printf( "\n 2nd root: %.3f" , y2);
} elseprintf( "\n Result is not real" );
// precompiled header files #include <stdio.h> // standard inputs and outputs int a = 5; // global variable int main( void ) // main function { int b = 10; // local variable char c; printf( "global: %d\r\n " , a); printf( "local: %d\r\n " , b); scanf( "%c " , &c); return 0; }
Type Bits Range unsigned char
8 X <0, +255> char
8 X <-128, +127> short int
16 X <-32.768; +32.767> unsigned int
32 X <0; +4.294.967.295> int
32 X <-2.147.483.648; +2.147.483.647> float
32
1,18 10
double
64
2,23 10
long double
80
3,37 10
(8 bits) unsigned char
(8 bits) – standard expression signed char
signed (8 bits) – char
complementary coded te value of coded number and negated
m E S
M x e
2
1
2
1 1 2 1 S is sign bit E is binary value in exponent M is binary value in mantissa
23 127
2
1
2
x E S
52 1023
2
1
2
1 M x E S
Float (4 bytes) Double (8 bytes)
23 127
2
1
2
1 M x E S
52 1023
2
1
2
1 M x E S
#include "stdio.h" int main( void ) { int A[5] = {0, 1, 2, 3, 4}; int B[3][2] = {0, 1, 2, 3, 4, 5}; int i,j; char c; for ( i=0; i<5; i++)
printf( "A[%d] = %d\r\n" , i, A[i]);
B[0][0] = A[4]; for ( i=0; i<3; i++) { for ( j=0; j<2; j++) printf( "B[%d,%d] = %d\r\n" , i, j, B[i][j]); } scanf( "%c" , &c); return 0; }(in memory: 1 byte/character – ASCII coding) (Null Terminated Strings) : the last character of
string null – spec. notation v C/C++ ’\0’
\b - backspace BS \f - form feed FF (also clear screen) \n - new line NL \r - carriage return CR \t - horizontal tab HT \v - vertical tab (not all versions) \“ - double quotes (not all versions) \' - single quote character ' \\ - backslash character \
\ddd - character ddd , where ddd is an ASCII code
given in octal base \xhhh - character hhh , where hhh is an ASCII code#include "stdio.h" int main( void ) { float x = 3.14, y = 2.27; float* p; // address of float variable p = &x; // address of x to p *p = y; // content of y on address in p return 0; }
1. Expressions
2. Arithmetic conversions
3. Operators
4. Statements if-else and for THANK YOU FOR YOUR ATTENTION