fork download
  1. #include <iostream>
  2. #include"stdio.h"
  3. using namespace std;
  4.  
  5. int FleetSunk(int a[25][25]);
  6.  
  7. void HIT(int a[][25],int,int);
  8.  
  9.  
  10. int main()
  11. { int i,j,a[25][25],x,y;
  12. FILE *file1 = fopen("2d.txt", "r");
  13.  
  14. for(i=0;i<25;i++){
  15. for(j=0;j<25;j++){ /* create a txt file 2d in current folder consisting of matrix as shown in question otherwise take direct input into 2 array by 2 for loop
  16.   */
  17. fscanf(file1,"%c\t", &a[i][j]);
  18. }
  19. }
  20. j=FleetSunk(a); //checking if some part of ship is present
  21. while(j==0)
  22. {
  23. scanf("%d%d",&x,&y);
  24. HIT(a,x,y);
  25. FleetSunk(a);
  26. }
  27. printf("The fleet was destroyed");
  28. return 0;
  29. }
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44. void HIT(int a[][25],int x,int y)
  45. {
  46. if(a[x][y]=='#') //if part of ship is present at x,y then printing hit
  47. {
  48. printf("HIT");
  49. a[x][y]='H';
  50. }
  51.  
  52. if(a[x][y]=='H')
  53. {
  54. printf("HIT AGAIN");
  55. }
  56. }
  57.  
  58. int FleetSunk(int a[][25])
  59. {
  60. for(int i=0;i<25;i++) //if a single # present in a ocean then all fleet is not destroyed
  61. {
  62. for(int j=0;j<25;j++)
  63. {
  64. if(a[i][j]=='#')
  65. {
  66. return 0;
  67. }
  68.  
  69. }
  70. }
  71. }
Runtime error #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
Standard output is empty