push_back()
See Announcements link in Canvas to keep up with what is going on. Here are a few for review:
Appeal must included citations to sources of information that document or support an alternative answer. Teams may access reference materials during the appeal.
Appeal must include an appropriate rewrite of questions or answers that you interpret as ambiguous or confusing.
do-while
loopsdo-while
(or just do
) loop:
do { statements } while (test); // loop test
do-while
loop is to validate user input#include <iostream> using namespace std; int main() { double input = 0.0; // initialize value do { cout << "Enter a positive number: "; cin >> input; if (input <= 0.0) { cout << "You must enter a positive number\n"; } } while (input <= 0.0); // test condition at end cout << "You entered: " << input << endl; return 0; }
Any problems with any of these Coding Challenges? (Zybooks sign in)
If so, list the CA numbers in Chat
In this exercise we use indefinite loops to process user input and to ensure correct user input.
Remember to verify your code by compiling after each step.
For this exercise we break into teams. Within the team, work with each other to develop a solution. When the team has finished, choose one member to show your solution to the class by sharing your screen. The instructor will ask one team to share their solution.
#include <iostream> using namespace std; int main() { // Enter your code here return 0; }
double
named sumScores
and nextScore
and initialize the variables to 0
. In addition, declare an integer variable named count
and initialize it to 0
. The following is the pseudocode for these steps:
set sumScores to 0 set nextScore to 0 set count to 0
Compile your code to make sure you declared the variables correctly.
sumScores
after the loop.
cout << "\nSum of scores: " << sumScores << endl;
When you run the program, the output should look like:
Score 1: 38 Score 2: 39 Score 3: -1 Sum of scores: 77
do
-while
loop instead. Replace your current loop with the following:
Note that the statements inside the loop did not change, only the loop statement itself. To make sure you made the changes correctly, compile and run your code and check to see if it works the same. The difference between a while
and do-while
loop is that a do-while
ensures the body of the loop is executed at least once.
scores.cpp
", and submit the file to Canvas with the rest of the exercise files for the week.When finished developing your code click here to verify. Code need not look exactly the same. After you have completed your own program, reviewing another is often helpful in learning how to improve your programming skills.
vector
:
#include <vector> using namespace std;
scores
that holds 10 values of type int
:
vector<int> scores(10);
int
vector<int> scores = { 90, 95, 97, 89, 98 }; // 5 values
at()
function:
scores.at(4) = 98;
int
in this case) and can be used like a variable:
cout << scores.at(4) << endl;
for (int i = 0; i < 5; i++) { cout << scores.at(i) << endl; }
size()
function
vector<int> scores = { 90, 95, 97, 89, 98 }; cout << scores.size() << endl;
size()
function is useful in counting loops:
unsigned i; for (i = 0; i < scores.size(); i++) { // do something with scores[i] cout << scores.at(i) << endl; }
unsigned
data type is shorthand for unsigned int
Any problems with any of these Coding Challenges? (Zybooks sign in)
If so, list the CA numbers in Chat
In this exercise we create a vector.
Remember to verify your code by compiling after each step.
For this exercise we break into teams. Within the team, work with each other to develop a solution. When the team has finished, choose one member to show your solution to the class by sharing your screen. The instructor will ask one team to share their solution.
#include <iostream> using namespace std; int main() { // Enter your code here return 0; }
main()
, add a statement to define a vector of type double
named temp
along with a list of five (5) randomly chosen values.cout << temp.at(0) << endl;
for
-loop that accesses every index of the temp
vector and prints every value to the screen like:
for (unsigned i = 0; i < temp.size(); ++i) { cout << temp.at(i) << endl; }
total
before the loop and initialize the variable to zero (0).temp.at(i)
value to total
every time the loop iterates.
for (unsigned i = 0; i < temp.size(); i++) { cout << temp.at(i) << endl; total = total + temp.at(i); }
total
.total
.templist.cpp
", and submit the file to Canvas with the rest of the exercise files for the week.When finished developing your code click here to verify. Code need not look exactly the same. After you have completed your own program, reviewing another is often helpful in learning how to improve your programming skills.
for
loopvector<int> scores = { 90, 95, 97, 89, 98 }; int sum = 0; for (unsigned i = 0; i < scores.size(); i++) { sum = sum + scores.at(i); } cout << "Sum=" << sum << endl; cout << "Average=" << sum / scores.size() << endl;
if
statements to test for a condition
vector<int> scores = { 90, 95, 97, 89, 98 }; int value; for (unsigned i = 0; i < scores.size(); i++) { if (test condition) { value = scores.at(i); } } cout << value << endl;
if
statement the code updates the valueAny problems with any of these Coding Challenges? (Zybooks sign in)
If so, list the CA numbers in Chat
In this exercise we use iteration for common computations on a vector.
For this exercise we break into teams. Within the team, work with each other to develop a solution. When the team has finished, choose one member to show your solution to the class by sharing your screen. The instructor will ask one team to share their solution.
#include <iostream> #include <vector> using namespace std; int main() { // Enter your code here return 0; }
main()
, add a statement to define a vector of type double
named temp
along with a list of five (5) randomly chosen values.double
to store the sum, minimum and maximum values of the vector, like:
double sum = 0; double min = temp.at(0); double max = temp.at(0);
Notice we initialize the sum
to 0 and the min
and max to the first value of the vector. Add a comment after each variable declaration to explain why it is initialized in that way.
for
-loop that accesses every index of the temp
vector and adds it to the sum, like:
for (unsigned i = 0; i < temp.size(); ++i) { sum = sum + temp.at(i); }
cout << "Sum=" << sum << endl; cout << "Average=" << sum / temp.size() << endl; cout << "Minimum=" << min << endl; cout << "Maximum=" << max << endl;
for
loop, after the sum computation, add an if
statement to test for a minimum value, like:
if (min > temp.at(i)) { min = temp.at(i); }
if
statement to test for a maximum value.iterate.cpp
", and submit the file to Canvas with the rest of the exercise files for the week.When finished developing your code click here to verify. Code need not look exactly the same. After you have completed your own program, reviewing another is often helpful in learning how to improve your programming skills.
Name | Price |
---|---|
Milk | 3.95 |
Bread | 2.99 |
Cheese | 3.95 |
Two Vectors Storing Related Data
Name Vector
Bread
Price Vector
.at(0)
.at(1)
.at(2)
.at(3)
2.99
.at(4)
Slice
.at(5)
.at(6)
.at(7)
.at(8)
.at(9)
#include <iostream> #include <vector> using namespace std; int main() { vector<string> product = { "Apple", "Bread", "Cheese", "Milk", "Orange" }; vector<double> price = { 1.62, 2.99, 4.95, 3.95, 1.10 }; cout << "Enter a product name: "; string name; cin >> name; for (unsigned i = 0; i < product.size(); i++) { if (product.at(i) == name) { cout << name << " costs " << price.at(i) << endl; } } return 0; }
Any problems with any of these Coding Challenges? (Zybooks sign in)
If so, list the CA numbers in Chat
In this exercise we explore the use of multiple vectors.
For this exercise we break into teams. Within the team, work with each other to develop a solution. When the team has finished, choose one member to show your solution to the class by sharing your screen. The instructor will ask one team to share their solution.
#include <iostream> using namespace std; int main() { // add code here return 0; }
vector<string> area = { "Aptos", "Capitola", "Felton", "Santa Cruz", "Watsonville" }; vector<int> particleCounts = { 37, 39, 51, 53, 42 };
getline()
.for
loop to look up the location in the area vector. Inside the for
loop we place an if
statement to test for a match between a vector element for area and the location entered by the user.
cout << location << " particle readings are " << particleCounts.at(i) << endl;
Enter a location: Aptos Aptos particle readings are 37
particulates.cpp
", and submit the file to Canvas with the rest of the exercise files for the week.When finished developing your code click here to verify. Code need not look exactly the same. After you have completed your own program, reviewing another is often helpful in learning how to improve your programming skills.
push_back()
push_back()
vector<int> scores; cout << scores.size() << endl;
push_back()
function resizes the vector by adding one element to its end:
scores.push_back(42); cout << scores.size() << endl;
back()
, returns the last value in a vector
cout << scores.back() << endl;
pop_back()
, removes the last element of a vector, shrinking its size by one:
scores.pop_back(); cout << scores.size() << endl;
#include <iostream> #include <vector> using namespace std; int main() { vector<int> scores; cout << "Enter scores (-1 to quit):\n"; int value = 0; while (value != -1) { cin >> value; if (value != -1) { scores.push_back(value); } } cout << "You entered:\n"; for (unsigned i = 0; i < scores.size(); i++) { cout << scores.at(i) << endl; } return 0; }
Any problems with any of these Coding Challenges? (Zybooks sign in)
If so, list the CA numbers in Chat
push_back()
(12m)In this exercise we add and remove elements from the end of a vector.
For this exercise we break into teams. Within the team, work with each other to develop a solution. When the team has finished, choose one member to show your solution to the class by sharing your screen. The instructor will ask one team to share their solution.
#include <iostream> #include <vector> using namespace std; int main() { // Enter your code here return 0; }
main()
, add a statement to define a vector of type double
named temp
along with a list of five (5) randomly chosen values.for
-loop that accesses every index of the temp
vector and prints every value to the screen followed by a space. When run, the output should look like the following, depending on the numbers chosen.
12.3 23.4 34.5 45.6 56.7
temp
vector using the push_back()
function. For example, here is how to add one element:
temp.push_back(42.1);
cout << "size: " << temp.size() << endl; cout << "last: " << temp.back() << endl;
for
-loop from step 4. When run, the output should look like the following, depending on the numbers chosen.
12.3 23.4 34.5 45.6 56.7 size: 7 last: 24.6 12.3 23.4 34.5 45.6 56.7 42.1 24.6
pop_back()
and then print the size and last element again:
temp.pop_back(); cout << temp.size() << endl; cout << temp.back() << endl;
for
-loop from step 4. When run, the output should look like the following, depending on the numbers chosen.
12.3 23.4 34.5 45.6 56.7 size: 7 last: 24.6 12.3 23.4 34.5 45.6 56.7 42.1 24.6 size: 6 last: 42.1 12.3 23.4 34.5 45.6 56.7 42.1
pushback.cpp
", and submit the file to Canvas with the rest of the exercise files for the week.When finished developing your code click here to verify. Code need not look exactly the same. After you have completed your own program, reviewing another is often helpful in learning how to improve your programming skills.