DCIT104 Lecture 3
DCIT104 Lecture 3
PROGRAMMING
FUNDAMENTALS
Programmers benefit from having a mindset of thinking precisely and paying attention to
details. The following questions emphasize attention to detail. See if you can get all of
the questions correct on the first try.
The focus needed to answer the above correctly on the first try is the
kind of focus needed to write correct programs. Due to this fact, some
employers give "attention to detail" tests to people applying for
programming positions.
See for example this test, or this article discussing the issue. Or, just web
search for "programmer attention to details" for more such tests and
articles.
The following program calculates yearly and monthly salary given an hourly wage. The
program assumes a work-hours-per-week of 40 and work-weeks-per-year of 50.
• Type the code below into your IDE and change the incorrect code to print a monthly
salary. Then run the program.
• Copy the program code on the next slide into your IDE and
run the program to see three possible married-couple names.
#include <iostream>
#include <string>
using namespace std;
int main() {
string firstName1;
string lastName1;
string firstName2;
string lastName2;
cout << "What is the first person's first name?" << endl;
cin >> firstName1;
cout << "What is the first person's last name?" << endl;
cin >> lastName1;
cout << "What is the second person's first name?" << endl;
cin >> firstName2;
cout << "What is the second person's last name?" << endl;
cin >> lastName2;
cout << "Here are some common married-couple names:" << endl;
cout << firstName1 << " " << lastName1 << " and " << firstName2 << " " << lastName2 << endl;
cout << firstName1 << " and " << firstName2 << " " << lastName1 << endl;
cout << firstName1 << " and " << firstName2 << " " << lastName2 << endl;
// FIXME: Print two hyphenated last name options, with either last name
// appearing first. (A hyphen can be written as "-")
= is not equals
In programming, = is an assignment of a left-side variable with a right-side value. =
is NOT equality as in mathematics. Thus, x = 5 is read as "x is assigned with 5", and
not as "x equals 5". When one sees x = 5, one might think of a value being put into a
box. Google Confidential and Proprietary
zyBooks
Variables and Assignments
In the code below, litterSize is assigned with 3, and yearlyLitters is assigned with 5.
Later, annualMice is assigned with the value of litterSize * yearlyLitters (3 * 5, or 15),
which is then printed. Next, litterSize is assigned with 14, yearlyLitters is assigned with
10, and annualMice is assigned with their product (14 * 10, or 140), which is printed.
Using the code stub below, write a statement that assigns numCoins with
numNickels + numDimes. E.g.,: 5 nickels and 6 dimes results in 11 coins.
•Note: These activities may test code with different test values. This activity will perform
two tests: the first with nickels = 5 and dimes = 6, the second with nickels = 9 and dimes =
0.
#include <iostream>
using namespace std;
int main() {
int numCoins;
int numNickels;
int numDimes;
numNickels = 5;
numDimes = 6;
cout << "There are " << numCoins << " coins" << endl;
return 0;
Google Confidential and Proprietary
}
Initializing Variables
• Using the code stub below, write one statement that declares an
integer variable numHouses initialized to 25
#include <iostream>
using namespace std;
int main() {
return 0;
}
Google Confidential and Proprietary
Assignment Statement with Same Variable on Both Side
• Using the code stub below, write a statement that increases numPeople by 5.
– Ex: If numPeople is initially 10, the output is: There are 15 people.
#include <iostream>
using namespace std;
int main() {
int numPeople;
cout << "There are " << numPeople << " people." << endl;
return 0;
}
Google Confidential and Proprietary
zyBooks
Common Errors
• A common error is to read a variable that has not yet been assigned
a value.
• If a variable is declared but not initialized, the variable's memory
location contains some unknown value, commonly but not always
0.
• A program with an uninitialized variable may thus run correctly on
system that has 0 in the memory location, but then fail on a
different system—a very difficult bug to fix.
• A programmer must ensure that a program assigns a variable with a
value before reading.