CMSC 160 Fall 2018
 10/25/2018 Exploring Vectors and Loops Due:  end of lab

Handin

You may work in groups of up to 3.  If so, hand in only ONE submission sheet.    It will be assumed that all members of the group participated in each part.

Assignment:    (3 points)

PART A:   The structure for these should be:

#include <iostream>
#include <vector>
using namespace std;

int main()
{
// each snippet goes  here
}

For each of the following snippets:
1. Try to predict what it does
2. Type the code in to a program, run it and see if you were right.
3. If you were wrong, understand what happened.  Ask questions as necessary.
 1 string s = “Quiz Today.”; for (int i = 3; i < 7; i++)     cout << s[i]; 2 string s = “Quiz Today.”; for (char ch : s)     cout << ch; 3 vector list = {4,7,8,10,3,5}; for (int num : list)       cout << num; 4 vector list = {4,7,8,10,3,5}; for (int num : list)     if (num < 6)        cout << num; 5 vector list = {4,7,8,10,3,5}; for (int num1 : list)    for (int num2 : list)       cout << num1 << " " << num2 << endl; 6 vector list = {4,7,8,10,3,5}; for (int I = 0; I < list.size(); I++)         cout << list[I] << endl; 7 vector list = {4,7,8,10,3,5}; for (int I = 0; I < list.size(); I++)      if (list[I] < 7)         cout << list[I] << endl; 8 vector list = {4,7,8,10,3,5}; for (int I = 0; I < list.size(); I++)    for (int J = 0;  J < list.size(); J++)         cout << list[I] << endl; 9 vector list = {4,7,8,10,3,5}; for (int I = 0; I < list.size(); I++)    for (int J = 0;  J < list.size(); J++)         cout << list[J] << endl; 10 vector list = {4,7,8,10,3,5}; for (int I = 0; I < list.size(); I++)    for (int J = 0;  J < I; J++)         cout << list[J] << endl; 11 vector list = {4,7,8,10,3,5}; for (int I = 0; I <= list.size(); I++)         cout << list[I] << endl; 12 vector names = {"Joe","Chris"}; for (string person:  names)      cout << person; 13 vector names = {"Mary","Tom"}; names.push_back("Terry"); cout << names.size() << endl; for (string person:  names)      cout << person; 14 vector info ;   info.push_back(3.14);   info.push_back(5.1);   info.push_back(9.8);   info.push_back(5.6);   for (double num : info)     cout << num << endl;   info[2] = info[3] * 2;     for (double num : info)     cout << num << endl;   for (int i = 0; i < 15; i ++)     cout << i << ":" << info[i] << endl;

PART B:

1)  On the server, retrieve the file    VectorLabB1.cpp
• Look at the file.
• Attempt to compile and run.
• Now, change the line  cout << info << endl;      to say    displayVectorOfDoubles(info);
• Compile and run.
• Note that there is an extra comma at the end of the display.     How might you fix this?
• Now, change the line:    void displayVectorOfDoubles(vector<double> data)      to   be     void displayVectorOfDoubles(vector<double> &   data)
• Compile and run.

2)  On the server, retrieve the file    VectorLabB2.cpp

• Look at the file.
• Attempt to compile and run.   Note that there is a function missing.      This function can be found on page 287 of the textbook (in section 6.7.3).
• Add the function to the file and get the program to run.
• Observe the behavior.

Homework:

Read sections  6.1, 6.2, 6.3,  6.5, and 6.7
NOTE:   The first sections use something called arrays rather than vectors, but they are very similar.
Differences:
• With arrays,  they have to have a separate variable to represent the size, whereas vectors  have a function    vectorname.size()  that says how many elements there are.
• The declaration of arrays is different.    EG     double  []  list;      vs    vector<double>   list;