So, I am in the mid part of my C++ class and am having trouble with a vector project at the end of the chapter. I don't expect the answer, but if someone would point me in the right direction I would greatly appreciate it :)

#1). Write a loop that will print the values in a 20 element vector named quantity to the screen.
(When I compile the code it increments by 10 for the 20 values and I'm pretty sure this isn't correct)

#2). Write a loop that will initialize the values in a 100 element vector of type double, named x with the loops index value. For example, the element at subscript 0 should have a value of 0, and the value at subscript 11 should have a value of 11.
(When I compile the code it doesn't give me any values, just "0").

Here is my code:

``````// vector.cpp Project 9-1 pg. 232

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

vector <long> LongVector; // These 3 vectors are just step 1 of the project. Please ignore.
vector <float> n (10);
vector <int> z (100, 0);

int main()
{
// loop that prints 20 values
vector <double> quantity (20);
for (int index = 0; index < 20; index++)
{
cout << "Value " << index << quantity [index] << ": " << endl;
}

cout << endl << endl; // added this for neatness :)

// loop that prints 100 elements
vector <double> x (100);
for (double index = 0; index < 100; index++)
{
cout << "Element " << index + 1 << ": " << x [index] << endl;
}

return 0;
}``````

## All 11 Replies

#1 : Your vector 'quantity' is uninitialized, which means you are trying to print garbage.

#2 : Your loop is suppose to _fill_ the vector, now you are just printing it out like in #1.
Also, the loop counter must be named x, not the vector itself.

So if I ask you to make the first element of

``vector <double> vDouble (100);``

0, how would you do it?

Edit:
- In the second for loop, the index stays an integer. The type of the index never depends on what type you store in the vector.

Also, I don't know if the teacher mentioned this... but there are 'iterators' for vectors... maybe he wants you to use them.

Look into the `.push_back()` method of the vector as well. Otherwise you are treating your vectors like arrays and they have so much more power than that.

>>#1). Write a loop that will print the values in a 20 element vector named quantity to the screen.
(When I compile the code it increments by 10 for the 20 values and I'm pretty sure this isn't correct)

It's not actually incrementing by 10, it just looks like it because your output statement isn't written correctly. There are 2 different pieces of the output being mashed together to look like 1 output. Have another look at it. What elements of the statement can you re-arrange?

>>#2). Write a loop that will initialize the values in a 100 element vector of type double, named x with the loops index value. For example, the element at subscript 0 should have a value of 0, and the value at subscript 11 should have a value of 11.
(When I compile the code it doesn't give me any values, just "0").

Have another look at your statement on Line 25:

``vector <double> x (100);``

Technically, this statement is a declaration, NOT an initialization, just like either of these:

``````int myInt;
double myDouble;``````

Initialization involves assigning values to variables. The vector's constructor just happens to assign a default value of (0) to numeric data types. If you add proper assignment statements to the previous lines of code:

``````vector <double> x (100, 0); //<---notice the additional argument to the constructor
int myInt = 0;
double myDouble = 0.0;``````

then it becomes initialization. The problem with this version of initialization for a vector is that it makes all elements the same value. In this case that value is (0). Like with most arrays, to initialize a vector with varying values, you need to use a loop.
The loop you have created at this point is simply a display loop, it is NOT and initialization loop. You need to figure out how to write another loop that performs assignments between Lines 25 and 26

Your vector 'quantity' is uninitialized, which means you are trying to print garbage.

Not true. If you create a vector of integers and do not initislize it, the integers are initialized to 0.

More generally, if you create a vector of objects of type T, the initialization process creates an object initialized with the value T(), then copies that object into each element of the vector.

I am following some of the things that have been posted and thank you very much for answering. I have revised my code but now I have a set of new problems lol. Issues I am having now. I am taking online courses due to my busy schedule so asking you guys is faster than asking the teacher for help :)

#1). On the first loop it is now giving me a value, which is good but it says Value 0:. Shouldnt it say something else like; Value 1: 1?

#2). On the second loop when I state the size of my vector it should say 100, but it says 01. Then when it prints the values there is a zero in front of all the values.

``````// vector.cpp Project 9-1 pg. 232

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

vector <long> LongVector; // declares empty vector
vector <float> n (10);    // declares vector with 10 uninitialized elements
vector <int> z (100, 0);  // declares vector with 100 elements initialized to 0
int x;

int main()
{
// prints 20 values
vector <double> quantity (20);
for (int index = 0; index < 20; index++)
{
cout << "Value " << quantity [index] << ": " << index + 1 << endl; // changes
}

cout << endl << endl; // added this for neatness :)

// loop that prints 100 elements
vector <double> x (100, 0);
for (int index = 0; index < x.size(); index++)
x [index];

cout << "My Vector contains:";
for (int index = 0; index < x.size(); index++)
{
cout <<  x [index] <<index + 1 << endl;

}

return 0;
}``````

You still don't set your vector elements to any values. Line 29 does absolutely nothing.

If you've initialized a vector, you can set the elements like `x[index] = index` (or any other value for that matter). Otherwise you need to use `x.push_back(index)` (or any other value).

>>#1). On the first loop it is now giving me a value, which is good but it says Value 0:. Shouldnt it say something else like; Value 1: 1?
That's because you moved the wrong part of the output statement. Do you understand how output statements work? I'll be honest, I don't think you do.

POP QUIZ!!:
What will this produce for output?

``````const int MULTIPLIER = 3;
const int VEC_SIZE = 10;

...

vector <int> multipliedVals (VEC_SIZE, 0);  //declare a vector of VEC_SIZE ints

for (int i = 0; i < VEC_SIZE; ++i) {        //initialization loop
multipliedVals[i] = MULTIPLIER * i;
}

for (int j = 0; j < VEC_SIZE; ++j) {
cout << "Value @ element " << j << " is " << multipliedVals[j] << endl;
}

...``````

You still don't set your vector elements to any values. Line 29 does absolutely nothing.

If you've initialized a vector, you can set the elements like `x[index] = index` (or any other value for that matter). Otherwise you need to use `x.push_back(index)` (or any other value).

Thank you. I will post back later after I read the chapter again. I think I might have missed something (sarcastic).

>>#1). On the first loop it is now giving me a value, which is good but it says Value 0:. Shouldnt it say something else like; Value 1: 1?
That's because you moved the wrong part of the output statement. Do you understand how output statements work? I'll be honest, I don't think you do.

POP QUIZ!!:
What will this produce for output?

``````const int MULTIPLIER = 3;
const int VEC_SIZE = 10;

...

vector <int> multipliedVals (VEC_SIZE, 0);  //declare a vector of VEC_SIZE ints

for (int i = 0; i < VEC_SIZE; ++i) {        //initialization loop
multipliedVals[i] = MULTIPLIER * i;
}

for (int j = 0; j < VEC_SIZE; ++j) {
cout << "Value @ element " << j << " is " << multipliedVals[j] << endl;
}

...``````

Well, you didnt have to be mean about it. I'm a newbie remember. Yes I know how output statements work. The answer to your pop quiz is:
Value @ element 0 is 0
Value @ element 1 is 3
Value @ element 2 is 6
Value @ element 3 is 9
Value @ element 4 is 12
Value @ element 5 is 15
Value @ element 6 is 18
Value @ element 7 is 21
Value @ element 8 is 24
Value @ element 9 is 27

Thanks for the practice!

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

vector <long> LongVector; // declares empty vector
vector <float> n (10);    // declares vector with 10 uninitialized elements
vector <int> z (100, 0);  // declares vector with 100 elements initialized to 0
double x;

int main()
{
// prints 20 values
vector <double> quantity (20,0); // 20 element vector initialized to 0

cout << "quantity contains " << quantity.size() << " elements " << endl;
for (int index = 0; index < 20; ++index)
{
quantity[index] = index; // adding to vecror
}

for (int index = 0; index < 20; index++)
{
cout << index << ": " << quantity [index] << endl;
}

cout << endl << endl; // added this for neatness :)

// loop that prints 100 elements
vector <double> MyVal (100, 0); // 100 element vector initialized to 0

cout << "My Vector contains:" << MyVal.size() << " elements " << endl;
for (int index = 0; index < 100; ++index)
{
MyVal[index] = x++; // adding to vector
}

for (int index = 0; index < 100; ++index)
{
cout << "subscript " << index << ":  " << " has a value of " << MyVal[index] << endl;
}

return 0;
}``````

>>Well, you didnt have to be mean about it. I'm a newbie remember.
I know you're a newbie. Trust me, that wasn't "mean". I asked you a simple question.

The way you changed your statement around was nonsensical and created the impression that you didn't know how to build an output statement properly (i.e. you didn't know what parts go where). Based on the code you just posted, I think you picked up on what I was getting at, so don't gripe too much.

``````for (int index = 0; index < 100; ++index)
{
MyVal[index] = x++; // adding to vector
}``````

I know x is global, but you haven't initialized it and it's not an integer, it's a double. It seems to work, which surprises me. Normally you wouldn't handle a double that way, especially if it's uninitialized. Generally, I prefer to limit use of increment (++) and decrement (--) to initialized integers and use compound assignment instead (x+=1 or x-=1) on other types, it's a readability thing.

>>Well, you didnt have to be mean about it. I'm a newbie remember.
I know you're a newbie. Trust me, that wasn't "mean". I asked you a simple question.

The way you changed your statement around was nonsensical and created the impression that you didn't know how to build an output statement properly (i.e. you didn't know what parts go where). Based on the code you just posted, I think you picked up on what I was getting at, so don't gripe too much.