# Thread: There must be a better way?!

1. ## There must be a better way?!

I began writing this battleship code today and as I'm working on it, I'm finding that I have a function that could be more efficient(understatment). I'm just not sure how. The code compiles and runs, but I have to repeat this 15 times per player.
Code:
```int assignFleet1(int board1 [] [10])

{
int row, col;
int carrier1;

printf("Player 1 Welcome, it is time to place your fleet!\n");
printf("First place your carrier on the board\n");
printf("A carrier takes up 5 spots\n");
printf("Carrier 1st coordinate:");
scanf("%d", &carrier1);

row = (carrier1 / 10);
col = (carrier1 % 10);
printf("row: %d\n", row);//just to make sure I'm getting what I expect
printf("col: %d\n", col);
board1[row] [col] = 5;

return 1;```
any thoughts?
here is what I have come up with: a for loop using the variable car1 for carrier 1st player.
Code:
```for (car1 = 0; car1 < 5; car1++)
{ for (coordinate =0; coordinate < 5; coordinate++)
printf("Enter coordinate %d for carrier\n", car1);
scanf("%d\n", &coordinate);
printf("Coordinate %d is %d \n", car1, coordinate);
row = (coordinate / 10);
col = (coordinate % 10);
board1[row][col] = 5;```
but it doesn't work.

2. You are looping five times to get coordinates for car1, then looping five times to get coordinates for "car2", etc. But there's only one carrier in the traditional game of battleship, so you probably don't need the outside loop for five different carriers.

3. Code:
```	{ for (coordinate =0; coordinate < 5; coordinate++)
printf("Enter coordinate %d for carrier\n", car1);
scanf("%d\n", &coordinate);```

You have the right approach, you just need to consider the detail a little more.

4. Awesome! it worked Salem. I had to change the variable. Here is my updated code
Code:
```int assignFleet1(int board1 [] [10])

{

int car1, coordinate, location;
int row, col;
int carrier1;
printf("Player 1 Welcome, it is time to place your fleet!\n");
printf("First place your carrier on the board\n");
printf("A carrier takes up 5 spots\n");

for (car1 = 0; car1 < 5; car1++)
{for (coordinate =0; coordinate < 5; coordinate++)
{printf("Enter coordinate for carrier\n" );
scanf("%d", &location);
printf("Coordinate  is %d \n",  location);
row = (location / 10);
printf("row: %d\n", row);
col = (location % 10);
printf("col: %d\n", col);
board1[row][col] = 5;
}//for
}

return 1;
}//assignFleet```
works great!
Also Tabstop, thanks for looking over my code. Although there is only 1 carrier, it is 5 places long, so I have to assign it to 5 indexes.

5. Originally Posted by time4f5
Also Tabstop, thanks for looking over my code. Although there is only 1 carrier, it is 5 places long, so I have to assign it to 5 indexes.
So it doesn't bother you that you're typing in 25 indexes instead?

6. Originally Posted by tabstop
So it doesn't bother you that you're typing in 25 indexes instead?
Actually that should be 5 sets of 5 contiguous indexes... One might appease the player somewhat by getting the bow and stern coordinates and filling in the rest automagically...

7. That was what I was going to suggest Tater. Not only does the user have to do less typing, the programmer wins too. Less work for you and easier error checking. Instead of checking 5 coordinates to make sure they're all in a row, and horizontal or vertical, the check for the bow and stern coordinates is easy. For the carrier, which is 5 units long:
• Assume the bow is at (x1,y1) and the stern at (x2,y2)
• If x1 == x2 and |y2 - y1| == 5, then it's good
• If y1 == y2 and |x2 - x1| == 5, then it's good
• Otherwise, ask the user for a new set of coordinates and check again

8. I was going to suggest the user enter a "start" location and a direction

Code:
```"Enter Grid Row location for Carrier:"   (user enters '4')
"Enter Grid Column location for Carrier:" (user enters '8')
"Enter direction for Carrier (U, D, L, R):" (user enters 'L')```
but I think I like Tater's better...