So I am trying to change my int vec1
that is populated with all zeroes. What I am doing for every even number I would make it to a 10 and for every odd would be an 11. But I am unsure how to do that and unsure how to print it out. What I thought was to use the % when finding out indices like vec1[0], vec1[2], and so forth are even and would turn them to 10. basically, a hint on how to look into the indices themselves.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v;
int vec1(10);
for (int i = 0; i <= 10; ++i)
{
vec2.push_back(i);
cout << vec1(i) <<" ";//keep getting error for this but unsure how to print it out
}
return 0;
}
You can achieve this like this:
First you have to loop over the v
, to check if we are on even index or odd index, we simply take mod with 2 of i
. If it is zero then it pushes 10, if not then 11. And in the second loop it simply print the array.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int size = 10;
vector<int> v(size, 0);
for (int i = 0; i < size; ++i)
if( i % 2 == 0)
v[i] = 10;
else
v[i] = 11;
for( auto i: v){
cout << i << " " ;
}
return 0;
}
You can get size
via cin >> size
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.