1

Ok first of what Im going for is this.There are 12 columns and 15 rows so the line lenght is 12

Enter the line 1 of the puzzle:XTZMQYKCECFH---->12 chars
Enter the line 2 of the puzzle:SHOUTEXOEAPI
Enter the line 3 of the puzzle:XGTLQBELTNFK
'
'
'
Enter the line 15 of the puzzle:

So then ı take the input to another func to print the matrix and should be like this:

X T Z M Q Y K C E C F H
S H O U T E X O E A P I
X G T L Q B E L T N F K
'''
'''
'''

So here is the code but ı dont think it will work with scanf maybe ı should use gets()? I thought of just taking the line, storing it in another array and then tranferring into the matrix. Please try not to use any other library then stdio.h because the challenge is to do it not using prebuild string functions

#include <stdio.h>
#define COLUNM 12
#define ROW 15

void printPuzzle(char matrix[ROW][COLUNM]){
   int i,j;
   for (i=0;i<ROW;i++){
       for(j=0;j<COLUNM;j++){
           printf("\n");
           printf("%3c",matrix[i][j]);        }
   }

}
int main (){
   int i,j;
   j=0;
   char matrix[ROW][COLUNM];
       for(i = 1 ; i < COLUNM+1 ; i++){

           printf("Enter line %d of the puzzle :\n",i);

           scanf("%s",matrix[j][i]);
           j++;
       }
   printPuzzle(matrix[ROW][COLUNM]);
return 0;
}

I have thought of this as well for the main func:

int main (){
 int i,j;
 char line[COLUNM+1];

 char matrix[ROW][COLUNM];
     for(i = 0 ; i < ROW ; i++){

         printf("Enter line %d of the puzzle :\n",i+1);
         scanf("%s",line[i]);
         for(j=0;j<COLUNM;j++){
             matrix[i][j]=line[j];
         }



     }
 printPuzzle(matrix[ROW][COLUNM]);
return 0;
}

But ı keep getting these warnings 30 | printPuzzle(matrix[ROW][COLUNM]); warning: passing argument 1 of ‘printPuzzle’ makes pointer from integer without a cast

.c:5:23: note: expected ‘char (*)[12]’ but argument is of type ‘char’ 5 | void printPuzzle(char matrix[ROW][COLUNM]){

Dont know much about c can someone explain in plain English

hohenpaid
  • 99
  • 9
  • Never use `gets()`, as that function has been officially removed from the ISO C standard for security reasons. Use `fgets()` instead. – Andreas Wenzel Apr 11 '20 at 12:08
  • 1
    Below the question you can see a gray "edit" link, that is the one you can use to update the question with your new code. – tevemadar Apr 11 '20 at 12:16
  • The parameter of the function `printPuzzle` is of type `char[15][12]`, which [decays](https://stackoverflow.com/questions/1461432/what-is-array-decaying) to the type `char(*)[12]` when used as a function parameter. This means that you must pass a pointer to the entire array. However, you are calling the function with the expression `matrix[ROW][COLUNM]`, which the preprocessor changes to `matrix[15][12]`. This means you are passing the value of a single element. This element does not even exist, because you are accessing the array out of bounds. Try passing `matrix` instead. – Andreas Wenzel Apr 11 '20 at 12:39
  • @AndreasWenzel ok I understand . Thank you for explaining in such a simple way – hohenpaid Apr 11 '20 at 13:28

2 Answers2

1

I made some changes. Like we only pass name of array in function and printf("\n"); will be in outer for loop. Try this code

  #include <stdio.h>
  #define COLUNM 12
  #define ROW 15

  void printPuzzle(char matrix[][COLUNM])
  {
   int i,j;
   for (i=0;i<ROW;i++)
   {
     for(j=0;j<COLUNM;j++)
      {

        printf("%3c",matrix[i][j]);
      }
      printf("\n");
   }

 }
 int main (){
  int i,j;
  j=0;
  char matrix[ROW][COLUNM];
  for(i = 0 ; i < COLUNM ; i++)
   {

       printf("Enter line %d of the puzzle :\n",i+1);

       scanf("%s",&matrix[j][i]);
       j++;
   }
  printPuzzle(matrix);
 return 0;
 }
Ankit Mishra
  • 530
  • 8
  • 16
1

You should use fgets to get string from keyboard. Then print all characters of each string.

See, the code for getting the string:

   char matrix[ROW][COLUNM];
   for(i = 0 ; i < ROW ; i++){
        printf("Enter line %d of the puzzle :\n",i);
        fgets(matrix[i],COLUNM + 1, stdin);
        j++;
   }

The function for printing all characters:

void printPuzzle(char matrix[ROW][COLUNM]){
   int i,j;
   for (i=0;i<ROW;i++){
       for(j=0;j<strlen(matrix[i]);j++){
           printf("%3c",matrix[i][j]);
       }
       printf("\n");
   }
}

The complete code:

#include <stdio.h>
#include <string.h>
#define COLUNM 12
#define ROW 15

void printPuzzle(char matrix[ROW][COLUNM]){
   int i,j;
   for (i=0;i<ROW;i++){
       for(j=0;j<strlen(matrix[i]);j++){
           printf("%3c",matrix[i][j]);
       }
       printf("\n");
   }
}

int main (){
   int i,j;
   j=0;
   char matrix[ROW][COLUNM];
   for(i = 0 ; i < ROW ; i++){
        printf("Enter line %d of the puzzle :\n",i);
        fgets(matrix[i],COLUNM + 1, stdin);
        j++;
   }
   printPuzzle(matrix);
return 0;
}
Hitokiri
  • 3,607
  • 1
  • 9
  • 29