HW (1.1-1.7)
How many lines will the compiler ignore in the code below? int userAge; int currentDecade; int nextDecade; int nextMilestone; // FIXME: Get user age userAge = 29; // Testing with 29 currentDecade = userAge / 10; nextDecade = currentDecade + 1; nextMilestone = nextDecade * 10;
3
Consider the instruction: z = x + y. If x is 10 and y is 20, then z is assigned with _____.
30
The program has a total number of _____ instructions.
4
The expression wage * 40 * 50 resulted in what value?
40000
How many spaces will the compiler ignore in the code below? numToBuy = numNeeded - numInStock + 2;
6
A compiler generates the following error messages:Line 7: Missing semicolonLine 9: numItems not definedLine 10: Expected '(' 1) The programmer should start by examining line _____. 2) If the programmer corrects an error on line 7, the programmer should _____. 3) If the programmer does NOT find an error on line 7, the programmer should check line _____.
7 compile 6
Suppose a new instruction was inserted as follows:...z = x + y Add 1 more to z (new instruction)Put z to output What would the last instruction then output to the screen?
8
comment
A comment is text a programmer adds to code, to be read by humans to better understand the code but ignored by the compiler.
Compiler warnings
A compiler will sometimes report a warning, which doesn't stop the compiler from creating an executable program but indicates a possible logic error.
Storage
A disk (aka hard drive) stores files and other data, such as program files, song/movie files, or office documents. Disks are non-volatile, meaning they maintain their contents even when powered off.
logic error,
A logic error, also called a bug, is an error that occurs while a program runs.
memory
A memory is a circuit that can store 0s and 1s in each of a series of thousands of addressed locations, like a series of addressed mailboxes that each can store an envelope (the 0s and 1s).
multi-line comment
A multi-line comment starts with /* and ends with */, where all text between /* and */ is part of the comment. A multi-line comment is also known as a block comment.
Newline character
A new output line can also be produced by inserting \n, known as a newline character, within a string literal.
Input
A program gets data, perhaps from a file, keyboard, touchscreen, network, etc.
Process
A program performs computations on that data, such as adding two values like x + y.
Output
A program puts that data somewhere, such as to a file, screen, network, etc.
program
A program starts in main(), executing the statements within main's braces { }, one at a time.
algorithm
A sequence of instructions that solves a problem is called an algorithm.
single-line comment
A single-line comment starts with // and includes all the following text on that line. Single-line comments commonly appear after a statement on the same line.
assembly
Because 0s and 1s are hard to comprehend, programmers soon created programs called assemblers to automatically translate human readable instructions, such as "Mul 97, #9, 98", known as assembly language instructions, into machine instructions.
compile-time error.
Because a syntax error is detected by the compiler, a syntax error is known as a type of compile-time error.
semicolon
Each statement typically appears alone on a line and ends with a semicolon, as English sentences end with a period.
Find the syntax errors. Assume variable numDogs has been declared. 1) cout << numDogs. 2) cout << "Dogs: " numDogs; 3) cout < "Everyone wins."; 4) cout << "Hello friends! << endl; 5) cout << "Amy // Michael" << endl; 6) cout << NumDogs << endl; 7) int numCats numCats = 3; cout << numCats << endl; 8) cout >> numDogs >> endl;
Error Error Error Error No error Error Error Error
Spaces are always ignored by the compiler.
False
When a compiler says that an error exists on line 5, that line must have an error. 2) If a compiler says that an error exists on line 90, the actual error may be on line 91, 92, etc. 3) If a compiler generates a specific message like "missing semicolon", then a semicolon must be missing somewhere, though maybe from an earlier line.
False
A compiler warning by default will prevent a program from being created. 2) Generally, a programmer should not ignore warnings. 3) A compiler's default settings cause most warnings to be reported during compilation.
False True False
Input/output devices
Input/output devices: A screen (or monitor) displays items to a user. A keyboard allows a user to provide input to the computer, typically accompanied by a mouse for graphical displays.
machine instructions
Instructions represented as 0s and 1s are known as machine instructions, and a sequence of machine instructions together form an executable program (sometimes just called an executable).
Which instruction completes the program to compute a triangle's area? base = Get next input height = Get next input Assign x with base * height____ _ Put x to output
Multiply x by 1/2
Syntax errors
One kind of mistake, known as a syntax error, is to violate a programming language's rules on how symbols can be combined to create a program.
Memory
RAM (random-access memory) temporarily holds data read from storage and is designed such that any address can be accessed much faster than disk, in just a few clock ticks (see below) rather than hundreds of ticks.
Each statement ends with what symbol?
Semicolon ;
string literal
Text in double quotes " " is known as a string literal.
cout
The cout construct supports output; cout is short for characters out.
cout
The cout statements output various values. The return 0 statement ends the program (the 0 tells the operating system the program ended without error).
cin
The following statement gets an input value and puts that value into variable x: cin >> x; cin is short for characters in.
int wage
The int wage statement creates an integer variable named wage. The wage = 20 statement assigns wage with 20.
operating system
The operating system allows a user to run other programs and interfaces with the many other peripherals.
Processor
The processor runs the computer's programs, reading and executing instructions from memory, performing operations, and reading/writing data from/to memory.
program, application
The programmer-created sequence of instructions is called a program, application, or just app.
endl
The statement cout << endl starts a new output line, called a newline.
processors
To support different calculations, circuits called processors were created to process (aka execute) a list of desired calculations, with each calculation called an instruction.
compilers
To support high-level languages, programmers created compilers, which are programs that automatically translate high-level language programs into executable programs.
1) A bit can only have the value of 0 or 1. 2) Switches have gotten larger over the years. 3) A memory stores bits. 5) A processor executes instructions like Add 200, #9, 201, represented as 0s and 1s.
True False True True
Indicate which are valid code. 1) // Get user input 2) /* Get user input */ 3) /* Determine width and height, calculate volume, and return volume squared. */ 4) // Print "Hello" to the screen // 5) // Print "Hello" Then print "Goodbye" And finally return. // 6) /* * Author: Michelangelo * Date: 2014 * Address: 111 Main St, Pacific Ocean */ 7) // numKids = 2; // Typical number 8) /* numKids = 2; // Typical number numCars = 5; */ 9) /* numKids = 2; /* Typical number */ numCars = 5; */
Valid Valid Valid Valid Invalid Valid Valid Valid Invalid
Whitespace
Whitespace refers to blank spaces (space and tab characters) between items within a statement and blank lines between statements (called newlines). A compiler ignores most whitespace.
Indicate the actual output of each statement. Assume userAge is 22. 1) cout << "You are " << userAge << " years."; 2) cout << userAge << "years is good.";
You are 22 years. 22years is good.
Which instruction completes the program to compute the average of three numbers? x = Get next input y = Get next input z = Get next input _____ Put a to output
a = (x + y + z) / 3
Which statement gets an input value into variable numCars?
cin >> numCars;
Type a statement that gets an input value into variable numUsers.
cin >> numUsers;
Type a statement that outputs: Hello
cout << "Hello";
Which statement outputs: Welcome!
cout << "Welcome!";
Type a statement that starts a new output line.
cout << endl;
Which statement starts a new output line?
cout << endl;
Given variable numCars = 9, which statement outputs 9?
cout << numCars;
Type a statement that outputs the value of numUsers (a variable). End statement with a semicolon. Do not output a new line.
cout << numUsers;
The statement int wage; creates a variable named wage that is used to _____ the value 20.
hold
Would the following order of statements work the same as above? wage = 20; int wage;
no
variables
refer to data
Each cout statement outputs items to _____.
the screen
Program execution begins at main() and executes statements surrounded by which symbols?
{}