CMPSC 201 Exam 1 Flashcards

1
Q

What is a computer?

A

A programmable electronic device that can
store, retrieve and process data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Advantages of computers

A

Reliable, Fast, Does not tire,
Follows instructions (programs) precisely

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Disadvantages of computers

A

Not “smart”– cannot analyze problems
Follows instructions (programs) precisely

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What can a computer do?

A

Arithmetic, Comparisons/decision making, Communication-input, output, transfer of data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

CPU

A

fetches and follows a set of simple instructions
(also called processor).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Main Memory

A

stores the executing program and its data in RAM
(random access memory). Volatile

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Volatile Memory

A

Volatile memory is a type of memory that requires a constant power supply to retain data. When the power is turned off, all data stored in volatile memory is lost. This type of memory is typically used for temporary storage and fast data access.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Secondary Memory

A

stores permanent records (files). Non-volatile

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Non-volatile Memory

A

Non-volatile memory (NVM) is a type of computer memory that retains stored information even after power is removed. This makes it ideal for long-term data storage and secondary storage applications.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Examples of Secondary Memory

A

hard disks, USB-drives,
CD-ROMs, DVDs, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Components of a Computer

A

Input Device, CPU, Main Memory, Secondary Memory, Output Device

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

ALU

A

Part of CPU stands for Arithmetic and Logic Unit performs mathematical operations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Control Unit

A

Control Unit coordinates all of the computer’s operations, performs the
fetch/decode/execute cycle
Fetch: Control Unit fetches next instruction
Decode: Control Unit decodes instruction
Execute: instruction is executed by appropriate
component

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

RAM Main Memory

A

consists of a long list of numbered memory
locations (RAM). Main memory is volatile!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Memory Locations

A

contain zeros and ones, on and off switches,
called binary digits or bits

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Byte

A

8 bits 1 address for the memory stored there

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Storage of Secondary Memory

A

Uses same idea of bits and bytes
Programs/data stored in units called files.
Stores files when they are not in use

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Operating Systems

A

Operating Systems – allocates computer’s
resources and allows communication between
user and computer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Programs/Program

A

a set of instructions to perform
specific tasks. / A set of instructions a computer
follows to perform a task. An algorithm that has
been translated into a programming language so
the computer can “understand” and perform the
steps.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Programming language

A

special language used
to write programs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Algorithm

A

set of well-defined steps for
performing a task or solving a problem

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

High Level Languages

A

designed to be easy for humans to read
and to write programs in, but too complicated for the computerand to write programs in, but too complicated for the computer
to understand

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Low Level Languages

A

consist of simple instructions which can
be understood by the computer after a minor translation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Compiled Languages

A

a separate file is created for the translated binary
code (C++, Java, FORTRAN, etc.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

Interpreted Languages

A

each command is interpreted and executed as
program is run (MATLAB, Python, etc.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Machine Language

A

written in the form of zeros and ones, can
be understood directly by the computer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

High Level to Machine Code Steps

A

Source Code - Preprocessor - Compiler - Linker - Executable Code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Source Code

A

Source file created by a
text editor *.cpp

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Preprocessor

A

Modifies source code by instructions (#)
A library of already written source code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

Compiler

A

Translates to Machine Code *.obj and
checks for syntax errors

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

Linker

A

Combines various object code files to create
an executable file *.exe

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Key Words

A

reserved words that have a special
meaning. (often shown in blue)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

Programmer Defined Symbols

A

words or names
that have been defined by the programmer. May
be variables, constants, or functions/routines.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

Operators

A

tell the computer to perform specific
operations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

Punctuation

A

begins or ends a statement, or
separates items of a list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

Syntax

A

grammar rules for combining elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

Syntax Errors

A

code does not follow the grammar
rules of language.
Found by the compiler.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

Logic Errors

A

results are not what was expected
due to incorrect implementation. (Note different
from text!)
Use test data for which you know what the results
should be.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

Run-time Errors

A

program causes the computer to
perform an illegal operation. (Note different from
text!) Divide by 0 or square root of negative number.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

Input

A

data the user of the program will need to
enter into the program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

Output

A

information the user expects to receive
from the program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

Processing

A

what the program does to the input to
produce the output.
Examples:
Calculate weekly pay
Calculate the area of a rectangle

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

include <iostream></iostream>

A

Preprocessor Directive; comes before everything else in the program except the explanatory comment at the top

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

using namespace std;

A

Follows #include<iostream></iostream>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

Algorithm vs Program

A

All Algorithms are programs, but not all programs are algorithms

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

Fetch

A

CPU gets, from main memory, the next instruction in the sequence of a program’s instructions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

Decode

A

The instruction is encoded in the form of a number, The control unit decodes the instruction and generates an electric signal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q

Execute

A

After decoding the signal is sent to the correct part of the computer and causes that component to perform an operation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q

System Software

A

Controls and manages the basic operations of a computer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q

Utility Programs

A

enhance the computer’s operations and safeguards data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

Application Software

A

makes the computer useful for everyday tasks. Games, browsers, word, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
Q

Constant Declarations

A

happen before the main function but after “using namespace std;” for global usage
Format: const data type identifier = value;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

int main()

A

The main function that holds all of the code be sure to use BRACES

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q

variable declarations

A

happen first within the int main function and set variables for your program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
55
Q

return 0;

A

Marks the end of the program and lets the computer know to stop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
56
Q

Test Data

A

input for which the programmer
knows the result

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
57
Q

4 Types of Control Structures

A

Branch, Loop, Sequence, Functions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
58
Q

Branch Control Structure (decision)

A

some statements may be
executed only when a specific condition is met.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
59
Q

Loop Control Structure (repetition)

A

statements are repeated

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
60
Q

Functions Control Structure

A

a group of statements are executed at
various times in the program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
61
Q

Sequence Control Structure

A

statements are executed in order they
are written

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
62
Q

Comments

A

describe what the program is
supposed to do and what various portions do.
Often used as documentation. Not executed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
63
Q

Preprocessor Directives

A

directions to perform
before program starts.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
64
Q

Functions (Not the control structure)

A

group of statements to perform a
specific task.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
65
Q

Statements

A

actions executed by the computer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
66
Q

// double slash

A

beginning of a single line comment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
67
Q

/* */ slash asterisk

A

Go around a multiline comment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
68
Q

octothorp (hashtag)

A

beginning of a
preprocessor directive

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
69
Q

< > brackets

A

enclose filenames or data types

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
70
Q

(_) parentheses

A

contains parameters for
functions or changes order of operations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
71
Q

{_} braces

A

enclose a group of statements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
72
Q

; semicolon

A

ends a C++ statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
73
Q

include Directive

A

Preprocessor directive
Causes contents of another file to be inserted into
the program.
Often the files are library files that contain
specific definitions.
Not C++ statements

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
74
Q

Case Sensitive Languages

A

C++ is one so Include is not the same as INCLUDE or include.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
75
Q

Main function

A

All C++ programs must have a function main
int main ()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
76
Q

C++ Output Statement

A

cout “c” + “out”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
77
Q

What is needed for cout?

A

cout
object, the insertion operator(s) («), and
variables or constants to be displayed.
cout «“Hello”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
78
Q

What is needed for multiple items to be output using the same cout statement?

A

&laquo_space;insertion operator
Ex: cout«“The volume is ” «volume;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
79
Q

Escape Sequences

A

Performs specific output tasks when enclosed in
quotes. (cout is still needed)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
80
Q

\n

A

Causes cursor to go to next line

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
81
Q

\t

A

Causes cursor to go to next tab stop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
82
Q

\a

A

Causes computer to beep

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
83
Q

\\ (Escape Sequence)

A

Causes backslash to be printed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
84
Q

\’

A

Causes single quote to be printed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
85
Q

\”

A

Cause double quote to be printed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
86
Q

endl manipulator

A

Inserts blank lines in output just like the escape sequence.
cout &laquo_space;radius &laquo_space;endl &laquo_space;volume;
radius

volume

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
87
Q

Blanks in cout

A

Blanks may be inserted by enclosing the spaces in
quotes.
cout &laquo_space;radius &laquo_space;“ ” &laquo_space;volume;
radius volume

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
88
Q

Identifiers

A

names (or symbols) used by the
programmer to refer to items such as variables,
named constants, functions, classes, etc.
should be descriptive of what they
stand for.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
89
Q

Identifier Rules

A

Cannot be a reserved word (keyword)
only can contains letters, numbers, or the underscore.
first character must be a letter or underscore
(better to be a letter).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
90
Q

Variables and symbolic constants

A

names for memory locations in RAM.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
91
Q

Variables

A

refer to memory locations in which the
value stored may change throughout the
execution of the program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
92
Q

Named or symbolic constants

A

refer to memory
locations in which the values do not change.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
93
Q

definition/declaration

A

statements that tell
the computer to allocate memory space and what
identifier will be used to refer to that space.
you must define or declare a variable or constant before you may use it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
94
Q

Variable Declarations Format

A

data type variable name;
where the variable name must follow the identifier
rules.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
95
Q

Data Types definition

A

determines how the data will be stored and
what types of operations can be performed on this data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
96
Q

Data Type Examples

A

Integers (whole numbers),
Real Numbers (may have fractional parts),
Characters (letters or numbers that you do not want for arithmetic operations),
and Boolean (true or false)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
97
Q

Integer variables

A

memory spaces that will store
whole numbers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
98
Q

Integer Data Types and size

A

short 2 bytes
unsigned short 2 bytes
int 4 bytes
unsigned int 4 bytes
long int 4 bytes
unsigned long 4 bytes
long long int 8 bytes

99
Q

Floating Point Data

A

Used to store real numbers, numbers with
fractional parts

100
Q

Floating Point Data Types

A

float 4 bytes single precision numbers
double 8 bytes double precision numbers
long double 10 bytes

101
Q

Precision

A

refers to the number of digits that are
stored before the values are rounded.

102
Q

Boolean Data

A

data that is either true or false
False is represented as 0 in the computer.
True is represented as 1.
Anything other than 0 (zero) is
considered to be true.
Written: bool

103
Q

Character Data

A

Written: char
holds a single character value
two methods to store multiple characters: C-strings or string objects.

104
Q

C-Strings

A

arrays (collections) of characters.
The number in brackets should include space for
the null character.
Ex: char variablename[n];
Can be assigned a value at time of declaration however the assignment operator does not work later.

105
Q

String Objects

A

include <string></string>

variables that have be declared of the
string class.
To use the string class, you must employ the preprocessor directive.
string variable name;
You do not need to know how many characters need to be stored.
Can use assignment operator
name = “Tom”;

106
Q

Default Data Type for whole numbers

107
Q

Default Data Type for floating point numbers

108
Q

Global Scope

A

Occur before main function and will be the same value to all functions (for constant declarations)

109
Q

Local Scope

A

Declaration only applies to that variable or constant within the function it is defined
You should usually define, declare,
the variables at the beginning of the function (for
now main) in which they occur.

110
Q

Multi Variable Declaration Format

A

int alpha, beta, gamma, delta;
double weight, feet;

111
Q

Assignment Statements

A

store values in the memory locations.

112
Q

Assignment Operator

A

= (Not the same as equals)

113
Q

Assignment Rules

A

Named constants must be assigned at the time they
are declared.
Assignments to variables can occur at the same
time as declaration or after.
EX: int numberincase = 12;
The left and right side cannot be interchanged!
X = 5; (valid) is not the same as 5 = X; (Not Valid)
The left-hand side must be a single variable.

114
Q

What happens when a floating-point number is stored in an int?

A

It gets truncated 4.57 => 4

115
Q

Assigning char Values

A

Reminder a variable declared as char can only
store 1 character (letter, digit, special character)!
To store more than character you must use a string (character array).
‘w’ is a single character whereas “w” is two characters (w and the null character).
char var1 = ‘w’;
char var1 = “w” // invalid
var1 = ‘r’;
var1 = “r”; //invalid
char str1[2]=“q” ;
char str1=“t”; //invalid

116
Q

Strings

A

a collection of characters.
two methods of creating strings in
C++. C-strings, String objects (requires the string class)

117
Q

Arithmetic Operators

A

manipulate numeric values and perform
arithmetic operations.
Classified as unary, binary, or ternary based on
the number of operands needed.

118
Q

Unary

A

one operand -a

119
Q

binary

A

requires two operands.
X + Y c / b

120
Q

ternary

A

three operands

121
Q

Operand

A

+, -, *, /, % things like that

122
Q

Binary Arithmetic Operators

A

+ Addition sum = y + x
- Subtraction diff = x - y
* Multiplication numb = c * b
/ Division rate = part / total
% Modulus remainder = d % 4

123
Q

Data Type and Operator Changes

A

Integers operated on by integers result in an
integer
Floating points operated on by integers or floats
result in floating points

124
Q

Precedence

A

Defines the order in which operations are
performed.

125
Q

Arithmetic Operator Precedence

A

Operations in parentheses are performed first
Unary negation
* / % multiplication, division, modulus
+ - addition and subtraction
Operations of equal precedence are performed in order they are written from left to right.

126
Q

Number Representation of int vs. floating point number

A

int ==> 4
float ==> 4.0

127
Q

Multiple Assignments

A

A single assignment statement may be used to store the same value in multiple variables at one time.
x = y = h = d = k = 4.0;
Could be used to replace the statements
x = 4.0;
y = 4.0;
d = 4.0;
h = 4.0;
k = 4.0;

128
Q

C++ Input Object

A

cin object and extraction operator to read data from keyboard and store in memory locations.
cin» variablename;

129
Q

When does input stop?

A

when white space (space, tab, newline) is encountered.

130
Q

Multiple Input Values

A

separated by extraction operators.
cin»x»y; //data is stored in order given

131
Q

Cin and Data Types

A

cin does not confirm data type.
A char variable only stores one character (letter, digit, or special character).
Using cin will allow the extraction of 1 character
from the input stream.
If more than one character are entered, the first is stored in the variable and the rest remain in the input stream.
White spaces are skipped.

132
Q

Cin with C-strings

A

Remember a C-string is an array of characters and may be declared as
char varname[n];
char last[12];
You can use cin to store characters in this C- string.
cin»last;
Input will stop is a white space is encountered. There is no check for number of characters.

133
Q

Data Overflow/Underflow

A

C++ does not check for data being within range of data type.
How the data is stored may be compiler dependent.
In many compilers, the number will “wrap”.
For example, given the following statements
short x = 32767;
x = x + 5;
cout&laquo_space;x«endl;
will usually output -32764.

134
Q

Data Type Coercion

A

Automatic conversion of a data type for
mathematical operations or assignment.
int alpha = 10.5; // 10.5 is coerced to 10
double beta = 4; // 4 is coerced to 4.0
alpha = alpha/beta; //two coercions occur
//10 is coerced to 10.0 for the division
//the value 2.5 is truncated to 2 for assignment.
beta = alpha * 4.4;

135
Q

Type Casting

A

Code specifies the conversion of the data type for the purposes of an operation.
Type casting a variable will not change what is stored in a variable

136
Q

Three Methods of Type Casting

A

static_cast<data>(variable or expression)
data type (variable or expression)
(data type) variable or expression
Last two methods considered out of date</data>

137
Q

What are 2 ways to change what is stored in a variable?

A

Assignment statement
Input new value

138
Q

Define

A

Utilizes a preprocessor directive to associate a
value with a word.
The data type is not needed because no memory
space is allocated.
No memory space is used. The preprocessor will
replace the word with the value. (Similar to find and replace in word processing)
Because no memory is used, the data type of the value is not needed.

139
Q

Define Format

A

define identifier value

EX: #define tax_rate 4.5

140
Q

Compound Operator

A

Provide a shortcut for assignment statements. Combines the = operator with another arithmetic operator

141
Q

a+ = 5.67;

A

is equivalent to a = a + 5.67;

a %= 3; is equivalent to a = a % 3;
Reminder—the % operator only works for integer
data types.

142
Q

a -= 6.0;

A

is equivalent to a = a – 6.0;

143
Q

a *= 3.4;

A

is equivalent to a = a * 3.4;

144
Q

a /= 2.1;

A

is equivalent to a = a /2.1;

145
Q

a %= 3;

A

is equivalent to a = a % 3;
Reminder—the % operator only works for integer
data types.

146
Q

Built-In Functions

A

a library file, like cmath, with a function defined that has the format of
Ex: pow(base, exponent)
pow(x,y) is equivalent to x^y…
Use the include preprocessor directive to tell the program
to incorporate the library file #include <cmath></cmath>

147
Q

Trig Functions Assumption

A

that the angle is in radians not in degrees

148
Q

Random Numbers

A

The rand( ) function is used to generated random
numbers, how the numbers are generated in the
same order each time the program is run.
cout«rand( )«endl;

149
Q

Seed

A

Used with random values to skip the first “seed”
values. Can use another built-in function time( )
to generate a value for the seed.
unsigned seed = time(0); //generate a value
srand(seed); // skip seed values
cout«rand( )«endl; // output next number

150
Q

What library file does the rand() function need?

A

cstdlib library file

151
Q

How to limit the range of rand() function?

A

The random number can be limited to a range
using arithmetic principles.
y = rand( ) % value + 1;
// numbers from 1 to value will be generated
For example, to generate numbers from 1 – 15
y = rand( ) % 15 + 1;

152
Q

Random Numbers Other than rand() function

A

include<random>
Random number engine
random_device // one engine type
random_device Myrandgen; //defines an engine</random>

153
Q

Distribution Object

A

uniform_int_distribution<int> randInts(0,100);
Object Type states how numbers will be distributed,</int>

<Data> Object name (Number range (min value, max value))
</Data>

154
Q

Other random number use

A

Use the object to store a generated number
int number
number = randInts(Myrandgen);

155
Q

setw() Manipulator

A

controls the number of spaces that will be
used to output an item
cout«manipulator«item;
cout«setw(10)«x; // allows 10 spaces
The n must be an integer and is often called the
field width specification.
By default numbers are right justified within this field.
Must be before each item to be output.

156
Q

setprecision() Manipulator

A

used to control the number of significant digits or, if used with the fixed manipulator, the number of decimal places that will be displayed for a floating-point number.
cout«setprecision(3)«y;
cout«fixed«setprecision(3)«y;
Both fixed and setprecision() remain in effect until a new manipulator is used. Has no effect on integer values!

157
Q

showpoint Manipulator

A

set to show the decimal point and zeroes for numbers like 45.0

158
Q

Other Manipulators

A

left - sends output left justified
right - sends output right justified
Can use more than one manipulator at a time, just
separate with the &laquo_space;operator.
The programmer can also change manipulators within the same cout statement.
Ex: cout«left«somevariable«right«var2;

159
Q

Formatting Input/Output alternative

A

cout.width(5) instead of cout«setw(5)
cout.precision(3)
cout.setf(iosflag(s))
cout.unsetf(iosflags(s))
cout is actually an object belonging to a class
and as such has member functions that can be
accessed using the format object.function.
Usually formatting of input is reserved for use
with strings (character arrays).
By default, the cin object will also stop input if a
space, tab, or return is encountered.

160
Q

How to specify how many characters you want to store in a string?

A

can use
cin.width(n) or cin»setw(n)
However, input still stops when a space or tab is encountered.

161
Q

cin.get()

A

To store “white spaces”, spaces, tabs, and/or returns, in char variables use cin.get( ). It has the format cin.get(varname)
Often can be used to stop the program until the user enters a return
If storing in a character array (C-string) the
format may be expanded to
cin.get(varname, n, ‘char’)

162
Q

getline

A

To store multiple characters, including spaces, in string objects.
getline(cin, strname);
Input stops when a newline character, ‘\n’ is encountered. Will store leading spaces as well.

163
Q

cin.ignore()

A

Used to skip over characters that you do not want
to be stored anywhere.
Has the format cin.ignore(n, ‘char’) where n is the number of characters to be skipped and char specifies where skipping should stop.
Skipping stops whenever the one of criteria is met.
If char stops input, the char remains in the input stream so it may input.

164
Q

Hand Tracing

A

Given test data the programmer should be able to predict what is stored in each variable at each line of code.

165
Q

Relational (Boolean) Expressions

A

expressions that evaluate to true or false.
They may be comprised of a bool variable or constant; or a combination of variables and constants using relational and/or logical operators.
Relational expressions are used for both selection and repetition control structures.

166
Q

Relational Operators

A

Used to compare, or relate, two values
(variables or constants)
May be used with string objects, but not with C-strings (character arrays)

167
Q

equal to relational operator

168
Q

not equal to relational operator

169
Q

greater than relational operator

170
Q

less than relational operator

171
Q

greater than or equal to relational operator

172
Q

less than or equal to relational operator

173
Q

Which has higher precedence Relational or Arithmetic Operators?

A

Arithmetic Operators

174
Q

Decision with One Branch

A

Statement(s) are executed when a condition is true.
“Simple If ” or “single branched if”
if (condition)
{
statement(s)
}

175
Q

What are statements inside of braces called?

176
Q

Simple If statement formatting

A

If more than one statement is to be executed, braces must be used. Otherwise only the first statement is part of the if. Statements that belong to the if should be indented.

177
Q

Decision with Two Branches

A

Sometimes we would like to take one action if a condition is true and another if the condition is false.
Use If/Else structure
if (condition)
{
statement(s) //to be executed if true
}
else
{
statement(s)//to be executed if false
}

178
Q

What is the opposite of > and <?

A

<= and >= respectively

179
Q

Can relational expressions be combined like 5<x<10

A

No, in C++ relational operators only evaluate 2 operands

180
Q

Logical Operators

A

Combine or negate relational expressions
Rewrite the expression
5 < x < 10
using the logical operator and (&&) as
5 < x && x < 10
(note that both relational operators must have 2 operands)
Logical operators have lower precedence than
relational operators.

181
Q

AND operator

182
Q

OR operator

183
Q

NOT operator

184
Q

Truth Tables

A

List possible combinations plus the resultant true/false values from logical operators

185
Q

Local Operator Precedence

A

&& higher than ||
Can always be overridden by parentheses

186
Q

Short Circuit Evaluation

A

When the result of a combination of relational expressions can be determined from the evaluation of the sub-expression to the left of the logical operator, the sub-expression to the right is not evaluated.
(x < y) || (x > z) x > z will not be evaluated if x<y is true
(y > 5) && (c < j) c < j will not be evaluated if y>5 is false

187
Q

Multi-branched Structures

A

One method to allow for multiple branches is the if/else if structure.
if (condition 1)
{
statement(s)
}
else if (condition 2)
{
statement(s)
}
* MORE AS NEEDED** MORE AS NEEDED*
else // default or trailing else, no if!
{
}

188
Q

Evaluating Multi-way Branches

A

Go down the ifs one by one
As soon as the statements associated with a true condition are executed, the branch structure is exited.
When none of the ifs evaluated to true, the statements associated with the trailing else (when there is one) are executed.

189
Q

Nesting Ifs

A

if (condition 1)
{
statement(s)
if (condition 2)
{
statement(s)
}
else
{
statement(s)
}
}
else
{
statement(s)
}
if inside an if statement
Proper indentation is very important for the person reviewing the program.

190
Q

Comparing Floats

A

Because of how floating-point numbers are stored in memory, unpredictable results may occur when trying to determine equality.
Stick to using greater than or less than operators and use logical operator to determine a range.
Example suppose you thought the round off error to cause a 0.0000003 difference in the value
(x == 4.5673675)
(x > 4.5673671) && (x < 4.5672679)

191
Q

Comparing C-Strings

A

C-strings cannot be compared using relational operators, but instead the strcmp( ) function (requires cstring library file).
Has the general format
strcmp(str1, str2)
Compares each character by subtracting the ASCII value of str2 character from str1 character from the beginning of the strings until a difference is found.
Returns a zero if strings are the same, a negative number is str1 is less than str2, or a positive number if str1 >str2.

192
Q

Scope

A

refers to where an identifier (often a variable name) association is known.

193
Q

Global Scope Variables are

A

NOT PERMITTED in this course

194
Q

Variables with the Same Name

A

If blocks are nested, the inner block can define (declare) a new variable with the same name as a variable in the outer block.
Each variable will have its own memory space.
(Don’t do this though it makes it hard to debug)

195
Q

Switches

A

The switch has a controlling expression that is evaluated and matched to a series of cases.
The controlling expression if evaluated to an integer or single character.
Switch is an alternative method to treat multi-branched decisions. A variable or expression will determine which branch will be executed.
switch (integer expression)
{
case const1: statement(s)
case const2: statements(s)
case const3: statement(s)
case constn: statement(s)
default : statement(s)
}

196
Q

General Switch Format

A

switch (expression)
{
case answer1:
statement(s)
break;
case answer2:
statement(s)
break;
case answer3:
statement(s)
break;
default:
statement(s)
}
The answers will be constants

197
Q

Break in switch

A

ends the execution of statements for each case.
If the break statement is omitted for a case, execution will continue using the statements for the next case until a break is encountered.

198
Q

Default in switch

A

Acts as a trailing else
The default case does not have to be included. If the default is not included and no cases match the expression, no statements will be executed.

199
Q

Advantages of switches

A

The “look” of the switch may be easier to
understand because it looks more like a menu.

200
Q

Disadvantages of switches

A

Cannot easily do irregular ranges.
Cannot match strings.

201
Q

Incrementing

A

adding one to a variable
A = A + 1
This statement can be replaced by the
incrementing operator ++.
A++; (post incrementing)
or ++A; (pre incrementing)

202
Q

Decrementing

A

Subtracting one from a variable
A = A -1
This statement can be replaced by the
decrementing operator –.
A–; (post decrementing)
or –A; (pre decrementing)

203
Q

Pre vs Post Incrementing/Decrementing

A

Determines when the incrementing (or
decrementing occurs), before or after the value is used.
Effects the results when the variable is combined with other values.

204
Q

Loops

A

Loops allow a group of statements to be executed over and over again.
All loops must have:
loop-control variable(s)loop-control variable(s)
body - block of statements to be executed repeatedly
a way for the loop to be terminated.
Three basic loop mechanisms: while, for, and do-while.
Loops may be classified as pre-test or post-test.

205
Q

While Loop

A

while (relational expression)
{
statement(s) // body of loop
}
The statements comprising the body of the loop will be executed until the relational expression evaluates to false.
Therefore one of the statements should modify the loop-control variable(s) so the loop terminates.

206
Q

Common Loop Errors

A

Not reaching the termination condition - loop never ends. It is an infinite loop.
Missing braces - only first statement is executed as body of the loop.
A semicolon at the end of while line
while (condition);
No statement is executed - it is an empty loop and an infinite loop if the condition is true.

207
Q

Do-While Loops

A

do
{
statement(s) // body of loop
}
while (condition); //test
The do-while loop is a post-test loop.
The condition is tested after the loop is executed
once.
May be sentinel or count-controlled.

208
Q

Sentinel controlled loops

A

indefinite/ the number of repetitions of the loop are not known

209
Q

Count-Controlled

A

number of repetitions of the loop is already known when starting the loop

210
Q

For-Loops

A

for(initialization; test; update) //header
{
statement(s) // body
}
Pretest loops
Count-controlled (usually)

211
Q

For loop expression variation

A

One, two, or all of the expressions may be omitted from the for loop.
for( ; x < 10 ; x = x+2)
the initial value of x is taken from earlier part of code.
for( ; x < 10 ; )
the loop control variable must be updated in the body of the loop to avoid an infinite loop.
for( ; ; ) // do NOT use in this course!
loop is terminated in some other manner.

212
Q

Other For-Loop quality in the header

A

The program may initialize or update a number of variables in the for header.
for(initialization(s); test; update(s))
{
statement(s)
}
for(x = 0, sum=0; x <= 20 && sum <150; x++, sum += x)
{
statement(s)
}
Be careful! Update occurs after body of loop has been executed.

213
Q

When do you use these loops?

A

Should the loop always execute at least once?
Yes => do-while
No => while or for
Should the loop be count controlled or
sentinel controlled.
Count => any of the loops
Sentinel => while or do-while (usually).

214
Q

How do you find a max using loops?

A

a repeated comparison of the number to the current maximum value. If the number is greater than the maximum value, then the maximum value is reset to that number.
There are two common ways to initialize the value of maximum: set it equal to the first value, or set it equal to an extremely low value.

215
Q

File Input/Output used when?

A

You want to input/output data faster than by hand and/or don’t want to just output the data to the stream.

216
Q

ifstream

A

abstract data type for file input

217
Q

ofstream

A

abstract data type for file output

218
Q

fstream

A

abstract data type for both input or output (Not allowed in the course!)

219
Q

library file for using file input or output

A

include<fstream></fstream>

220
Q

How to declare an object that refers to the file?

A

datatype object name;
ifstream infile; // infile is just an identifier
ofstream outfile; // outfile is just an identifier
Can declare multiple objects:
ifstream infile, datain, filein;
ofstream outfile, dataout, fileout;

221
Q

How to open a file?

A

infile.open(“filename”);
outfile.open(“filename”);
may specify a directory path by placing it in quotes as well
infile.open(“C:\somename.ext”);
Template: ObjectType.open(“filename”)

222
Q

How to close a file?

A

infile.close( );
outfile.close ( ) ;

223
Q

How to hard code file names?

A

Put actual name in quotes.
infile.open(“filename”); // in same directory
infile.open(“cse103/myfile.txt”); /* other directory /
The filename may also be stored in C-string (char array) or string object.
infile.open(stringname); /
stringname is character array or string object that contains the file name information */

224
Q

How to access or write into the file?

A

like cin
infile&raquo_space;alpha;
and cout
outfile &laquo_space;beta;
All the formatting for cout and cin will work with your file objects as well.

225
Q

Steps using a file?

A

Declare File Object
Associate File with Object
Use File Object

226
Q

Test File Error with…

A

if(!file)

(lol your lab)

227
Q

Fail Function?

A

The fail function returns a 0 or false if the file has
been opened correctly and a 1 or true if there was
a problem.
if (indata.fail( ) )
{
cout«“There is an error\n”;
}

228
Q

Use loops for input files when?

A

There is more data than you want to process by hand in a repetitive manner.

229
Q

How to determine end of a file?

A

State of the Extraction Operator
and
eof function of the input object

230
Q

How State of Extraction Operator works?

A

The extraction operator (») returns a true or
false based on whether the value was read
correctly. When the end of find is reached the
extraction operator will return a false.

Therefore the successful input can be used as a
status to complete the loop.
while(infile&raquo_space;number)

231
Q

How End Of File works?

A

infile.eof( )
which evaluates to false as long as the end of the file was not detected and to true when it was detected.
while (infile.eof( ) == 0)
{
statement(s)
more input from file
}

232
Q

Why use functions in a program?

A

helps organize code, make it
easier to port from program to program – modular
programming; and can call the same function from many places in the program.

233
Q

What is needed to employ functions?

A

Function Definition - Function Call - Function Declaration (Prototype)

234
Q

Function Declaration?

A

code for the specific task. (In this course function definitions should occur after main)

235
Q

Function Call

A

statement that directs control of the
program to that function

236
Q

Function Declaration (Prototype)?

A

identifies the function name and requirements. Usually occurs before main so the name is globally defined. Required for this course!

237
Q

How to write Function Definitions?

A

return type function name (data type parameter(s))
{
statement(s) //function body
return 0; (Not needed when return type == void)
}

238
Q

Function Declaration placement rules

A

Cannot be nested inside on another.

Therefore, because main is a function itself, they
may be placed before main or after main (requires
a function prototype).

For this course you are required to put the
functions after main and to use function prototypes
(declarations).

239
Q

Function Prototype Declarations rules?

A

Occur before main.
Look similar to function heading except that it
ends in a semicolon and it does not require the
parameter name(s), just the data type(s).
void print2lines ( );
int sum2int (int , int );
If the parameter name is included, it is ignored.
Function prototypes are required for this course.

240
Q

Code Format with Functions

A

//comments
# include library files
function prototype(s)
int main ( )
{
statement(s)
return 0;
}
function definition(s)
{
statement(s) // body
}

241
Q

Function Call?

A

is the statement that tells the function to execute.

When execution of the function has been
completed, flow of the program returns to
immediately after the call.
It has the format of

function name (parameter(s))

The parameters (arguments) from the call are
transferred to the function heading in the order in
which they appear.

242
Q

Function Call Rules?

A

A function can call another function or even itself.
y = sum2ints(5,sum2ints(pow(x,3), j));

A single C++ statement may call more than one
function.
xyz = cos(theta) + sum2ints(a,b);

Function calls can be part of a condition.
if (sum2ints(alpha, beta) > 10)if (sum2ints(alpha, beta) > 10)

if (boolfunction) //function with return type bool

243
Q

Parameters (Arguments) in a Functions?

A

The parameters in the function heading are referred to as “formal parameters”.
The parameters in the function call are referred to as “actual parameters” or arguments.
When the function call is executed, the values in the actual parameters are transferred to the formal parameters.

244
Q

Things to think about when making Functions.

A

What is this function supposed to do?
Is the function going to send information back to
where it was called?
If so, what is the data type of this information
What parameters does this function need to operate and what are the data types of these parameters?
What variables need to be declared within the function itself?