Variables and Assignments
Variables and Assignments
y = x assigns y with x's value, which presently is 5. z = x + 2 assigns z with x's present value plus 2, so 5
+ 2 or 7.
A subsequent x = 3 statement assigns x with 3. x's former value of 5 is overwritten and thus lost. Note
that the values held in y and z are unaffected, remaining as 5 and 7.
X+1=3 (invalid)
The left side must be a variable, not an expression like x + 1. In programming, = does not mean equal. =
means assign the left-side variable with the right-side's value. Thus, the left side MUST be a variable.
x=9
y=3
z=0
x=4
y=6
X is 4; y is 6 and z is 0
#include <iostream>
using namespace std;
int main() {
int userAge;
return 0;
Declare an integer variable named numPeople. (Do not initialize the variable.)
int numPeople;
What memory location (address) will a compiler allocate for the variable declaration below. If
appropriate, type: Unknown
The current value in houseRats is 200. What is in houseRats after executing the statement below? Valid
answers: 0, 199, 200, or unknown. (200)
numBooks = 5;
numBooks = 3;
answer = 3
int main() {
int x;
int y;
x = 9;
y = 6;
return 0;
Ans = 9 6
int main() {
int x;
int y;
x = 6;
y = 3;
x = 7;
return 0;
Ans = 7 3
int main() {
int x;
int y;
y = 4;
x = y;
return 0;
Ans 4 4
int main() {
int x;
int y;
y = 6;
x = y + 4;
return 0;
Ans = 10 6
int main() {
int a;
int x;
int y;
a = 5;
x = a + 5;
y = 13;
return 0;
Ans = 10 13
#include <iostream>
using namespace std;
int main() {
int numCoins;
int numNickels;
int numDimes;
numNickels = 5;
numDimes = 6;
numCoins=numNickels+numDimes;
cout << "There are " << numCoins << " coins" << endl;
return 0;
Declare an integer variable named numDogs, initializing the variable to 0 in the declaration. Int numDogs
=0;
#include <iostream>
using namespace std;
int main() {
return 0;
}
Write a statement ending with - 1 that decreases variable flyCount's value by flyCount = flyCount -1;
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;
numPeople = 10;
cout << "There are " << numPeople << " people." << endl;
return 0;
A name created by a programmer for an item like a variable or function is called an identifier. An
identifier must:
While various (crazy-looking) identifiers may be valid, programmers may follow identifier naming
conventions (style) defined by their company, team, teacher, etc. Two common conventions for naming
variables are:
Camel case: Lower camel case abuts multiple words, capitalizing each word except the first, as in
numApples or peopleOnBus.
Underscore separated: Words are lowercase and separated by an underscore, as in num_apples or
people_on_bus.
Neither convention is better. The key is to be consistent so code is easier to read and maintain.
Good practice is to create meaningful identifier names that self-describe an item's purpose. Good
practice minimizes use of abbreviations in identifiers except for well-known ones like num in
numPassengers. Programmers must strive to find a balance. Abbreviations make programs harder to read
and can lead to confusion. Long variable names, such as averageAgeOfUclaGraduateStudent may be
meaningful, but can make subsequent statements too long and thus hard to read.
Choose the "best" identifier for a variable with the stated purpose, given the above discussion.
Correct
Lcd is abbreviated but may be OK.
3)
The number of jelly beans in a jar.
numberOfJellyBeansInTheJar
jellyBeansInJar
nmJlyBnsInJr
1)
Is the following an error? Suppose an int's maximum value is 2,147,483,647.
numYears = 1,999,999,999; (yes)
int main() {
int userAgeYears;
int userAgeDays;
int userAgeMinutes;
int totalHeartbeats;
int avgBeatsPerMinute = 72;
cout << "You are " << userAgeDays << " days old." << endl;
return 0;
}
Enter your age in years: 19
You are 6939 days old.
You are 9992160 minutes old.
Your heart has beat 719435520 times.
#include <iostream>
using namespace std;
int main() {
int x;
int y;
x = 1;
y = 3 * (x + 9);
return 0; 1 30
#include <iostream>
using namespace std;
int main() {
int x;
int y;
x = 5;
y = x / 2;
#include <iostream>
using namespace std;
int main() {
int numDrinks;
int numTacos;
int totalCost;
cin >> numDrinks;
cin >> numTacos;
totalCost = (2 * numDrinks) + (4 * numTacos);
cout << "Total cost: " << totalCost << endl;
return 0;
}
Kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
k
Given sphereRadius and piVal, compute the volume of a sphere and assign sphereVolume
with the result. Use (4.0 / 3.0) to perform floating-point division, instead of (4 / 3) which
performs integer division.
#include <iostream>
The cost to ship a package is a flat fee of 75 cents plus 25 cents per
pound.
1. Declare a const named CENTS_PER_POUND and initialize with
25.
2. Get the shipping weight from user input storing the weight into
shipWeightPounds.
3. Using FLAT_FEE_CENTS and CENTS_PER_POUND constants,
assign shipCostCents with the cost of shipping a package weighing
shipWeightPounds.
Simple geometry can compute the height of an object from the
object's shadow length and shadow angle using the formula:
tan(angleElevation) = treeHeight / shadowLength.
1. Using simple algebra, rearrange that equation to solve for
treeHeight.
2. Write a statement to assign treeHeight with the height calculated
from an expression using angleElevation and shadowLength.#include
<iostream>
#include <cmath>
using namespace std;
int main( ) {
double treeHeight;
double shadowLength;
double angleElevation;
return 0;
A cashier distributes change using the maximum number of five dollar bills, followed
by one dollar bills. For example, 19 yields 3 fives and 4 ones. Write a single
statement that assigns the number of 1 dollar bills to variable numOnes, given
amountToChange. Hint: Use the % operator.
#include <iostream>
using namespace std;
int main() {
int amountToChange;
int numFives;
int numOnes;
amountToChange = 19;
numFives = amountToChange / 5;
numOnes = amountToChange % 5;
return 0;
}
Compute the average kids per family. Note that the integers should
be type cast to doubles.
#include <iostream>
using namespace std;
int main() {
int numKidsA;
int numKidsB;
int numKidsC;
int numFamilies;
double avgKids;
numKidsA = 1;
numKidsB = 4;
numKidsC = 5;
numFamilies = 3;
cout << "Average kids per family: " << avgKids << endl;
return 0;
A user
Amy_5
#include
<iostream
>
#include <string>
using namespace std;
/*
A user types a word and a number on a single line. Read
them into the provided variables. Then print: word_number. End
with newline. Example output if user entered: Amy 5
Amy_5
*/
int main() {
string userWord;
int userNum = 0;
/* Your solution goes here */
cin >> userWord;
cin >> userNum;
cout << userWord << "_" << userNum << endl;
123
2x speed
22
#include <iostream>
using namespace std;
int main() {
int userAge;
int insurancePrice;
return 0;
}