STL Vector problem with code

This is a discussion on STL Vector problem with code within the C++ Programming forums, part of the General Programming Boards category; ok so i intended for this code to find the average of a numberOfElements defined by the person using the ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    106

    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();
    }

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    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).
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    106
    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();
    }
    Last edited by jamort; 02-14-2010 at 01:01 PM.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    106
    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

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    Good training! Then you won't make the same mistake next time
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,262
    Quote Originally Posted by jamort View Post
    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....
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    106
    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

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    Don't trust the user.
    Instead, you can do:

    int temp;
    cin >> temp;
    array.push_back(temp);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    106
    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();
    }
    Last edited by jamort; 02-15-2010 at 03:27 AM.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    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();
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    106
    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...

    thought id add this

    "Thanks Elysia. You're a programming master! How the hell do you know every thing?"

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    Quote Originally Posted by jamort View Post
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    106
    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?

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,171
    Yes, it will. It works with any type.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Array of Vectors amd other STL questions
    By kolistivra in forum C++ Programming
    Replies: 16
    Last Post: 04-12-2007, 09:11 AM
  4. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 11:38 AM
  5. Replies: 4
    Last Post: 01-15-2002, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21