370 likes | 454 Views
Computers a nd Programming CPC. The 1 st lecture Jiří Šebesta. Complete sources are available on the Web pages:. http://www.urel.feec.vutbr.cz/~sebestaj/ C PC. TOPIC. Numeric systems Basic structure of the program Algorithms Variables and data in C language. Numeric systems (1/ 3 ).
E N D
Computers andProgramming CPC The 1st lecture Jiří Šebesta • Complete sources are available on the Web pages: http://www.urel.feec.vutbr.cz/~sebestaj/CPC
TOPIC Numeric systems Basic structure of the program Algorithms Variables and data in C language
Numeric systems (1/3) Used systems : Generalexpression of integer number :
Numeric systems (2/3) 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
Numeric systems (3/3) - storage in memory (generally according to standard IEEE754) Generalexpression of rational (floating point) number: S is sign bit (+ = 0; - = 1) E is binary value of exponent Mis binary value of mantissa
Basic structure of the program (1/10) How to create a computer program? What is a computer 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, numerical constant…
Basic structure of the program (2/10) notices headerof main function #include <math.h>// precompiled header files #include <stdio.h> int main(void) // header of main function { // body of main function char c; // variable declaration printf("ahoj"); // printing fuct. (stdio.h) scanf("%c", &c);// wait until key pressed (stdio.h) return 0; } body of function insertedheader files (libraries) variable declaration calling of library functions command for comeback from function with return value (0)
Basic structure of the program (3/10) • CONSOLE APPLICATION (independent on platform) – ANSI C • APPLICATION DEPENDENT ON PLATFORM (for given OS withutilizeoffered functions, for given microcontroller with utilize its peripheral) – extensivelibraries, 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) Application solutions in C:
Basic structure of the program (4/10) new project • Project open a previously created project projects for microcontrollers AVR 8 bits ARM 32 bits Code::Blocks – our programming tool console application = text output in DOS box
Compiler language setting Basic structure of the program (5/10)
Project name setting Basic structure of the program (6/10)
Kind of compiler setting Basic structure of the program (7/10) GNU – open free software GCC = GNU Compiler Collection
Debug version vs. release version Basic structure of the program (8/10) DEBUG – excellent debugging, final code is not optimized RELEASE – final version – optimized code for distribution
debugging and executing of an application – menu DEBUG Basic structure of the program (9/10) start of debugging, program stops at breakpoints stop ofdebugging (program can be in undefined cycle state) steppingin program according to the source code without jumps to functions breakpoint inserting to given row(where the cursor actually is placed) stepping in program including jumps to functions, by Step out given function can be early leaved Příklad: Ex01.c
Where you can obtain Code::Blocks ?? Basic structure of the program (10/10)
Algorithms defines a procedure of operations with data Algorithms (1/6) • Fundamental aspects of algorithm: Finiteness – algorithm consists of elementary steps (of procedure or process), whereas their number must be finite Inputs/outputs – algorithm has to defined unambiguous inputs and outputs (data, e.g. given structure of a file) Necessatarianism – algorithm must be punctually and clearly defined Effectiveness – algorithm must be effective and optimally used system capability (effectiveness in point of view operation time vs. occupation of operation memory or code length)
Versatility – algorithm has to be able to work with required data set in required range Algorithms (2/6) • Basic entities of algorithm: • Variables • Start of algorithm • End of algorithm • Steps of algorithm • Subroutines • Conditions (binary relations) • Decision making • Sequences • Cycles • Jumps (to use minimally)
Resources for algorithm definition (description): Algorithms (3/6) • Lexical – native form: textual (intellectual) guide, order, law – often not too exact definition, then transcription to programming language is impossible • 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-language – generative grammar)
Flowchart - symbols: Algorithms (4/6) Algorithm start or algorithm end Manual input (e.g. 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 in the end Joiner Cycle with condition in the beginning Oriented flowline
Flowchart - example: Algorithms (5/6) Program that compute roots of any quadratic equation: ax2 + bx + c=0 Read coefficients from keyboard. If roots are complex, print error message.
Algorithms (6/6) 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); } else printf("\nResult is not real"); Příklad:Ex02.c
Data – I (Input), – O (Output) , auxiliary (used for internal results) Data and variables (1/15) • 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 encapsulated set of variables with different types)
Named place in memory with the desired size (in bytes) • GLOBAL – valid in whole program • LOCAL– valid in body of function (only) Data and variables (2/15) // 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; } Example: Ex03.c
Typesof variables in ANSI C language– numerictypes Data and variables (3/15)
Numeric types – fixed point for integral numbers Data and variables (4/15) - unsigned number - storing in memory - signed number - storing in memory
Exampleunsigned char (8 bits) Data and variables (5/15) • Examplesigned char (8 bits) – standard expression
Complementary code • Exampleforsigned char (8 bits) – complementary coded Data and variables (6/15) • One is subtracted from absolu-te value of coded number and negated • Simpler hardware implementation of ALU
Numerical floating point types Data and variables (7/15) - storage in memory (generallyaccording to IEEE754 standard) S is sign bit E is binary value in exponent Mis binary value in mantissa
Numerical floating point types Data and variables (8/15) Float (4 bytes) Double (8 bytes)
Float (4 bytes) Data and variables (9/15)
Double (8 bytes) Data and variables (10/15)
Typesof variables – array Data and variables (11/15) #include"stdio.h" intmain(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; } Example: Ex04.c
Data and variables (12/15) • NTS (Null Terminated Strings) : the last character of stringnull – spec. notation v C/C++’\0’ • Array elements:pointers to characters String: an array of characters(in memory: 1 byte/character – ASCII coding)
Data and variables (13/15) String as an array of ASCII codes of charactersin memory:
Data and variables (14/15) \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 given in hexadecimal base Special characters:
Typesof variables – pointer(= address of variableinmemory) Data and variables (15/15) #include"stdio.h" intmain(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; } Example: Ex05.c
TOPICS OF THE NEXTLECTURE Expressions Arithmetic conversions Operators Statements if-else and for THANK YOUFOR YOUR ATTENTION