COP3104 Melina Test 1 Fall 2017

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

High-level procedural languages

Abstraction of concepts into more human-readable terms Closer to "natural language" (i.e. what we speak) Easy to write and design, *but must be translated for computer* Examples include C, Pascal, Fortran

Static Cast

Better to use newer C++ cast operators. For casting between regular variables, use static_cast c1 = static_cast<char>(i2); i1 = static_cast<int>(d2);

Statement -- smallest complete executable unit of a program

Declaration statement Execution statement Compound statement -- any set of statements enclosed in set braces { } (often called a block) Simple C++ statments end with a semi-colon. (A block does not typically need a semi-colon after it, except in special circumstances).

Delete

Delete

4) Linking

Final stage of the creation of an executable program. Linking of object code files together with any necessary libraries (also already compiled)

integer literal

an actual integer number written in code (4, -10, 18) If an integer literal is written with a leading 0, it's interpreted as an octal value (base 8). If an integer literal is written with a leading 0x, it's interpreted as a hexadecimal value (base 16) Example: int x = 26; // integer value 26 int y = 032; // octal 32 = decimal value 26 int z = 0x1A; // hex 1A = decimal value 26

Short- cut assignments

first term equals first term (operator) second term v += e; means v = v + e; v -= e; means v = v - e; v (star) = e; means v = v * e; v /= e; means v = v / e; v %= e; means v = v % e;

for loop

for (initialCondition; testExpression; iterativeStatement) { statement1; statement2; // ... statementN; } ex: int i, sum = 0; for (i = 1; i <= 50; i++) { sum += i; } http://www.cs.fsu.edu/~vastola/cop3014/examples/control/for/counting.cpp

scope for loop

for (int counter = 0; counter < 10; counter++) { // loop body } cout << counter; // illegal. counter out of scope This can be avoided by declaring the control variable before the loop itself. int counter; // declaration of control variable for (counter = 0; counter < 10; counter++) { // loop body } cout << counter; // OK. counter is in scope

Evolution of Programming Languages

http://www.cs.fsu.edu/~vastola/cop3014/review1.html

cascading

linking of multiple operators, especially of related categories, together in a single statement: x = a + b + c - d + e; // cascading arithmetic operators x = y = z = 3; // cascading assignment operators This works because the result of one operation sends back the answer (i.e. a return value) in its place, to be used in the next piece of the statement. In the above, (a + b) happens first, then the answer becomes the first operand in the next + operation.

operand and arity

operand -- an input to an operator Arity - how many operands an operator takes: unary operator -- has one operand binary operator -- has two operands ternary operator -- has three operands Examples: int x, y = 5, z; z = 10; // assignment operator (binary) x = y + z; // addition (binary operator) and any arithmetic function (+, *, /, %) x = -y; // -y is a unary operation (negation) x++; // unary (increment) double x=19.0 , y = 5.0, z; z = x / y; // z = x%y is not legal

1) create Source code

plain text file, usually given a filename extension to identify the programming language (like .c for C, or .cpp for C++)

An operation of mixed types

returns the larger of the two int x = 5; float y = 3.6; z = x + y; // what does z need to be? x + y returns a float.

logical operator

x == y x > y x >= y (x > 0 && y > 0 && z > 0) // all three of (x, y, z) are positive (x < 0 || y < 0 || z < 0) // at least one of the three variables is negative

Increment and Decrement Operators

++x; // pre-increment (returns reference to new x) x++; // post-increment (returns value of old x), shortcuts for x = x + 1 --x; // pre-decrement x--; // post-decrement shortcuts for x = x - 1 by themselves have the same effect Examples int x = 5, count = 7; result = x * ++count; // result = 40, count = 8 int x = 5, count = 7; result = x * count++; // result = 35, count = 8 count increases value is used in the rest of the expression

Object-oriented language

- Abstraction taken farther than procedural languages - Objects model real-world objects, not only storing data (attributes), but having inherent behaviors (operations, functions) - Easier to design and write good, portable, maintainable code - Examples include Smalltalk, C++, Java

Function

- reusable portion of a program, sometimes called a procedure or subroutine Like a mini-program (or subprogram) in its own right Can take in special inputs (arguments) Can produce an answer value (return value) Similar to the idea of a function in mathematics Why write and use functions? Divide-and-conquer: - Can breaking up programs and algorithms into smaller, more manageable pieces - This makes for easier writing, testing, and debugging - Also easier to break up the work for team development Reusability: - Functions can be called to do their tasks anywhere in a program, as many times as needed - Avoids repetition of code in a program - Functions can be placed into libraries to be used by more than one "program" Builder of the function -- responsible for creating the declaration and the definition of the function (i.e. how it works) Caller -- somebody (i.e. some portion of code) that uses the function to perform a task

Creation of C++ program

-Create source code (filename.cpp) -Preprocessing (compiler process, pre-process tasks on source code) -Compilation (syntax checking, create object code (translation of source code)) -Linking (Final stage of creation of an executable program) -Execution (program in RAM, executes code instructions)

Do while/while

// while loop format while (expression) { statement1; statement2; // ... statementN; } // do-while loop format do { statement1; statement2; // ... statementN; } while (expression); In a while loop, the expression is tested first In a do/while loop, the loop "body" is executed first

char - short - int - long -

1 byte at least 2 bytes 4 bytes at least 4 bytes

3) Compilation

Syntax checking, creation of object code Object code is the machine code translation of the source code

using (calling) / building (declaring) a function

The user of a function is the caller. Use a function by making calls to the function with real data, and getting back real answers. Consider a typical function from mathematics: functionName(argumentList) ex/ cout << sqrt (100.0); ex/ z = sqrt(x); Declaring and Defining: The builder of a function (a programmer) is responsible for the declaration (also known as prototype) and the definition. A function declaration, or prototype, specifies three things: - The function name -- usual naming rules for user-created identifiers - The return type -- the type of the value that the function will return (i.e. the answer sent back) - The parameter list -- a comma separated list of parameters that the function expects to receive (as arguments) return-type function-name( parameter-list ); ex/ double Average(double, double, double); //no parameter names here, okay on a declaration int DoTask(double a, char letter, int num); Definition Format: return-type function-name( parameter-list) { function-body (declarations and statements) } return expression; ex/ int Sum(int x, int y, int z) { int answer; answer = x + y + z; return answer; } double Average (double a, double b, double c) // add the parameters, divide by 3, and return the result { return (a + b + c) / 3.0; } bool InOrder(int x, int y, int z) // answers yes/no to the question "are these parameters in order, // smallest to largest?" Returns true for yes, false for no. { if (x <= y && y <= z) return true; else return false; }

Assignment Operator

Value on the right side (R-value) is assigned to (i.e. stored in) the location (variable) on the left side (L-value) L-value - storage location R-value - expression that evaluates to a single value ex/ assign T or F: bool flag; flag = true; flag = false;

escapes sequence

\ backslash, single quotes \n newline \t tab \r carriage return move cursor to start of line \a alert sound \" double quote \' single quote \\ backslash

character literal string literal

a character in single quotes: ('F', 'a', '\n') a string in double quotes: ("Hello", "Bye", "Wow!\n")

floating point literal

an actual decimal number written in code (4.5, -12.9, 5.0) Note: these are interpreted as type double by standard C++ compilers also scientific notation: 3.23e-12

example of switch

#include <iostream> using namespace std; int main() { int score; char grade; cout << "\nPlease enter test grade: "; cin >> score; switch (score / 10) { case 10: // score is 100 - 109 case 9: // score is 90 - 99 grade = 'A'; break; case 8: // score is 80 - 89 grade = 'B'; break; case 7: // score is 70 - 79 grade = 'C'; break; case 6: // score is 60 - 69 grade = 'D'; break; default: grade = 'F'; // anything below 60 flunks } cout << "\nStudent grade = " << grade << endl; return 0; } http://www.cs.fsu.edu/~vastola/cop3014/examples/control/switch/switch3.cpp

Predefined Functions

#include <iostream> // common I/O routines #include <cmath> // common math functions #include <cstdlib> // common general C functions

Scope of Identifiers

A global variable is declared outside of any blocks, usually at the top of a file, and is usable anywhere in the file from its point of declaration - best to avoid global variables A variable declared within a block (i.e. a compound statement) of normal executable code has scope only within that block. http://www.cs.fsu.edu/~vastola/cop3014/examples/func/scope1.cpp

switch statement

A switch statement is often convenient for occasions in which there are multiple cases to choose from. The syntax format is: switch (expression) { case constant: statements case constant: statements ... (as many case labels as needed) default: // optional label statements } EXAMPLE: #include <iostream> using namespace std; int main() { int score; char grade; cout << "\nPlease enter test grade: "; cin >> score; switch (score / 10) { case 10: // score is 100 - 109 case 9: // score is 90 - 99 grade = 'A'; break; case 8: // score is 80 - 89 grade = 'B'; break; case 7: // score is 70 - 79 grade = 'C'; break; case 6: // score is 60 - 69 grade = 'D'; break; default: grade = 'F'; // anything below 60 flunks } cout << "\nStudent grade = " << grade << endl; return 0; }

Automatic Type Conversions

For atomic data types, can go from "smaller" to "larger" types when loading a value into a storage location. General rule of thumb: Allowed if no chance for partial data loss char -> short -> int -> long -> float -> double -> long double Should avoid mixing unsigned and signed types, if possible Examples: int i1, i2; double d1, d2; char c1; unsigned int u1; d1 = i1; // legal. c1 = i1; // illegal. trying to stuff int into char (usually 1 byte) i1 = d1; // illegal. Might lose decimal point data. i1 = c1; // legal u1 = i1; // dangerous (possibly no warning) d2 = d1 + i2; // result of double + int is a double d2 = d1 / i2; // floating point division (at least one operand a float type)

floating point types

For storage of decimal numbers (i.e. a fractional part after the decimal) float double long double

ISA

Instruction Set Architecture: the specific set of low-level instructions available to a CPU. Differs for various CPU types (Intel Pentium, Mac G4, etc)

Library: two parts

Interface: header file, which contains names and declarations of items available for use #include <iostream> Implementation: pre-compiled definitions, or implementation code. In a separate file, location known to compiler starts with source code pre-processing compiling linking run it

Bridging the gap between high-level code and machine code: Interpreted languages and compiled languages

Interpreted languages: Source code is directly run on an interpreter, a program that runs the code statements Compiled Languages: A compiler program translates source code (what the programmer writes) to machine language (object code) A linker program puts various object code files together into an executable program (or other target type, like a DLL) C and C++ are compiled languages

2) Preprocessor

Part of compiler process, performs any pre-processing tasks on source code

Precedence & Associativity

Precedence - rules specifying which operators come first in a statement containing multiple operators x = a + b * c; // b * c happens first, since * has higher precedence than + Associativity - rules specifying which operators are evaluated first when they have the same level of precedence. Most (but not all) operators associate from left to right.

Break vs. Continue

break: This causes immediate exit from any loop (as well as from switch blocks) http://www.cs.fsu.edu/~vastola/cop3014/examples/control/break.cpp continue: When used in a loop, this statement causes the current loop iteration to end, but the loop then moves on to the next step. http://www.cs.fsu.edu/~vastola/cop3014/examples/control/continue.cpp In a while or do-while loop, the rest of the loop body is skipped, and execution moves on to the test condition In a for loop, the rest of the loop body is skipped, and execution moves on to the iterative statement

cin cout

cin >> istream cout << ostream The extraction operator can be cascaded, as well: int x, y; double a; cin >> x >> y >> a;

sig figs

cout << fixed << showpoint << setprecision(3)

declare and initialize variables

declare variable - tell compiler it exists and reserve memory for it ex/ int hi, h1, yo; initialize variable - load a value into it for the first time ex/ hi = 10; or int hi = 10; int hi(10); char letter = 'a'; const int SIZE = 45; cannot be changed**

delete

delete


संबंधित स्टडी सेट्स

Unit 4 Quizzes (Ch. 24 The Urinary System)

View Set

Lymphatic and Immune Study Guide

View Set

Klett Maximal Interaktiv 1 - Lektion 1.1 (Teil 3)

View Set