# STL Vector problem with code

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 02-14-2010
jamort
STL Vector problem with code
ok so i intended for this code to find the average of a numberOfElements defined by the person using the program... do i'm trying to use vector to pushback the array and make it larger, ad it works until it averages out the numbers and gives an insance answer... I'm not sure why, and help is appreciated...
thanks
CJ

Code:

```#include<iostream> #include<vector> using namespace std; int main() {     int numberOfElements;     vector<int> array;     int sum=0;     int arrayNumber = 0;     int forLoopNumber = 0;     bool finished = false;         cout<< "How many numbers will you be entering?";     cin>> numberOfElements;     cin.ignore();         array.push_back(1);     cout<<"\n Enter the numbers:\n\t\t\t";     while(finished == false){         cin>> array[arrayNumber];         cin.ignore();         if (arrayNumber + 1== numberOfElements){         finished = true;         }         if (arrayNumber + 1 != numberOfElements){         arrayNumber++;         array.push_back(1);         }     }     for (forLoopNumber = 0; forLoopNumber <= numberOfElements; forLoopNumber++){         sum += array[forLoopNumber];     }     cout<<"Average equals: " << sum/numberOfElements;     cin.get(); }```
• 02-14-2010
NeonBlack
Code:

```    for (forLoopNumber = 0; forLoopNumber <= numberOfElements; forLoopNumber++){         sum += array[forLoopNumber];     }     cout<<"Average equals: " << sum/numberOfElements;```
First of all, your for condition doesn't look quite right.

Second, the expression sum/numberOfElements is of type int, meaning that any fractional part of the quotient will be discarded (number will be rounded down).
• 02-14-2010
jamort
ok I belive I fixed the problem

somthing in the for loop wasn't working out... I also changed those that needed to be changed to float

Fixed code:

Code:

```#include<iostream> #include<vector> using namespace std; int main() {     float numberOfElements;     vector<int> array;     float sum=0;     int arrayNumber = 0;     int loopNumber = 0;     bool finished = false;         cout<< "How many numbers will you be entering?";     cin>> numberOfElements;     cin.ignore();         array.push_back(1);     cout<<"\n Enter the numbers:\n\t\t\t";     while(finished == false){         cin>> array[arrayNumber];         cin.ignore();         if (arrayNumber + 1== numberOfElements){         finished = true;         }         if (arrayNumber + 1 != numberOfElements){         arrayNumber++;         array.push_back(1);         }     }     while (loopNumber < numberOfElements){         sum += array[loopNumber];         loopNumber++;     }     cout<<"Average equals: " << sum/numberOfElements;     cin.get(); }```

Some optimization in the code:
Code:

```#include<iostream> #include<vector> using namespace std; int main() {     float numberOfElements;     vector<int> array;     float sum=0;     int arrayNumber = 0;     int loopNumber = 0;     bool finished = false;         cout<< "How many numbers will you be entering?";     cin>> numberOfElements;     cin.ignore();         array.push_back(1);     cout<<"\n Enter the numbers:\n\t\t\t";         while(finished == false){         cin>> array[arrayNumber];         cin.ignore();                         if (arrayNumber + 1== numberOfElements){               finished = true;         }                 else {               arrayNumber++;               array.push_back(1);         }     }         for (loopNumber = 0; loopNumber < numberOfElements; loopNumber++){         sum += array[loopNumber];     }         cout<<"Average equals: " << sum/numberOfElements;     cin.get(); }```
• 02-14-2010
Elysia
You are completely mistreating the vector. The whole point of the vector is that you do not need to worry about size. Read into a temp variable, then push that back. Don't push back an arbitrary number and read directly into the vector.
You basically got multiple if statements in the first loop. You can easily replace the second one with "else". Less typing, less prone to bugs and less code duplication. Better for all.
And the last loop should really be a for loop.
You've also borked the indentation in some places, especially in your first loop and the if statements.
• 02-14-2010
jamort
jeese lol tear it apart.... ok i see what your saying I'll work on it..
thanks for the input... I've done what I know exactly how to do I will fix the vector a little later..
thanks again
• 02-14-2010
Elysia
Good training! Then you won't make the same mistake next time ;)
• 02-15-2010
iMalc
Quote:

Originally Posted by jamort
Code:

```    float numberOfElements;     // ...     cout<< "How many numbers will you be entering?";     cin>> numberOfElements;```

Hmm so if I would like to enter 2.3 elements that's okay?
Nope, aparently 2.3 means an infinite number of items....
• 02-15-2010
jamort
Code:

```#include<iostream> #include<vector> using namespace std; int main() {     int numberOfElements;     int arrayNumber = 0;     int loopNumber = 0;     vector<int> array;     float sum=0;     bool finished = false;         cout<< "How many numbers will you be entering?";     cin>> numberOfElements;     cin.ignore();         cout<<" Enter the numbers:\n\t\t\t";         array.reserve(numberOfElements);         while(finished == false){         cin>> array[arrayNumber];         cin.ignore();                         if (arrayNumber + 1== numberOfElements){               finished = true;         }                 else {               arrayNumber++;         }     }         for (loopNumber = 0; loopNumber < numberOfElements; loopNumber++){         sum += array[loopNumber];     }         cout<<"Average equals: " << sum/numberOfElements;     cin.get(); }```
ok so i think theres probably something that i still need to fix, but i need to get a book on stl cause the one that i have doesnt really go into it
• 02-15-2010
Elysia
Don't trust the user.

int temp;
cin >> temp;
array.push_back(temp);
• 02-15-2010
jamort
Don't think I did what you said to do right.... let me know... I edited this because the way i had if there was a 0 or negative number entered it would mess up

Code:

```#include<iostream> #include<vector> using namespace std; int main() {     int numberOfElements = 0;     int arrayNumber = 0;     int loopNumber = 0;     vector<int> array;     float sum=0;     bool finished = false;     while (numberOfElements <= 0){           cout<< "How many numbers will you be entering?" << endl;           cin>> numberOfElements;           cin.ignore();                     if (numberOfElements <= 0){             cout<< endl << "Error... Please enter a positive integer"<<endl;;           }     }         cout<<" Enter the numbers:\n\t\t\t";         array.push_back(numberOfElements);         while(finished == false){         cin>> array[arrayNumber];         cin.ignore();                         if (arrayNumber + 1== numberOfElements){               finished = true;         }                 else {               arrayNumber++;         }     }         for (loopNumber = 0; loopNumber < numberOfElements; loopNumber++){         sum += array[loopNumber];     }         cout<<"Average equals: " << sum/numberOfElements;     cin.get(); }```
• 02-15-2010
Elysia
Code:

```#include<iostream> #include<vector> using namespace std; int main() {     int numberOfElements = 0;     int arrayNumber = 0;     int loopNumber = 0;     vector<int> array;     float sum=0;     bool finished = false;     int temp;     while (numberOfElements <= 0){           cout<< "How many numbers will you be entering?" << endl;           cin>> numberOfElements;           cin.ignore();                     if (numberOfElements <= 0){             cout<< endl << "Error... Please enter a positive integer"<<endl;;           }     }         cout<<" Enter the numbers:\n\t\t\t";     //    array.push_back(numberOfElements);         while(finished == false){         cin >> temp;         array.push_back(temp);         cin.ignore();                         if (arrayNumber + 1== numberOfElements){               finished = true;         }                 else {               arrayNumber++;         }     }         for (loopNumber = 0; loopNumber < numberOfElements; loopNumber++){         sum += array[loopNumber];     }         cout<<"Average equals: " << sum/numberOfElements;     cin.get(); }```
• 02-15-2010
jamort
ok... I see now so when you pushback an array the number that is passed becomes the new value...
thanks again... I got a book coming on stl and hopefully it will be here tomorrow...

"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
• 02-15-2010
Elysia
Quote:

Originally Posted by jamort
ok... I see now so when you pushback an array the number that is passed becomes the new value...

I'm not sure what you mean by that, but basically, push_back takes an argument and adds that argument to the end of its internal array. A vector is basically a dynamic array.
Safe and easy. You don't have to worry about a thing.
• 02-15-2010
jamort
yep thats what i meant it passes that arguement and then the arguement is added to the end of the array... I thought at first that the arguement that was passed was the amount you wanted to add to the array... and also I'm going to have to do some reading into this but will vector work with objects such as classes also?
• 02-15-2010
Elysia
Yes, it will. It works with any type.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last