is there not an easier way

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
#include <cstdio>
#include <cstdlib>

#include <iostream>
#include <string>
using namespace std;

int main ()
{
    int i,j;
    int star[10][10];
    string column[10] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
    char place[10] = {'o','o','o','o','o','o','o','o','o','o',};
    char *spot = &place[10];

    for(i = 0; i < 9; i++)
    {
         for(j = 0; j < 9; j++)
         {
            star[i][j] = 0;
         }
    }
cout << "  1 2 3 4 5 6 5 7 8 9 10" <<endl;

    for(i = 0; i < 9; i++)
    {

        cout << column[i] << " " << place[i] << " "  << place[i]  << " " << place[i] << " "  << place[i]
         << " " << place[i] << " "  << place[i]  << " " << place[i]  << " " << place[i]  << " "
         << place[i]  << " " << place[i] <<" " << place[i]<<endl;
    }
}


isnt there an easier way to do lines 28 through 30

and why does it not display J ?
Last edited on
A nested loop perhaps for the place[i]-s
1
2
3
4
5
6
7
8
9
for(int i = 0; i < 9; i++)
{
cout << column[i] << " ";
for(int j = 0; j < 11; j++)
{
cout << place[i] << " ";
}
cout << endl;
}


(not written in IDE, might contain errors, though most probably not)
in the for loop it only goes up to 8 because 9 is not less than 9
It should be
1
2
3
4
5
6
7
    for(i = 0; i <= 9; i++)
    {

        cout << column[i] << " " << place[i] << " "  << place[i]  << " " << place[i] << " "  << place[i]
        << " " << place[i] << " "  << place[i]  << " " << place[i]  << " " << place[i]  << " "
        << place[i]  << " " << place[i] <<" " << place[i]<<endl;
    }

Not
1
2
3
4
5
6
7
    for(i = 0; i < 9; i++)
    {

        cout << column[i] << " " << place[i] << " "  << place[i]  << " " << place[i] << " "  << place[i]
         << " " << place[i] << " "  << place[i]  << " " << place[i]  << " " << place[i]  << " "
         << place[i]  << " " << place[i] <<" " << place[i]<<endl;
    }
Last edited on
#1 TheMassiveChipmunk is right about the end condition, but I favour the alternative fix. That is keeping the < and then upping the end condition to 10.

And array of n runs from 0 - (n - 1); so for(i = 0; i < n; ++n) is the laziest/clearest form (to my mind).

for(i = 0; i < 10; i++)

But it would be better to define constants.

1
2
const int row_count = 10;
const int col_count = 10;


And then use them for the array declaration and loops (note: arrays can only be declared with consts; loops don't really care!)

#2 Is there a reason "column" is a string array whereas "place" is a char array?

Given the strings in the "column" array are single letters, you could use a char array there, too.

#3 "spot" may well blow up if you try to use it!

char *spot = &place[10];

You declared the variable "place" to have 10 elements, which in C++ run 0 .. 9. So the 10th element is just off the end of your array. If you mean to get a pointer to the first element of this array, then replace the 10 by 0.

#4 All elements in "place" have a 'o' in them. Is this right?

#5 Where does "star" fit in?

#6 GisleAune's loop idea is cool.

Look for other loops!

Andy
Last edited on
ok GisleAune, i understand everything in urs except for the inner loop, the condition of j < 11 (that doesnt make sense) it works and it tried to decrease it to 9 and 10, and it doesnt give enough lines

why does it have to be 11, i would think it should go 1 line over, but yet does not
It should be < 10

And the reason it doesn't hit problems, is that the wrong index is used in the inner loop.

Andy

1
2
3
4
5
6
7
8
9
10
11
for(int i = 0; i < 9; i++)
{
    cout << column[i] << " ";

    for(int j = 0; j < 11; j++)
    {
        cout << place[i] << " ";
    }

    cout << endl;
}


Should be


1
2
3
4
5
6
7
8
9
10
11
for(int i = 0; i < 10; i++)
{
    cout << column[i] << " ";

    for(int j = 0; j < 10; j++)
    {
        cout << place[j] << " ";
    }

    cout << endl;
}
Last edited on
Remember you can increment through the letters.

1
2
3
4
5
6
7
8
	cout << "  1 2 3 4 5 6 7 8 9 10";
	for(char bColumnHeading = 'A';bColumnHeading++;bColumnHeading<='Z'){
		cout << bColumnHeading << " ";
		for(char unsigned bItem = 0;bItem++;bItem<=10){
			cout << "o ";
		}
		cout << endl;
	}
Last edited on
but using < 10 has a missing 'place' of rows
Where's it missing?

Andy
.. 1 2 3 4 5 6 7 8 9 10
A o o o o o o o o o
B o o o o o o o o o
C o o o o o o o o o
D o o o o o o o o o
E o o o o o o o o o
F o o o o o o o o o
H o o o o o o o o o
I o o o o o o o o o
J o o o o o o o o o

under the 10

from A through J
Last edited on
That looks like that o/p from

1
2
    for(i = 0; i < 9; i++)
    {


Are you sure your code is using < 10 ???

Andy
Last edited on
1
2
3
4
5
6
7
8
9
for(int i = 0; i < 9; i++)
{
cout << column[i] << " ";
for(int j = 0; j < 11; j++)
{
cout << place[i] << " ";
}
cout << endl;
}


this works properly, if i change the 11 to 10, im missing the column under the 10 in the display, if i change the 11 to a 9, im missing 2 columns under 10 and 9

but it doesnt make sense, why 11 would work, you'd think the 11 would give too many lines

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
#include <cstdio>
#include <cstdlib>

#include <iostream>
#include <string>
using namespace std;

int main ()
{
    int i,j;
    string column[10] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
    char place[10] = {'o','o','o','o','o','o','o','o','o','o',};

    cout <<"first display"<<endl<<endl;
cout << "  1 2 3 4 5 6 5 7 8 9 10" <<endl;
    for(int i = 0; i <= 9; i++)
    {
    cout << column[i] << " ";
        for(int j = 0; j < 11; j++)
            {
            cout << place[i] << " ";
            }
    cout << endl;
    }
    cout <<endl<<endl;
    cout <<"second display"<<endl<<endl;
cout << "  1 2 3 4 5 6 5 7 8 9 10" <<endl;
    for(int i = 0; i < 10; i++)
{
    cout << column[i] << " ";

    for(int j = 0; j < 10; j++)
    {
        cout << place[j] << " ";
    }

    cout << endl;
}
}
Last edited on
Careful!

#1 In your first fragment, you've reintroduced an error in the inner loop (using i as the index for place rather than j)

#2 In your second fragment, this row is not quite right (line 27)

cout << " 1 2 3 4 5 6 5 7 8 9 10" <<endl;

Does this fix the 10/11 confusion?

Andy
Last edited on
@metulburr the reason GisleAune used 11 as a terminating condition is because in your original code, you printed place[i] 11 times.

1
2
3
4
5
6
7
for(i = 0; i < 9; i++)
{

        cout << column[i] << " " << place[i] << " "  << place[i]  << " " << place[i] << " "  << place[i]
         << " " << place[i] << " "  << place[i]  << " " << place[i]  << " " << place[i]  << " "
         << place[i]  << " " << place[i] <<" " << place[i]<<endl;
}
omg...im retarted. i didn't even see that extra 5 in there, then i just kept copy and pasting that same line of code

yea, my original intent was the display for battleship. a-j down in column and 1-10 in rows and a 'o' for each spot

1
2
3
4
5
6
7
8
9
10
11
12
13
    cout <<"second display"<<endl<<endl;
cout << "  1 2 3 4 5 6 7 8 9 10" <<endl;
    for(int i = 0; i < 10; i++)
{
    cout << column[i] << " ";

    for(int j = 0; j < 10; j++)
    {
        cout << place[j] << " ";
    }

    cout << endl;
}


this stil doesnt make any sense to me cuz:



the inner and outer loops dontt make sense cuz it running 11 loops but should only need 10 (0-9)(theoretically) so i change the loop condition to 9 and it is shy a loop?



i dont understand why the 9 is shy of the J loop? doesnt it run a full 10 loops?
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
#include <cstdio>
#include <cstdlib>

#include <iostream>
#include <string>
using namespace std;

int main ()
{
    int i,j;
    string column[10] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
    char place[10] = {'o','o','o','o','o','o','o','o','o','o',};


cout << "  1 2 3 4 5 6 7 8 9 10" <<endl;
    for(int i = 0; i < 9; i++)
{
    cout << column[i] << " ";

    for(int j = 0; j < 10; j++)
    {
        cout << place[j] << " ";
    }

    cout << endl;
}



i dont understand why the 9 is shy the 10 loop? shouldn't it also run the full 10 loops?
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
#include <cstdio>
#include <cstdlib>

#include <iostream>
#include <string>
using namespace std;

int main ()
{
    int i,j;
    string column[10] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
    char place[10] = {'o','o','o','o','o','o','o','o','o','o',};


cout << "  1 2 3 4 5 6 7 8 9 10" <<endl;
    for(int i = 0; i < 10; i++)
{
    cout << column[i] << " ";

    for(int j = 0; j < 9; j++)
    {
        cout << place[j] << " ";
    }

    cout << endl;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
cout <<"second display"<<endl<<endl;
cout << "  1 2 3 4 5 6 7 8 9 10" <<endl;
    for(int i = 0; i < 10; i++)
{
    cout << column[i] << " ";

    for(int j = 0; j < 10; j++)
    {
        cout << place[j] << " ";
    }

    cout << endl;
}


this stil doesnt make any sense to me cuz:



the inner and outer loops dontt make sense cuz it running 11 loops but should only need 10 (0-9)(theoretically) so i change the loop condition to 9 and it is shy a loop?

What do you mean? This prints correctly for me.

In each iteration of the i loop (there are 10 iterations), it runs the j loop 10 times. 11 loops are not running. There are 11 print statements per iteration of the i loop (the printing of the column then the 10 prints in the j loop). Maybe that's the source of the confusion?
Topic archived. No new replies allowed.