fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. int main()
  5. {
  6. const int Width = 7;
  7. const int Height = 5;
  8.  
  9. std::vector<char> myArray;
  10. myArray.resize(Width * Height, 'X');
  11.  
  12. //Indexing as a 1D array:
  13. for(int i = 0; i < (Width * Height); ++i)
  14. {
  15. myArray[i] = (i % 3)? 'O':'X';
  16. }
  17.  
  18. //And also indexing as if it were a 2D array:
  19. for(int y = 0; y < Height; ++y)
  20. {
  21. for(int x = 0; x < Width; ++x)
  22. {
  23. int index = (y * Width) + x;
  24. std::cout << myArray[index] << " ";
  25. }
  26.  
  27. std::cout << std::endl;
  28. }
  29.  
  30. return 0;
  31. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
X O O X O O X 
O O X O O X O 
O X O O X O O 
X O O X O O X 
O O X O O X O