lOMoARcPSD|12264716 Csci 114 Eos Study Procedural Programming (University of Wollongong) Studocu is not sponsored or endorsed by any college or university Downloaded by Jason Jason (fqtsys@hotmail.com) lOMoARcPSD|12264716 CSCI 114 EOS study Lecture 1 Computer: machine to follow instructions Program: instructions for the computer Programmer: person who writes the programs They are all dependant on each other! Computer system Hardware Input device Central processing unit Primary storage or main memory Output devices auxiliary storage devices Software System software Manages system resources, provides the interface between the users and hardware, does not directly serve the users Application software Directly serve the user’s needs Buses all data to be passed between the components I/O devices CPU Memory CPU – performs the basic operations Arithmetic logic unit – data manipulation Retrieves and decodes the programs instructions Coordinates the activates of the of parts of the computer Control unit – regulates machines activities Hardware optimized for high-speed numeric calculation Hardware designed for true/false decisions Main memory holds programs and data Stores bits in fixed sized chunks: “word” (8, 16, 32, 64 bits) Each word is stored in a cell which has a unique address The cells can be accessed in any order hence RAM Volatile 8 bits make a byte Time sharing environment In the time sharing environment many users are connected to one or more computers These computers may be minicomputers r central mainframes The output devices are shared by all the users The computing is done by the central computer Downloaded by Jason Jason (fqtsys@hotmail.com) lOMoARcPSD|12264716 The central computer manages the shared data and printing and it must also do the computing Client/server Client server environment splits the computing function between a central computer and users computers The user have pc or workstation with some computation responsibility Users are called client and central computer is the server Types of languages Low level: used for communication with computer hardware directly. Often written in binary machine code High level: closer to human language Each computer has its own machine language The instruction in machine language must be in streams of 1 and 0, because internal circuits and transistors can only use this. Symbolic language uses symbols (mnemibics) to represent the low level languages Program called and assembler is used to translate Because symbolic language has to be assemble into machine language Lecture 2 Internal representation of data and Numbering systems. The Decimal, hexadecimal, octal and Binary system: Decimal is base 10 and uses 10 symbols: 0 1 2 3 4 5 6 7 8 9 Binary is base 2 and uses 2 symbols: 0 1 Hexadecimal is base 16 and uses 16 symbols: 0 1 2 3 4 5 6 7 8 9 A B C D E F Octal is base 8 and uses 8 symbols: 0 1 2 3 4 5 6 7 Decimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Binary 0 01 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 Octal 0 1 2 3 4 5 6 7 10 11 12 13 14 15 Downloaded by Jason Jason (fqtsys@hotmail.com) Hexidecimal 0 1 2 3 4 5 6 7 8 9 A B C D lOMoARcPSD|12264716 14 15 16 17 18 19 20 1110 1111 10000 10001 10010 10011 10100 16 17 20 21 22 23 24 E F 10 11 12 13 14 The value of a number depends on the magnitude of the digits expressing it and on their position. To convert an integer to decimal: d *Base i where d is the magnitude and i is the digit starting from 0 from right to left. Example for the decimal number 46538 (decimal): magnitud e Digit position base 4 6 5 3 8 4 3 2 1 0 10 10 10 10 10 Therefore: (4*104) + (6*103) + (5*102) + (3*101) + (8*100) = 40000 + 6000 + 500 + 30 + 8 = 46538 To convert a decimal number to binary, divide the number by 2. If there is no remainder record a zero, if there is a remainder record a 1. Now repeat the process with the result from the previous division until you either divide 0 or 1. The binary equivalent is the recording of 1s or 0s backwards. Example: decimal number 543 543 | 1 271| 1 135 | 1 67 | 1 Therefore the binary number is 1000011111 33 | 1 16 | 0 8|0 4|0 2|0 0|1 Binary to Octal, divide the binary number into chunks of 3 (as 2 3 = 8, 8 being octal base, 2 being binary base). Then convert each chunk into its octal digit. Example binary number 1100111010: 1|100|111|010 1| 4 | 7 | 2 Therefore the octal number is 1472 Downloaded by Jason Jason (fqtsys@hotmail.com) lOMoARcPSD|12264716 Binary to hexadecimal, same as octal except divide the binary number into chunks of 4 (as 2 4 = 16, 16 being the base for hexadecimal, 2 being binary base). Then convert each chunk into its hex digit. Example binary number 1100111010: 11|0011|1010 3|3 |A Therefore the Hex number is 33A Adding Binary numbers: 0 + 0 = 0, 1 + 0 = 1, 1 + 1 = 0 and carry the 1, 1 + 1 + 1 = 1 and carry the 1. Example: 11101010 01101101 + = 101010111 Subtracting Binary numbers: 0 – 0 = 0, 1 – 0 = 1, 0 – 1 (grab the next 1 to make 10), 1 – 1 = 0 10 – 1 = 1. Example: 11101010 01101101 – = 1111101 Storing integers. An integer is a number without a decimal part. Integers can be unsigned integers and signed integers. C++ supports 3 different sizes of integers: short, int and long int. Let us assume an integer is two bytes (16 bits) long. A n-bit word can represent 2n different numbers. Thus a 16-bit word can represent 216 = 65536 different integers from 0 to 65534. Since negative integers must also be represented we need to reserve one bit for the sign. Three methods can be used: Signed and magnitude uses one bit to represent the sign (0 for positive 1 for negative). It means there are only 15 bits to represent the absolute value of the number. Example: +0 = 0 000000000000000 -0 = 1 000000000000000 +32767 = 0 111111111111111 -32767 = 1 111111111111111 One’s compliment where negative numbers are stored in their complemented format. The sign bit is propagated to the most significant bit of the value and the numbers inversed. Example: +11 = 0000 0000 0000 1011 -11 = 1111 1111 1111 0100 Downloaded by Jason Jason (fqtsys@hotmail.com) lOMoARcPSD|12264716 Two’s compliment, as with one’s compliment the numbers are inversed. Then add 1 to the rightmost number. Example: -11 = 1111 1111 1111 0101 Storing Floats (basic) The mantissa (without the decimal point) and the exponent (without the base) is stored. ASCII code 0 = 46, A = 65, a = 97 Lecture 3: C++ Basics and coding style. Syntax is the rules of grammar for a conventional language. It describes how the symbols in the alphabet of the language can be put together to form meaningful sequences. This indicates what makes up names, operators and other statements in the language. Semantics defines what theses legal sequences of symbols mean (what the computer is supposed to do when it meets these tokens.) Stucture of a c++ program: - Should start with a header – essential to identify the file, its purpose and who wrote it etc. - Pre-processor directives (eg: #include <iostream>) are special instructions to the preprocessor that tell it how to prepare the program for compilation. - Global decleration section appears at the beginning of the program. Every c++ program is made up of a global decleration section and one or more functions. - The main function, most C++ programs have one. o Any function may contain two types of code: decleration and statements. Variables - Variables are named memory locations that have a type, such as int or char and consequently a size inherited from their type. Each variable must be declared and defined. - A decleration creates the variable, a definition names it. Basic Data Types - Void, int, char, float, bool - Short int: 2 bytes, int: 2 or 4 bytes, long int: 4 bytes - Float: 4 bytes, double: 8 bytes, long double: 10 bytes - Bool: 1 = true, 0 = false - Char: ‘a’ 1 byte, smallest integral data type. Lecture 4 Operators and Expressions Identifiers are the names given to the variables. The first character must be a letter or an underscore. Only alphabetic characters, digits and underscores may be used. Reserved words cannot be used. Downloaded by Jason Jason (fqtsys@hotmail.com) lOMoARcPSD|12264716 Constants are data values that cannot be changed during the program execution <const>. Arithmetic operators and precedence: Lecture 5 Simple I/O. Increment and decrement: X++ : Post increment: X is incremented by one after the expression is evaluated. ++X : Pre increment: X is incremented by one before the expression is evaluated. The same applies to Pre and Post decrement. Stream extractor operator “>>” Skips whitespace characters – blanks, tabs and newlines. Character I/O functions: get() – reads the next character from the input stream and returns its value through a reference parameter. It can read any character including whitespace and control characters. “cin.get(aChar)” clear() – clears the input buffer “cin.clear()” ignore() – ignores items in the input buffer up to a certain number of characters or a specific character. “cin.ignore(200, ‘\n’)” will ignore 200 characters or until it sees a newline byte. Formatting output. Include <iomanip> setiosflags(ios::fixed) setiosflags(ios::showpoint) – show decimal point setprecision(2) – how many places after the decimal is to be shown setw() – defines the width of a field “setw(15)” setfill() – defines what to fill empty space with “setfill(‘*’)” Lecture 6. Downloaded by Jason Jason (fqtsys@hotmail.com) lOMoARcPSD|12264716 Typecasting Eg: static_cast<int>(6.7) = 6 static_cast<int>(6.2) = 6 static_cast<double>(7/2) = 3.0 static_cast<double>(7)/2 = 3.5 static_cast<int>(‘A’) = 65 Lecture 7 control structures The if statement: can be nested and cascaded. If (x > 32) Do this… else Do this… Lecture 8 Conditional expressions provide an alternate way of expressing simple if-else statements: (condition ? expression1(if true) : expression2(if false)) The Switch statement provides an alternative for cases where the value of an integer expression is compared to a specific value: Switch (expression) { Case const_expr1: statement1; break; case const_expr2: statement2; break; default: statement3; } Lecture 9 The while-do loop: While (expression) Statement The guarded loop is known as a sentinel-controlled while loop. Data values used to signal wither the start or end of a data series are called sentinels. One thing that must be provided in the body of the loop is a possibility for the guard condition to be changed. Downloaded by Jason Jason (fqtsys@hotmail.com) lOMoARcPSD|12264716 The for loop is apre-test loop that uses three expressions: the initialisation expression, the conditional expression and the updating expression. For (init_expr; test_expr; update_expr) Statement; We use do-while loops when we want the body of the loop to execute at least once: do { statement; } while (expression); Lecture 10 Functions The best way to develop and maintain a large computer program is to construct it from smaller components or modules. Modules in C++ are functions and classes. Two main types of functions: Pre-packaged – found in header files. User defined – written by the programmers Functions allow the reuse of code that is required more than once. Functions allow the protection of data: local data cannot be seen or changed by code outside the function. Function declaration is done in the global area, calling is done in the statement section, definition is done after calling. With a user defined function you need to specify: Name Parameters Type of data it returns What it returns Block of statements to be carried out when the function is called. Function libraries come in two parts, the code or executable portion that performs the task and the interface which guides the compiler in how to reference the functions. Function header syntax: return_type function_name (parameter list) Eg void myFunc (int a, int b) Function prototype syntax: return_type function_name (parameter_data_types) Eg void myFunc (int, int); Function call syntax: function_name (passing parameters) Eg myFunc (a, b); Downloaded by Jason Jason (fqtsys@hotmail.com) lOMoARcPSD|12264716 The return statement tells trhe function which value to return to the calling function. It also terminates the function call and returns immediately to the calling function. Parameters: Actual parameters are variables or values passed to the function when it is called. Formal parameters are in the function and are the local variables that will contain the data received by the function. If a function does not take any parameters, declare its formal argument list void. Parameters passed by value are passed by copying the value of the actual parameters to the formal parameters. Any changes to the formal parameters do not affect the value of the actual parameters. Variables declared in a function body (including formal parameters) are only accessible while the function is executing. They have a local scope. Functional decomposition is the procedure of splitting large programs into smaller parts, each written as a function, and each solving a sub-problem. An individual function should perform one task only. It only needs as input the values required for the task. It should only return the value for which it is designed. Lecture 11 functions 2 When parameters are passed by reference the calling function has direct access to the memory address of the values and may modify them. The & is used to pass by reference: Decleration/prototype: void myFunc (int &); Function call: myFunc (x); Definition: void myFunc (int &y) Variables declared in the global area of a program are visible from their point of decleration until the end. They have a global scope. Variables have a defined lifetime. Where and how long a variable’s storage location is kept before it is released is determined by the storage calss of the variable. There are four in C++: auto, extern, static and register. If a storage class is used, it must be placed before the variable’s data type: “extern int x;” Local variables cannot be members of the extern storage class. Storage for all variables except register is reserved in the computer’s memory area. Auto is the default class of local variables. Storage for auto variables is automatically created each time a function defining auto variables is called. The variable is alive while the function is executing, it dies when the function returns control to its function call. A variable declared as a static variable causes the program to keep it alive even when control has been returned to the calling function. They stay alive for the life of the program. Local static variables are initialised once, generally when the program is compiled. Static variables can only be initialised by constants or constant expressions. Downloaded by Jason Jason (fqtsys@hotmail.com) lOMoARcPSD|12264716 Register variables have the same life duration as auto though they are stored in a different area: high speed storage areas called registers (in the processing unit). Register variables are automatically switched to auto if the computer’s register capacity is exceeded. The address operator ‘&’ cannot be used with register variables. Global variables can be defined as static or declared as extern. The extern storage class extends the scope of a global variable. An extern declaration tells the complier that a global variable already exists (it ahs been defined in another source file) and can be used in the current source file. The scope of a static global variable can not be extended beyond the file in which it is created. Overhead can be avoided for simple functions by using inline functions. These in essence eliminate the call by duplicating the contents of the function wherever it is used substituting the actual arguments for the formal ones. Whether to make a function inline depends on size vs. time. Downloaded by Jason Jason (fqtsys@hotmail.com)