1

I am working on C++ printing vectors in grid. Here, I need to put random numbers in a vector size of 3x * 3y. And then I have to print them out with two dimensional matrix with one array. I do not understand how to represent two dimensional matrix with one array. In addition, I am not sure how to print out multidimensional vectors. I have to work on print_vector function which prints vectors with grid form. Could you please help me to improve this code below?

int main()
{
  populate_vector();
  return 0;
}

void populate_vector()
{

  int x,y;
  cout<<"Enter two Vectors x and y \n->";
  cin>> x;
  cin>> y;
  srand((unsigned)time(NULL));
  std::vector<int> xVector((3*x) * (3*y));
  for(int i = 0; (i == 3*x && i == 3*y); ++i){
    xVector[i] = rand() % 255 + 1;
       if(i == 3*x){
         cout << "\n";
       }
  }
  print_vector(xVector);
}

void print_vector(vector<int> &x) {


}
Dan
  • 35
  • 1
  • 6
  • I suggest using a double for loop and reconstructing the index into the flat array from the two indices (i * width + j), the \n naturally goes after the inner loop and this translates well to a vector of vectors for example. – Borgleader Mar 14 '17 at 15:46
  • Could you please give me an example of that for loop? – Dan Mar 14 '17 at 15:48

2 Answers2

0

Something like this will clarify your code, there is a procedure where the vector is populated, and another one where the vector is printed

int main()
{
    int x,y;
  std::cout<<"Enter two Vectors x and y \n->";
  std::cin>> x;
  std::cin>> y;

  srand((unsigned)time(NULL));
  int xSize = x * 3; // it is important to have the size of the final grid stored
  int ySize = y * 3; // for code clarity 
  std::vector<int> xVector( xSize * ySize);
  // iterate all y 
  for ( y = 0 ; y < ySize; ++y) {
      // iterate all x 
    for ( x = 0 ; x < xSize;  ++x) {
      // using row major order https://en.wikipedia.org/wiki/Row-_and_column-major_order
        xVector[y * xSize + x]  = rand() % 255 + 1;
    }
  }
  // when printing you want to run y first 
  for ( y = 0 ; y < ySize; ++y) {
    for ( x = 0 ; x < xSize;  ++x) {
      // iterate all y 
        printf("%d ", xVector[y * xSize + x] );
    }
    printf("\n");
   }
}

I think you want to pay attention to this step, where you can convert x and y position into a one array dimension. It's simple you just have to multiply the y by the size of x and add x.

So something like this in two dimensions

1 2 3 
4 5 6 

will end up in something like this

1 2 3 4 5 6

you can see it running here

saykou
  • 167
  • 1
  • 9
0

I'm not an expert, but I like to just have a vector of vectors.. something like:

void print_vector(vector<vector<int>>& m_vec)
{
    for(auto itY: m_vec)
    {
        for(auto itX: itY)
            cout << itX << " ";

        cout << endl;
    }
}

void populate_vector()
{
    int x,y;
    cout << "Enter Two Vectors x and y \n ->";
    cin >> x;
    cin >> y;
    srand((unsigned)time(NULL));
    vector<vector <int>> yVector;
    for(auto i = 0; i < y*3; ++i)
    {
        vector<int> xVector;
        for(auto j = 0; j < x*3; ++j)
            xVector.push_back(rand()%255+1);

        yVector.push_back(xVector);
    }
    print_vector(yVector);
}

EDIT: Ooh, I'd never seen this site before, thanks Saykou... here is the code working: http://cpp.sh/3vzg

Godmil
  • 15
  • 6
  • part2.cpp:10:21: warning: range-based for loop is a C++11 extension [-Wc++11-extensions] for(int itY : m_vec) ^ part2.cpp:10:17: error: no viable conversion from 'std::__1::vector >' to 'int' for(int itY : m_vec) ^ ~ – Dan Mar 14 '17 at 22:37
  • I seperated those two functions and ran it from terminal g++ – Dan Mar 14 '17 at 22:38
  • Ah yes, for those for loops and auto stuff you'd need at tell the compiler that you're using c++ 11 http://stackoverflow.com/questions/10363646/compiling-c11-with-g – Godmil Mar 14 '17 at 22:50
  • Here is it rewritten as standard C++ 98 http://cpp.sh/4sll5 Though I'd recommend getting into C++11 it's really nice :) – Godmil Mar 14 '17 at 23:04