Two Hard-coded Arrays WITH DIFFERENT SIZES into One 2-Dimensional Vector

This is a discussion on Two Hard-coded Arrays WITH DIFFERENT SIZES into One 2-Dimensional Vector within the C++ Programming forums, part of the General Programming Boards category; I am using 2 ARRAYS OF DIFFERENT SIZES in One 2-Dimensional Vector, and my output is not correct. The arrays ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    28

    Two Hard-coded Arrays WITH DIFFERENT SIZES into One 2-Dimensional Vector

    I am using 2 ARRAYS OF DIFFERENT SIZES in One 2-Dimensional Vector, and my output is not correct. The arrays are size 4 and size 13.
    I want COLUMN 0 to have: 55, 66, 77, 88.
    I want COLUMNs 1-12 to have 1,2,3,4,5,6,7,8,9,10,10,10,11 in EACH ROW. It would seem that the 2nd loop for the size 13 array would need to loop 4 times in order to fill 4 rows, however, I'm not sure how to do that. Here is what I have so far in code and output:

    Code:
    #include <iostream>     
    #include <vector> 
      
    using namespace std; 
      
    int main() 
    { 
        int typeArray[4] = {55,66,77,88}; 
        int valArray[13] = {1,2,3,4,5,6,7,8,9,10,10,10,11}; 
      
        // 3 = LENGTH or NUMBER of ROWS; 2 = WIDTH or NUMBER of COLUMNS; 
        //  0 = VALUE all cells are initialized to 
        // using 2 "for loops" 
        vector< vector <int> > myVector(4, vector<int> (13,0)); 
      
      for (int i = 0; i < myVector.size(); i++)  
        { 
          myVector[i][0] = typeArray[i]; 
      
          for (int j = 0; j < myVector[i].size(); j++)  
            { 
               myVector[1][j] = valArray[j]; 
             } 
      
          } 
      
     // print vector to screen with 4 ROWS, 13 COLUMNS 
            for (int i = 0; i < 4; i++) 
              {          
                for (int j = 0; j < 13; j++) 
                 {  
                  cout << myVector[i][j] << ' '; 
                  }          
                  cout << '\n'; 
              } 
        system("Pause"); 
        return 0;  
    }
    ------------------------------------
    OUTPUT:
    55 0 0 0 0 0 0 0 0 0 0 0 0
    1 2 3 4 5 6 7 8 9 10 10 10 11
    77 0 0 0 0 0 0 0 0 0 0 0 0
    88 0 0 0 0 0 0 0 0 0 0 0 0
    -----------------------------
    Please advise how to populate rows correctly, thank you.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,915
    So do you want your output to be?
    55,1,2,3,4,5,6,7,8,9,10,10,10,11
    66,1,2,3,4,5,6,7,8,9,10,10,10,11
    77,1,2,3,4,5,6,7,8,9,10,10,10,11
    88,1,2,3,4,5,6,7,8,9,10,10,10,11

    Jim

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    Hello Jim, yes that is exactly what I want it to look like, thanks.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,915
    Then to populate this vector you will loop through an outer loop 4 times and store the correct element 0 and then loop starting at 1 to populate the rest of the elements. Also your vector[x].size() should be 14 instead of 13.

    Or you could populate the vector with your array.
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    const int typeSize = 4;
    const int valSize = 14;
    
    int main()
    {
       int typeArray[typeSize] = {55,66,77,88};
       int valArray[valSize] = {0,1,2,3,4,5,6,7,8,9,10,10,10,11};
    
       // Create a vector of vector of ints populated by valArray
       vector< vector <int> > myVector(typeSize, vector<int>(valArray, valArray + sizeof(valArray) / sizeof(int)));
       for(size_t i = 0; i < myVector.size(); ++i)
          myVector[i][0] = typeArray[i];
    
    // print vector to screen with 4 ROWS, 13 COLUMNS
       for (size_t i = 0; i < myVector.size(); i++)
       {
          for (size_t j = 0; j < myVector[0].size(); j++)
          {
             cout << myVector[i][j] << ' ';
          }
          cout << '\n';
       }
       cin.get();
    
       return 0;
    }
    Notice the added element in the valArray array.

    Jim

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    Hi Jim, thank you for the revised code! Let me re-code my program and put together some questions I have about how you are doing it. e.g. assigning "valArray" in the 2-D vector declaration rather than loop through it. This is actually one of the biggest issues I had originally with how to populate a 2-Dimensional vector with two different arrays. Let me work on this, and I'll get back to you shortly, thanks!
    Last edited by codechick; 03-24-2012 at 05:08 PM.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    Hi Jim,
    This code works perfectly. I am wondering if there is a way to make the "valArray" size 13 without the extra -0- and then manipulate the index by subtracting a -1-? I'm going to do math on the "valArray" indices and would prefer not to have the "-0-" if possible.

    Otherwise, this is a really nice example of solid coding that helps put together a lot of pieces of info that I know and needed help from someone like you to understand and put together.

    Thank you, please let me know about the first question.
    I'm going to post again with changing the "typeArray" values to strings and outputting them with the "valArray" numbers.
    So, if you're interested, please keep an eye out, thank will be tomorrow, thx!

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    2,915
    This code works perfectly. I am wondering if there is a way to make the "valArray" size 13 without the extra -0- and then manipulate the index by subtracting a -1-? I'm going to do math on the "valArray" indices and would prefer not to have the "-0-" if possible.
    Yes but the you would not be able to use valArray to initialize your vector, you will need to use a loop after you define your vector.

    Jim

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    Yes but the you would not be able to use valArray to initialize your vector
    I disagree - Something like this could work perhaps.

    I must admit I fail to see the shortcomings of the initial answer regardless.

    Code:
    #include <vector>
    
    int main()
    {
        typedef std::vector<int>::size_type typeArrSize;
        int typeArray[] = {55,66,77,88};
        int valArray[] = {1,2,3,4,5,6,7,8,9,10,10,10,11}; 
    
        std::vector< vector<int> > my( sizeof typeArray / sizeof typeArray[0]
           , vector<int>(valArray, valArray + sizeof valArray / sizeof valArray[0]) );
    
        for (typeArrSize i = 0; i < my.size(); i++) {
           my[i].insert(my[i].begin(), typeArray[i]);
        }
    }

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    2,915
    That should also work, didn't think about insert().


    Jim

  10. #10
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    Hello Jim and White Flags,

    I just logged in tonight. Thank you for the responses and the alternate ways of initializing the vector. As mentioned, I have learned A LOT on this, thank you.
    Tomorrow, I"m putting up the next piece of the program, so pls keep an eye out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Two Hard-coded Arrays into One 2-Dimensional Vector
    By codechick in forum C++ Programming
    Replies: 11
    Last Post: 03-24-2012, 03:41 PM
  2. sizes of arrays within functions
    By tempster09 in forum C Programming
    Replies: 3
    Last Post: 01-13-2010, 11:17 PM
  3. build a vector of vectors, sizes not known
    By pheres in forum C++ Programming
    Replies: 8
    Last Post: 09-04-2007, 12:43 PM
  4. arrays with different sizes..
    By zoso123 in forum C Programming
    Replies: 8
    Last Post: 06-30-2006, 05:24 PM
  5. Array sizes and arbitrary sized arrays
    By bennyandthejets in forum C++ Programming
    Replies: 3
    Last Post: 07-04-2003, 12:24 AM

Tags for this Thread


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