// Creating a vector that holds integers
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
int main()
{
vector<int> v;
for(int i = 0; i < 10; i++)
{v.push_back(i);}
int size = v.size();
for(int i = 0; i < size; i++)
{cout << v[i] << ", ";}
cout << endl;
for(int i = 0; i < size; i++)
{v[i] *= 10;} // v[i] = v[i] * 10;
for(int i = 0; i < size; i++)
{cout << v[i] << ", ";}
cout << endl;
}
/*
g++ Intvector.cpp -o Intvector
./Intvector
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 10, 20, 30, 40, 50, 60, 70, 80, 90,
*/
int size = v.size();
for(int i = 0; i < size; i++)
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 2-8. Create a vector<float> and put 25 floating-point numbers into it using a for loop. Display the vector.
// Creating a vector that holds floats
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
int main()
{
vector<float> v;
for(float f = 0.0; f < 25; f++) // iterate over float with integer values
{v.push_back(f/2);}
for(int i = 0, size = v.size(); i < size; i++) // iterate over integer i
{
cout << v[i] << ", ";
if (i == size/2) {cout << endl;}
}
cout << endl;
}
/*
g++ Floatvector.cpp -o Floatvector
./Floatvector
0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6,
6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10, 10.5, 11, 11.5, 12,
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 2-9. Create three vector<float> objects and fill the first two as in the previous exercise (Exercise_2-8). Write a for() loop that adds each corresponding element in the first two vectors and puts the result in the corresponding element of the third vector. Display all three vectors.
// Creating three vectors that hold floats
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
int main()
{
vector<float> v1, v2, v3;
for(float f = 0.0; f < 15; f++) // iterate over float with integer values
{
v1.push_back(f/2);
v2.push_back(f/3);
}
int size = v1.size(); // 15
for(int i = 0; i < size; i++)
{v3.push_back(v1[i]+v2[i]);}
cout << "v1: ";
for(int i = 0; i < size; i++)
{
cout << v1[i] << ", ";
if (i == size/2) {cout << endl << " ";}
}
cout << endl;
cout << "v2: ";
for(int i = 0; i < size; i++)
{
cout << v2[i] << ", ";
if (i == size/2) {cout << endl << " ";}
}
cout << endl;
cout << "v3: ";
for(int i = 0; i < size; i++)
{
cout << v3[i] << ", ";
if (i == size/2) {cout << endl << " ";}
}
cout << endl;
}
/*
g++ vectors.cpp -o vectors
./vectors
v1: 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5,
4, 4.5, 5, 5.5, 6, 6.5, 7,
v2: 0, 0.333333, 0.666667, 1, 1.33333, 1.66667, 2, 2.33333,
2.66667, 3, 3.33333, 3.66667, 4, 4.33333, 4.66667,
v3: 0, 0.833333, 1.66667, 2.5, 3.33333, 4.16667, 5, 5.83333,
6.66667, 7.5, 8.33333, 9.16667, 10, 10.8333, 11.6667,
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 2-10. Create a vector<float> and put 25 numbers into it as in the previous exercises. Now square each number and put the result back into the same location in the vector. Display the vector before and after the multiplication.
// Creating a vector that holds floats, then square its values
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
int main()
{
float temp;
vector<float> v;
for(float f = 0.0; f < 25; f++) // iterate over float with integer values
{v.push_back(f/2);}
int size = v.size(); // 25
for(int i = 0; i < size; i++)
{
cout << v[i] << ", ";
if (i == size/2) {cout << endl;}
}
cout << endl;
for(int i = 0; i < size; i++)
{
temp = v[i];
v[i] = temp * temp;
}
for(int i = 0; i < size; i++)
{
cout << v[i] << ", ";
if (i == size/2) {cout << endl;}
}
cout << endl;
}
/*
g++ SquareFloatvector.cpp -o SquareFloatvector
./SquareFloatvector
0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6,
6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10, 10.5, 11, 11.5, 12,
0, 0.25, 1, 2.25, 4, 6.25, 9, 12.25, 16, 20.25, 25, 30.25, 36,
42.25, 49, 56.25, 64, 72.25, 81, 90.25, 100, 110.25, 121, 132.25, 144,
*/