Skip to document

APSC 160 Notes

Notes up until first midterm
Course

Introduction To Computation In Engineering Design (APSC 160)

71 Documents
Students shared 71 documents in this course
Academic year: 2020/2021
Uploaded by:
0followers
2Uploads
4upvotes

Comments

Please sign in or register to post comments.
  • Student
    thanks for uploading

Related Studylists

Bithiah

Preview text

APSC 160 Notes

Fundamentals I

● Comment Statement: ○ Included for the benefit of other programmers + ignored by compiler when the program is translated to machine language. ○ Provides information about who wrote it, when it was written and what program does when its run ● Compiler Directive: ○ Instruction for the compiler rather than an instruction to be executed when the program runs ○ All compiler directives in C start with “#” character ● Header File: ○ Contains declaration of symbols used by our programs ○ Ex. ​Stdio ■ Includes declaration of “printf” function that is used by our program to print message on screen ● Function: “​return 0​;” ○ Terminates program, indicates program terminated normally

Fundamentals II

● Abstraction: ​process of hiding non-essential details and only exposing those aspects of a system we are interested in ● Header File “​#define​” ○ Used to create ​symbolic constant​. When compiler executes directive, it searches for all instances for symbol listed beside this header file and replaces it with value beside symbol ■ Symbolic constant: ​makes programs easier to read and maintain. Should be used to represent constant values. Capitalized

Fundamentals III

● Have to end every line with “​;​” ● Variable declaration: ​two parts, type of data to be stored in variable and name of variable (data type, variable name) ○ Type of data depends on what variable is being represented for ○ Variable name is chosen by programmer ○ Variables must be declared before they are used in program

● Garbage value: ​initially variables hold this value. It’s whatever value happens to be stored in memory at that time ● Assignment operator “​=​” ○ Used to assign value to variable, NOT an equals sign ○ Variable receiving assigned value must be on the left ● Whenever the name of a variable appears in code, it is replaced with the value of that variable. Garbage value is overwritten and cannot be retrieved. Variables can only store one value at any given time ● Format specifier ○ Used in printf function as a placeholder for the value of variable ● “​&​” ○ Specifies address

Fundamentals IV

● Integer Data Types ○ Can only store integer values in specified range ○ See Fundamentals IV for chart ○ Choose type “​int​” whenever you want to store integer values ● Unsigned Integer Data Types ○ See Fundamentals IV for chart ○ Only use these if you want to store non-negative values ● Floating Point Data Types ○ Used to store non-integer values ex. 3. ○ Use type “​double​” to store floating point value ● Character data types ○ Letters ○ Type “​char​” consumes single byte of memory and is capable of storing a single character ○ middleInitial = ​‘​A​’ ■ Used single quote marks to represent value ● Identifiers in C ○ They must ​start​ with a letter of the alphabet or an underscore ○ All other characters must be a letter of the alphabet, a digit, or an underscore

Fundamentals V

● Unary operator ○ Only takes one operand (negative) ○ Ex. y = -x ● Binary Operator ○ Takes two operands ○ Addition, subtraction, multiplication, division, remainder (%)

Fundamentals VIII

● All functions in C have common characteristics ○ Have a name ○ 0 or more parameters (inputs) ○ 0 or 1 return value (output) ● Abstraction:​ details of how a function works hidden from programmer who uses function ● Function interface: ​what functions name? ○ Programmer must understand this ● If function does not take any parameters, its list is specified as void ○ Ex. int main(void) ● If function doesn’t return a value, then return type is void ○ Ex void main( void ) ○ No return statement because it doesn’t return anything

Flow of Control I

● Algorithm:​ an unambiguous sequence of step-by-step instructions for solving a problem ○ Basically a sequence of instructions ● Can be specified graphically ○ Using a flowchart ● Algorithms can consist of ○ branching/selection: ​where one sequence of instructions is executed if some condition is true and another if its false ○ looping/repetition: ​where a sequence of instructions is repeated for as long as some condition is true ● Boolean Expression:​ an expression whose value is either true or false ● “​Repeatedly” ○ Algorithm has a loop ● See Flow of Control I for more details

Flow of Control II

● “​!=​” not equal to ○ Unary operand ● “​Count % 2​ ​== 0​” ○ Determine if count is ​even integer ● “​&&”​ ​if both operands are true ○ Binary operand ● “​||​” only one operand has to be true

○ Binary operand ● Helpful to draw table ● See Flow of Control II for precedence of operands

Flow of Control III

● See Flow of Control III for ​simple branching code

Flow of Control IV

● Nested Branching:​ ability to put a branch inside of another branch ○ Can be in true block, false block, or both ● Symbolic constants are useful in updating program if specifications change ● Side by side branches make layout more neutral ● Preferred branching code found on Flow of Control IV ● Braces “{}” are optional when block contains single statement ● See Flow of Control III for ​nested branching​ code

Flow of Control V

● Simple pre-test loop:​ Boolean condition tested before body of loop is executed ○ Body of loop contains statements that will be executed ONLY when Boolean expression is true and will keep executing WHILE Boolean expression is true ● Use trace table to keep track of loops ● See Flow of Control V for ​simple loop​ code

Flow of Control VI

● If no change in variable, there can be an infinite loop ● An error in Boolean expression can also result in an infinite loop ● “Do While” loops ○ Execute function first then ask whether user wants it to continue ● See Flow of Control VI for ​do while loop​ code

Flow of Control VII

● If they want a grid printed, increment rows in outer while loop ● Nested looping:​ when one loop is wrapped inside another one ● Generally redefine value at start of outer while loop, before inner while loop ● Figure out what outer while loop needs to execute then figure out inner while loop, work in modular increments ● See Flow of Control VII for ​nested loop​ code​ and for ​while if ​code

Was this document helpful?

APSC 160 Notes

Course: Introduction To Computation In Engineering Design (APSC 160)

71 Documents
Students shared 71 documents in this course
Was this document helpful?
APSC 160 Notes
Fundamentals I
Comment Statement:
Included for the benefit of other programmers + ignored by compiler when the
program is translated to machine language.
Provides information about who wrote it, when it was written and what
program does when its run
Compiler Directive:
Instruction for the compiler rather than an instruction to be executed when the
program runs
All compiler directives in C start with “#” character
Header File:
Contains declaration of symbols used by our programs
Ex. Stdio.h
Includes declaration of “printf” function that is used by our program to
print message on screen
Function: “return 0;”
Terminates program, indicates program terminated normally
Fundamentals II
Abstraction: process of hiding non-essential details and only exposing those aspects
of a system we are interested in
Header File “#define
Used to create symbolic constant. When compiler executes directive, it
searches for all instances for symbol listed beside this header file and replaces
it with value beside symbol
Symbolic constant: makes programs easier to read and maintain.
Should be used to represent constant values. Capitalized
Fundamentals III
Have to end every line with “;
Variable declaration: two parts, type of data to be stored in variable and name of
variable (data type, variable name)
Type of data depends on what variable is being represented for
Variable name is chosen by programmer
Variables must be declared before they are used in program