Array placement

Hello, I am trying to do a simple battleship game, however i am trying to get the 0's in place, however they are coming up like:
0
00000
00000
00000
0000

and want them to get to:

00000
00000
00000
00000

I am missing something very simple... but what :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

	const int size = 5;

 	std::array<int, size * size> Array2;

	for (int y = 0; y < size * size; y++)
	{
		Array2[y] = 0;
		if (y % size == 0)
		{
			std::cout << Array2[y] << std::endl;
		}
		else
			std::cout << Array2[y];
	}
Last edited on
0 % size is zero which is causing you to print end of line on first iteration. (edit, thanks to helios below). *I* knew what I meant :(

consider:
1
2
3
4
5
6
7
8
9
10

int main()
{
  for(int i = 0; i < 100; i++)
    if((i+1)%5)
      cout << "0";		
    else
	 cout << "0" << endl;			
}
Last edited on
0 % size = true
Did you mean "(0 % size == 0) == true"?
You can zero-fill your array when creating it: std::array<int, size* size> Array2 { 0 };

Your formatting logic is a bit off, a consequence of arrays being zero-based.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <array>

int main()
{
   const int size = 5;

   std::array<int, size* size> Array2 { 0 };

   for (int y = 0; y < size * size; y++)
   {
      if (0 == (y + 1) % size)
      {
         std::cout << Array2[y] << '\n';
      }
      else
      {
         std::cout << Array2[y] << ' ';
      }
   }
}
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
use a matrix
Creating an actual 2D STL array isn't that hard, and you can access individual elements using "regular" [ ][ ] format:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <array>

template <typename T, size_t N>
std::ostream& operator<<(std::ostream&, std::array<T, N> const&);

template<typename T, size_t R, size_t C>
std::ostream& operator<<(std::ostream&, std::array<std::array<T, C>, R> const&);

int main()
{
   const size_t row_size = 4;
   const size_t col_size = 3;

   // creating a C-style array is easy with an initializer list
   // short myArray[row_size][col_size] = { {-501, 206, 2011}, {989, 101, 206}, {303, 456, 596}, {123, 543, 108} };

   // notice the dimension sizes are reversed when initializing a multidimensional std::array,
   // with an extra set of brackets required for the initializer list
   std::array<std::array<short, col_size>, row_size> my2DArray = { { {-501, 206, 2011},
                                                                     {989, 101, 206},
                                                                     {303, 456, 596},
                                                                     {123, 543, 108} } };

   std::cout << "The 2D STL array contents are:\n";

   for (size_t row = 0; row < row_size; row++)
   {
      for (size_t col = 0; col < col_size; col++)
      {
         std::cout << my2DArray[row][col] << '\t';
      }
      std::cout << '\n';
   }
   std::cout << '\n';

   std::cout << "Let's display that 2D array again:\n";

   // using range-based for loops for output
   for (const auto& r : my2DArray)
   {
      for (const auto& c : r)
      {
         std::cout << c << '\t';
      }
      std::cout << '\n';
   }
   std::cout << '\n';

   // overloading operator<< makes outputting 2D arrays really easy
   std::cout << "Let's try that again:\n";

   std::cout << my2DArray << '\n';

   // properly overloaded operator<< works with 1D arrays as well
   std::array<int, 5> my1DArray = { 34, 56, -21, 5002, 365 };

   std::cout << "Here's a 1D array:\n";

   std::cout << my1DArray << '\n';
}

template <typename T, size_t N>
std::ostream& operator<<(std::ostream& os, std::array<T, N> const& arr)
{
   for (auto const& x : arr)
   {
      os << x << '\t';
   }

   return os;
}

template<typename T, size_t R, size_t C>
std::ostream& operator<<(std::ostream& os, std::array<std::array<T, C>, R> const& arr)
{
   for (const auto& x : arr)
   {
      std::cout << x << '\n';
   }

   return os;
}
The 2D STL array contents are:
-501    206     2011
989     101     206
303     456     596
123     543     108

Let's display that 2D array again:
-501    206     2011
989     101     206
303     456     596
123     543     108

Let's try that again:
-501    206     2011
989     101     206
303     456     596
123     543     108

Here's a 1D array:
34      56      -21     5002    365


I personally prefer using actual multidimensional array containers instead of simulated ones.
Yes. I meant zero mod anything is zero, which is making him put an end of line after the first element. The +1 approach I showed is the simplest way to fix this. Or you can write a book to do it. I have fixed my mistake (words, code was right) above, sorry.

depends on what you are doing whether a 1-d or 2-d container is better. Different tasks are faster or slower with each approach, one size does not fit all. I prefer 1d but that is because the things I do are typically faster in that mode.
Last edited on
Topic archived. No new replies allowed.