Vectors problem
Mar 21, 2018 at 12:13am UTC
I'm trying to make a program that displays the contents of various vectors, and for the last vector I need to make it display 5, 10, 15, 20, 25, and 30 using a for loop but can't figure out how. I'm trying to make it start at 5 and go up by increments of 5 but can't seem to get it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
// Vectors First Program.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector <int > example;
example.push_back(3);
example.push_back(10);
example.push_back(33);
for (int x = 0; x < example.size(); x++)
{
cout << example[x] << " " ;
}
cout << endl;
if (!example.empty())
example.clear();
vector <int > another_vector;
another_vector.push_back(10);
example.push_back(10);
if (example == another_vector)
{
example.push_back(20);
}
for (int y = 0; y < example.size(); y++)
{
cout<<example[y] << " " ;
}
cout << endl;
vector <int > another_another_vector;
for (int i = 0; i <= 25; i ++)
{
another_another_vector.push_back(i);
another_another_vector[i] = i + 5;
}
for (int i = 0; i <= 25; i ++)
{
cout << another_another_vector[i] << endl;
}
system("PAUSE" );
return 0;
}
Mar 21, 2018 at 8:35am UTC
The loop on line 37 should be:
1 2 3 4 5 6
for (int i = 5; i <= 25; i += 5)
{
another_another_vector.push_back(i);
another_another_vector[i] = i + 5;
}
Change the loop on line 42 like the loop on line 30:
1 2 3 4 5
for (int y = 0; y < another_another_vector.size(); y++)
{
cout<<another_another_vector[y] << endl;
}
Mar 21, 2018 at 10:06pm UTC
Thanks, this works just how I wanted it to.
Topic archived. No new replies allowed.