MU CS1050 Exam 1

studied byStudied by 51 people
5.0(1)
Get a hint
Hint

Types of computer languages (3)

1 / 85

encourage image

There's no tags or description

Looks like no one added any tags here yet for you.

86 Terms

1

Types of computer languages (3)

Machine languages, assembly languages, high-level languages.

A computer can directly understand only its own machine language; assembly languages were developed to make programming easier.

New cards
2

Phases of executing C programs (6)

Edit, preprocess, compile, link, load, execute.

New cards
3

Compiling

Translates C program into machine language-code (AKA "object code").

New cards
4

Syntax error

Occurs when the compiler can't recognize a statement because it violates the rules of the language; caught by the compiler.

New cards
5

Linking

Uses gcc; links the object code with the code for missing functions contained elsewhere (e.g. standard libraries such as stdio.h or math.h) ; produces an executable image with no missing pieces.

New cards
6

int main (void)

Part of every C program; int denotes that an integer is returned.

New cards
7

Escape sequences

\n , \t , \a , \\ , \"

Starts with a backslash; denotes that the character after the slash will signify something unique (e.g. new line instead of the character n).

New cards
8

Executable

./a.out

Linked program that can be executed

New cards
9

Types of integers & decimals

Integers: short, int, long (used for different sized numbers); decimals: float, double (double has 2x the precision of float).

New cards
10

%d

Used as a placeholder when scanning/printing integers.

scanf( "%d" , &number ); scans in an integer that is assigned to number.

New cards
11

%f

Used as a placeholder when scanning/printing floats/doubles.

scanf( "%f" , &number ); scans a float/double

printf( "%.2f" , number ); denotes precision, will print 2 digits to the right of the decimal place, default precision is .6

New cards
12

Variable names are...

1) Made of letters, digits, underscores and do not begin with a digit, 2) Can be made any length, 3) Case sensitive.

New cards
13

Format control string

First argument in printf/scanf; indicates the type of data that should be input by the user (e.g. %d or %f).

scanf( "%d" , &integer1 ); %d denotes that the user should input an integer.

New cards
14

Conversion specifier

%d, %f

Indicates that the data should be an integer/float/double.

New cards
15

Address operator

'&' character used in scanf and other functions.

New cards
16

Arithmetic operators (operator, algebraic expression, and C expression)

Addition: + , f+7 , f+7

Subtraction: - , p-c , p-c

Multiplication: , bm , bm

Division: / , x/y , x/y

Remainder: % , r mod s , r%s

New cards
17

Integer division...

...ALWAYS yields integer result, unless typecasting is used. Any fractional part of the calculation will be lost/truncated.

int 1 / int 2 = integer result

(float) int 1 / (float) int 2 = float result

New cards
18

Precedence of arithmetic operators

Parenthesis ( ) : Evaluated first; if nested, the innermost parentheses are evaluated before the outer; if there are several sets of parenthesis not nested they're evaluated from left to right.

Multiplication * , division / , remainder % : Evaluated second, evaluated left to right if there are several.

Addition + , subtraction - : Evaluated last, evaluated left to right if there are several.

New cards
19

Sequential execution

Executed from top to bottom, in the order in which they're written.

New cards
20

Equality operators

Algebraic: = , in C: == , example: x == y , meaning: x is equal to y.

Algebraic: (not equal) , in C: != , example: x != y , meaning: x is not equal to y.

New cards
21

Relational operators

> , x > y , x is greater than y.

< , x < y , x is less than y.

>= , x >= y , x is greater than or equal to y.

<= , x <= y , x is less than or equal to y.

New cards
22

Assignment operator vs. equality operator

Assignment operator: = ("gets" or "is assigned the value of")

Equality operator: == ("double equals") , read right to left

New cards
23

Algorithm

A procedure for solving a problem in terms of the actions to be executed and the order in which these actions are to be executed.

New cards
24

Transfer of control

Ability to specify that the next statement to be executed may be other than the next one in the sequence.

New cards
25

Types of selection structures (3)

If statement: Single selection; either performs/selects an action if a condition is true or skips the action if the condition is false.

If...else statement: Double selection; performs an action if the condition is true and performs a different action if the condition is false.

Switch statement: Compound selection; performs one of many different actions depending on the value of an expression.

New cards
26

Repetition structures

While, do...while, for.

New cards
27

'if...else'

Allows you to specify that different actions should be performed when the condition is true or false.

- Else always attaches to the nearest if

- Cannot make one else correspond to two ifs

- Should never use a semicolon at the end

if ( grade >= 60 )

{

printf( "Passed" );

}

else

{

printf( "Failed" );

}

New cards
28

Conditional operator

?:

- Closely related to the if...else statement

- C's ONLY ternary operator

- First operand is a condition, second is the value if the condition is true, third is the value if the condition is false

printf( "%s" , grade >= 60 ? "Passed" : "Failed" );

Grade >= 60 is condition, "Passed" is printed if true, "Failed" is printed if false.

New cards
29

%s

Conversion specifier for printing a character string.

New cards
30

Compound statement/block

A set of statements contained within a pair of braces.

New cards
31

Logic error, nonfatal logic error

Has effect at execution time; produces unintended output. Nonfatal: program continues but produces incorrect results.

New cards
32

Control statements (7)

if, else if, switch, while, do...while, for, sequential.

New cards
33

'while' repetition statement

- Action is performed repeatedly while condition remains true.

- Repetition is terminated when the condition becomes false.

-Tests the condition at the beginning of the loop before the body of the loop is performed.

product = 3;

while (product <= 100)

{

product = 3*product

}

Program will loop until it finds the first power of 3 larger than 100.

New cards
34

Counter-controlled repetition

- Variable counter specifies the number of times a set of statements should execute

- AKA definite repetition; we know how many times it will loop before it begins executing.

- Counters are normally initialized to 0 or 1.

New cards
35

Total

Variable used to accumulate the sum of a series of values.

New cards
36

"Garbage" value

Contained by uninitialized variable; it's the last value that was last stored in memory that was reserved for that variable.

New cards
37

Sentinel-controlled repetition

AKA indefinite repetition, because we don't know how many times it will loop before it begins executing.

New cards
38

Sentinel value

AKA signal value, dummy value, flag value.

Indicates "end of data entry."

- Must be chosen so it can't be confused with input values (e.g. if user is entering grades such as 98, 96, etc., -1 would be an acceptable sentinel value).

New cards
39

Float

Data type that handles numbers with decimal points (AKA floating-point numbers). Introduces cast operator to handle averaging calculations.

e.g. average = (float) total / counter;

New cards
40

Cast operator

average = (float) total / counter;

- Cast operator (float) creates temporary floating point copy of its operand. Temporary - the value stored in variable total is still an integer.

- Associate from left to right, have same precedence as other unary operators (+ , -); have one level higher precedence than multiplicative operators (% , * , /).

New cards
41

Promotion

AKA implicit conversion.

Compiler does this to ensure that operands are of the same type; e.g. in an expression containing the data types int and float, copies of the int operands are made/promoted to the type float.

New cards
42

Assignment operators

Abbreviates assignment expressions, e.g. c = c + 3 can be abbreviated as c +=3.

The += operator adds the value of the expression on the right (3) to the value of the variable on the left (c) and stores the result in the variable on the left (c).

Any statement in the form 'variable = variable operator expression' can be written in the form 'variable operator= expression' using operators + - % * /

New cards
43

Arithmetic assignment operators

Operator, expression

+= , c = c + 7

-= , d = d - 4

= , e = e 5

/= , f = f * 3

%= , g = g % 9

New cards
44

Increment and decrement operators

Unary increment operator: ++ , can be used instead of c = c+1 or c += 1.

Unary decrement operator: -- , can be used instead of c = c-1 or c -= 1.

Only works for the number 1.

New cards
45

Pre increment , post increment operators

++c , --c

c++ , c--

Pre increment: bumps variable and then uses it.

Post increment: uses variable first, then bumps it (e.g. for next loop).

New cards
46

Loop-continuation condition

Must be true for a loop to continue.

New cards
47

Requirements of counter-controlled repetition

Name of a control variable (or loop counter), initial value of the control variable (usually 0 or 1), increment/decrement by which the control variable are modified each time through the loop, condition that tests for the final variable (i.e. whether or not the loop should continue).

New cards
48

'for' repetition statement

Handles all the details of counter-controlled repetition:

for( counter=1 ; counter <= 10 ; counter++)

Specifies each of the items needed for counter-controlled repetition with a control variable; contains initial value, condition and increment.

New cards
49

Off-by-one error

Logic error, e.g. writing counter<10 rather than counter<=10 , loop would only execute 9 times instead of 10.

New cards
50

General form of the for statement

for( expression1 ; expression2 ; expression3 )

statement

New cards
51

Comma operators

Commas used when a for statement uses expression1 and expression3 as a comma-separated list; guarantee that the lists evaluate from left to right.

New cards
52

Increments/decrements in for statements

counter = counter+1

counter += 1

++counter

counter++

are all equivalent in the increment part of a for statement; because the variable does not appear in a larger expression, all forms have the same effect.

New cards
53

pow( x,y )

Calculates the value of x raised to the y power; requires two double arguments. Included in the math.h library.

New cards
54

%21.2f

Conversion specifier %21.2f is used to print the value of the variable in the program (.2 denotes 2 digits after the decimal point); the value 21 denotes the field width in which the value will be printed.

New cards
55

'switch' multiple-selection statement

Consists of a series of case labels, an optional default case and statements to execute for each.

switch( grade )

case 'a' :

++aCount;

break;

only use ' ' for characters (case 'a' , case 1).

New cards
56

'getchar' function

From stdio.h; read one character from the keyboard and stores that character in an integer variable.

e.g. grade = getchar( )

Character is returned by getchar and assigned to the variable 'grade'.

New cards
57

%c

Prints/scans a character from the keyboard.

New cards
58

EOF

Used as sentinel value, normally has variable -1; symbolic integer constant defined in stdio.h

Choose to represent characters as ints because EOF has an integer value (usually -1).

New cards
59

'do...while' repetition statement

- Tests the loop-continuation condition after the loop body is performed; if you want to loop at least one time for sure, use a do...while repetition statement.

do

{

statement

}

while ( condition );

New cards
60

'break' statement

Causes an immediate exit from the statement when used in a while, do...while, for, or switch statement.

New cards
61

'continue' statement

Skips the remaining statements in the body of a control statement and performs the next iteration of the loop when used in a while, do...while, for, or switch statement.

- While and do...while: loop-continuation test is evaluated immediately after the continue statement is executed.

- For statement: increment expression is executed and then the loop-continuation is evaluated.

New cards
62

Logical operators

&& (logical 'and'), || (logical 'or'), ! (logical 'not' AKA logical negation). && has higher precedence than ||.

e.g. if( gender == 1 && age >=65 )

Both must be true

e.g. if( semesterAvg >= 90 || finalExam >= 90 )

One must be true

New cards
63

Binary operator

e.g. && and ||, combine two conditions

New cards
64

Unary operator

e.g. ! , only a single condition

New cards
65

"Divide and conquer"

Modularizing; constructing a program from smaller modules that are more manageable than the original program; e.g. using other functions outside of main.

New cards
66

Modules

Functions, e.g. printf, scanf, pow

New cards
67

Programmer-defined functions

Functions that the programmer creates (not included in stdio.h, math.h, etc.)

New cards
68

Function call

Specifies the function name and provides information (as arguments) that the called function needs to perform its designated task.

e.g. printf( "%d" , square(x) );

square(x) is called

New cards
69

Function parameters

Provide the means for communicating information between functions; local variables of a function.

New cards
70

Software reusability

Using existing functions as building blocks to create new programs; e.g. preexisting functions in stdio.h, math.h, etc.

New cards
71

Function prototype

-Tells the compiler how the function will be used; tells the type of data returned, the number of parameters the function expects to receive, the types of parameters, and order in which parameters are expected.

-Always use ; after a function prototype

-Technically optional, but recommended to prevent problems later on

e.g. int square( int y );

New cards
72

Local variables

Variables that belong only to the function in which they are used; they are created in destroyed within that function.

New cards
73

Function header

Function-name: any valid identifier

Return-value-type: data type of the result returned to the caller

Return-value-type, function-name, and parameter-list are referred to as the function header.

New cards
74

Function body

Definitions and statements within braces, AKA block.

New cards
75

'return: '

If a function does return a result,

return expression;

returns the value of expression to the caller.

If nothing is returned, it needs to say void (e.g. int main( void )).

New cards
76

Coercion of arguments

Forcing of arguments to the appropriate type; e.g. 'sqrt' can be called with an integer even though the prototype expects a double argument and it will still work correctly.

- Promotes to more precision (e.g. integer to float); will not make a float into an integer.

New cards
77

Mixed-type expressions

Expressions containing values of two or more data types.

- Type of each value in a mixed-type expression is automatically promoted to the "highest" type in the expression.

New cards
78

Promotion hierarchy

Data type, printf conversion, scanf conversion:

long double , %Lf , %Lf

double , %f , %2f

float , %f , %f

unsigned long int , %lu , %lu

long int , %ld , %ld

unsigned int , %u , %u

int , %d , %d

unsigned short , %hu , %hu

short , %hd , %hd

char , %c , %c

New cards
79

Stack

- Data structure (collection of related data items)

- Analogous to a pile of dishes

- Pushing: adding a dish

- Popping: removing a dish, always from the top

- AKA last-in first-out (LIFO) data structures

New cards
80

Program execution stack

AKA function call stack

- Return address of the calling function is located here; the called function uses this to know how to return to its caller

New cards
81

Activation record / stack frame

Data containing the local variables used in each invocation of a function during a program's execution.

New cards
82

Stack overflow

Occurs if more function calls occur than can have their activation records stored on the program execution stack

New cards
83

Call-by-value

A copy of the argument's value is made & passed to the called function; changes to the copy do not affect an original variable's value in the caller; technically all call-by-value in C.

New cards
84

Call-by-reference

Caller allows the called function to modify the original variable's value.

New cards
85

Side effects

Variable modifications; can occur when calling by reference.

New cards
86

Random number generation

Found in stdlib.h, 1 + rand( ) % 6

e.g. Simulating a six-sided die: 1 + rand ( ) % 6 produces integers from 0-5. 6 is called the scaling factor; the range of numbers is shifted by adding 1 to the previous result.

New cards

Explore top notes

note Note
studied byStudied by 12 people
... ago
5.0(2)
note Note
studied byStudied by 13 people
... ago
5.0(1)
note Note
studied byStudied by 17 people
... ago
5.0(1)
note Note
studied byStudied by 5 people
... ago
5.0(1)
note Note
studied byStudied by 25 people
... ago
4.0(1)
note Note
studied byStudied by 54 people
... ago
5.0(3)
note Note
studied byStudied by 206 people
... ago
5.0(3)
note Note
studied byStudied by 2 people
... ago
5.0(1)

Explore top flashcards

flashcards Flashcard (50)
studied byStudied by 5 people
... ago
5.0(1)
flashcards Flashcard (103)
studied byStudied by 46 people
... ago
5.0(1)
flashcards Flashcard (41)
studied byStudied by 1 person
... ago
5.0(1)
flashcards Flashcard (60)
studied byStudied by 2 people
... ago
5.0(1)
flashcards Flashcard (20)
studied byStudied by 67 people
... ago
5.0(1)
flashcards Flashcard (38)
studied byStudied by 12 people
... ago
5.0(1)
flashcards Flashcard (20)
studied byStudied by 9 people
... ago
4.0(1)
flashcards Flashcard (30)
studied byStudied by 5 people
... ago
5.0(1)
robot