two dimensional vector

This is a discussion on two dimensional vector within the C++ Programming forums, part of the General Programming Boards category; I know this is probably simple but I cannot get my user entered information to show up in my table ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    5

    two dimensional vector

    I know this is probably simple but I cannot get my user entered information to show up in my table display. I have messed with this until I am blind. Can someone please help me. Also can I make calculations by column?
    <code>
    int x ;
    int y;

    for( int row = 0; row < table.size(); row++ )
    {
    cout << "Enter Salesman number.";
    cin >> x ;

    for( int col = 1; col < table[row].size(); col++ )
    {
    cout << "Enter number sold.";
    cin >> y;
    }
    }


    cout << "Display table\n";
    for( row = x; row < table.size(); row++ )
    {
    for( int col = y; col < table[row].size(); col++ )
    cout << table[row][col] << '\t';
    cout << endl;
    }
    </code>

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Talking

    Show us your declarations- or all your code. What is table defined as?
    Mr. C: Author and Instructor

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    5
    Thanks for the response! Here is all of my code.

    <code>

    #include<iostream>
    #include <vector>
    #include<iomanip>

    using namespace std;

    using std::setw;
    using std::setiosflags;
    using std::setprecision;


    int main()
    {

    const int ROWS = 2,
    COLUMNS = 3;

    //instantiate a double dimen vector
    vector< vector<double> > table(ROWS,
    vector<double>(COLUMNS, 0.0) ) ;

    cout << "Value in row 0, column 2: " << table[0][2] << "\n";

    cout << "Number of Rows: " << table.size() << "\n";

    cout << "Number of Columns: " << table[1].size() << "\n";


    int x ;
    int y;

    for( int row = 0; row < table.size(); row++ )
    {

    cout << "Enter Salesman number.";
    cin >> x ;

    for( int col = 1; col < table[row].size(); col++ )
    {
    cout << "Enter number sold.";
    cin >> y;

    }

    }


    cout << "Display table\n";
    for( row = x; row < table.size(); row++ )
    {
    for( int col = y; col < table[row].size(); col++ )
    cout << table[row][col] << '\t';
    cout << endl;
    }



    return 0;

    }

  4. #4
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    why do some people use the vector library? why cant you just use

    Code:
    int array[10][10];
    //a two dimensional array of int with 10 rows and 10 columns
    nextus, the samurai warrior

  5. #5
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    when you use cin >> x, and cin >> y, it writes the input value to x or y. your loop just keeps over writing the same variables instead of adding the values to your table.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    5
    I'm sorry I don't understand. Could you give me an example?
    Thanks. Why does my table just print out zeros and not the last entry?

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Why not use a matrix header file...

  8. #8
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    I have no idea where you plan on storing or how you plan on using the salesman number, but if you want to store the number of items sold in a table, then you need to make sure you add it to the vectors like so:

    cin>>y;
    table[rows].push_back(y);

    Or better yet, unless you think you might change the number of rows and columns in the future, use a two-dimensional array which is easier.

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    5
    Doesn't using push_back add another row or column? I just want to fill my rows and columns with user info.

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    The best way is: go buy a book.

    Vectors are cool, but unless you're using them as a vector, by adding, removing, and modifying, they're pointless.

    do this:

    Code:
    double table[ROWS][COLUMNS];
    How is this set up?
    rows=2
    collumns=3

    so....

    Code:
               row
              0    1
          0  x    x
    col  1  x    x
          2  x    x
          3  x    x
    Remember, all arrays are zero-based. So a statement like this:

    Code:
    for( int col = 1; col < table[row].size(); col++ )
    Also, why do this:

    Code:
    cout << "Number of Rows: " << table.size() << "\n";
    cout << "Number of Columns: " << table[1].size() << "\n";
    When you can replace table.size() with ROWS and the other with COLUMNS? Its more efficient.

    Why does it print zeros? Because you filled it with zeros. Remember this?

    Code:
    vector< vector<double> > table(ROWS,
    vector<double>(COLUMNS, 0.0) ) ;
    That 0.0 is filling the whole collums with zeros. And no where in your code do you fill it up with somthing else.

    BTW, I can't make sense of your program, it just doesn't make any sense on what you're doing. You're output routine is very wierd, printing everything bottom-right of the coordinates (x,y)the user entered...

  11. #11
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Side note: Code tags are with [ and ], not < and >.

  12. #12
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Oops, you are right, i missed that you initialized them already, I thought you hadn't. Anyways, to put data into the table, you need to do something like this:

    cin>>table[row][col];

    And like it has been stated before, vectors for something like this are pointless, use two-dimensional arrays.

  13. #13
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by nextus
    why do some people use the vector library? why cant you just use

    Code:
    int array[10][10];
    //a two dimensional array of int with 10 rows and 10 columns
    See: http://www.parashift.com/c++-faq-lit....html#faq-34.1
    Although I do agree that a vector of vectors is... ...stupid.

  14. #14
    Registered User
    Join Date
    Feb 2003
    Posts
    5
    PJYelton - Thank-you very much! You actually helped me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 09:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 11:38 AM
  3. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 06:44 PM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 01:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM

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