Thread: vectors

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    104

    vectors

    Below is my code.
    Why does the statement cout<<" numbers[i]=" << numbers[i];
    always print out the last number in the vector numbers? Plus if
    you see any things I could be doing better in the code, please let me know.

    Code:
    #include <string>
    #include <vector>
    #include <iostream>
    #include <stdlib>
    using namespace std;
    class SpeedDial {
    	public:
    	int assignNumbers(vector <int> numbers, vector <int> howMany, int slots) {
            int leastKeyPresses=-1;
            int numbs=howMany.size();
            int ttlKeyPrsz=0,ttlKeyPrszSvd=0,copy=0;
            char snumb[50];
            vector <int> noKeyPress(numbs);
            vector <int> noNumbs(numbs);
            vector <int> KeyPreszSvd(numbs);
            for(int i=0; i<numbs; i++){
                cout<<" numbers[i]=" << numbers[i];
                itoa(numbers[i],snumb,10);
                noNumbs[i]=strlen(snumb);
                noKeyPress[i]=noNumbs[i]*howMany[i];
                ttlKeyPrsz += noKeyPress[i];
                KeyPreszSvd[i]=noKeyPress[i]-(howMany[i]*2);
            }
            for(int j=1; j<numbs; j++){
                if(KeyPreszSvd[j]>KeyPreszSvd[j-1]){
                    copy=KeyPreszSvd[j-1];
                    KeyPreszSvd[j-1]=KeyPreszSvd[j];
                    KeyPreszSvd[j]=copy;
                }
            }
            if(slots>numbs)slots=numbs;
            for(int i=0; i<slots; i++){
    //            cout<<KeyPreszSvd[i];
                ttlKeyPrszSvd+=KeyPreszSvd[i];
            }
            cout<<ttlKeyPrsz << " " << ttlKeyPrszSvd << " ";
            leastKeyPresses=ttlKeyPrsz-ttlKeyPrszSvd;
            return (leastKeyPresses);
    	}
    };
    
    
    void main(){
            int slots=4;
    //        int slots=5;
    /*
            vector <int> numbers(6,3);
            vector <int> howMany(1,7);
    */
    
            vector <int> numbers(6,(9753,1245987,4833,34473,8733,1437));
            vector <int> howMany(6,(5,2,4,3,2,4));
    /*
            vector <int> numbers(3,(124839,9876,43823));
            vector <int> howMany(3,(73,95,102));
    */
             SpeedDial *p;
             cout << p->assignNumbers(numbers,howMany,slots);
             cin >> "";
    }
    Last edited by kes103; 01-05-2003 at 09:50 PM.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    there are some errors here,
    Code:
    #include <cstring>
    #include <vector>
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    class SpeedDial {
    public:
    int assignNumbers(vector <int> numbers, vector <int> howMany, int slots) {
            int leastKeyPresses=-1;
            int numbs=howMany.size();
            int ttlKeyPrsz=0,ttlKeyPrszSvd=0,copy=0;
            char snumb[50];
            vector <int> noKeyPress(numbs);
            vector <int> noNumbs(numbs);
            vector <int> KeyPreszSvd(numbs);
            for(int i=0; i<numbs; i++){
                cout<<" numbers[i]=" << numbers[i];
                itoa(numbers[i],snumb,10);
                noNumbs[i]=strlen(snumb);
                noKeyPress[i]=noNumbs[i]*howMany[i];
                ttlKeyPrsz += noKeyPress[i];
                KeyPreszSvd[i]=noKeyPress[i]-(howMany[i]*2);
            }
            for(int j=1; j<numbs; j++){
                if(KeyPreszSvd[j]>KeyPreszSvd[j-1]){
                    copy=KeyPreszSvd[j-1];
                    KeyPreszSvd[j-1]=KeyPreszSvd[j];
                    KeyPreszSvd[j]=copy;
                }
            }
            if(slots>numbs)slots=numbs;
            for(int i=0; i<slots; i++){
    //            cout<<KeyPreszSvd[i];
                ttlKeyPrszSvd+=KeyPreszSvd[i];
            }
            cout<<ttlKeyPrsz << " " << ttlKeyPrszSvd << " ";
            leastKeyPresses=ttlKeyPrsz-ttlKeyPrszSvd;
            return (leastKeyPresses);
    }
    };
    
    
    int main(){ //NO void main, main must return int
            int slots=4;
    //        int slots=5;
    /*
            vector <int> numbers(6,3);
            vector <int> howMany(1,7);
    */
    
            vector <int> numbers(6,(9753,1245987,4833,34473,8733,1437));
            vector <int> howMany(6,(5,2,4,3,2,4));
    /*
            vector <int> numbers(3,(124839,9876,43823));
            vector <int> howMany(3,(73,95,102));
    */
             SpeedDial *p;
             cout << p->assignNumbers(numbers,howMany,slots);
             cin >> "";  //error here, what are you trying to do here?
    		 
    		 return 0;
    }

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    104

    vectors

    Alpha:
    Thanks, but it doesn't work any different when making main an integer type. The difference between void and int is that int must
    return a value and void doesn't return a value.
    But, you did not answer my main question.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >The difference between void and int is that int must return a value and void doesn't return a value.
    True, but in the case of main, anything but a return value of int is undefined and quite wrong.

    >But, you did not answer my main question.
    I will.

    >Why does the statement cout<<" numbers[i]=" << numbers[i]; always print out the last number in the vector numbers?
    Because you don't initialize vectors like that, remember that vectors are not arrays. There's no constructor that takes a parenthesized list and assigns each value in that list to the vector. The reason you get the last item in the list is because of how the comma operator works. Every expression but the last one will be evaluated and the result discarded, after all of this your constructor looks like this:

    vector <int> numbers ( 6, 1437 );

    Which assigns 6 copies of 1437 to the vector. Instead, you should create an empty vector and simply push the values onto it or set the size initially:

    vector <int> numbers ( 6, 0 );

    And place each value manually with the subscript operator:

    numbers[0] = 9753;
    numbers[1] = 1245987;
    ...etc

    >cout << p->assignNumbers ( numbers, howMany, slots );
    You've assigned no memory to p, dereferencing a garbage pointer is undefined.

    Try this instead:
    Code:
    int main(){
      int slots=4;
      
      vector <int> numbers;
      vector <int> howMany;
      
      numbers.push_back(9753);
      numbers.push_back(1245987);
      numbers.push_back(4833);
      numbers.push_back(34473);
      numbers.push_back(8733);
      numbers.push_back(1437);
      
      howMany.push_back(5);
      howMany.push_back(2);
      howMany.push_back(4);
      howMany.push_back(3);
      howMany.push_back(2);
      howMany.push_back(4);
      
      SpeedDial p;
      
      cout << p.assignNumbers(numbers,howMany,slots);
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 11:13 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM