0

thanks for the help, sorry for my bad english I'm trying to use a matrix with functions to input from user or fill the matrix with random numbers and then print it, but i can print only the last row, i think the problem is related with pointers, please help me The user choose if he wants to fill the matrix or use random numbers to do it, This is the code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 25
#define MIN 10

int i, j, y, x, n;
int mat [MAX] [MAX];
int main ()
{   
  int x, y, i, j, user, n;
  printf ("Goodmorning, insert matrix dims (y,x): ");
  scanf ("%d %d",&y,&x);
  for (i=0;i<y;i++)
   for (j=0;j<x;j++)
    mat [i] [j] = 0;
  printf ("\nInsert '0' for random, insert '1' for manual: ");
  scanf ("%d", &user);
  if (!user)
  rand_matrix (mat [x], x, y);
  if (user)
  input_matrix (mat [x], x, y);
  stampa_matrix (mat  [x], x, y);
  return 0;
}

  rand_matrix (int matrix  [y] [x] , int b, int a)
 {
 srand(time(NULL));
 for (i=0;i<a;i++)
 for (j=0;j<b;j++)
  {
   n = rand () % MAX + MIN;
   matrix [i] [j] = n;
  }
 }

input_matrix (int matrix [y] [x], int b, int a)
{
for (i=0;i<a;i++)
 for (j=0;j<b;j++)
 scanf ("%d", &matrix[i] [j]);
}

stampa_matrix (int matrix [y] [x] , int b, int a)
{
 for (i=0;i<a;i++) 
 {
  printf ("\n");
  for (j=0;j<b;j++)
  printf ("%3d ", matrix [i] [j] );
 }
}
FaS
  • 1
  • 1
    `rand_matrix (mat [x], x, y);` --> `rand_matrix (mat, x, y);` – BLUEPIXY Apr 16 '17 at 19:40
  • 1
    `rand_matrix (int matrix [y] [x] , int b, int a)` --> `rand_matrix (int matrix [y] [MAX] , int b, int a)` – BLUEPIXY Apr 16 '17 at 19:45
  • when calling any of the `scanf()` family of functions, always check the returned value (not the parameter values) to assure the operation was successful. – user3629249 Apr 18 '17 at 16:41
  • the posted code fails to check the `x` and `y` values to assure they are in the range 1...MAX, so the user could enter a value that is greater than `MAX` for either of these value, resulting in (an eventual) buffer overflow. Such overflow is undefined behavior and can lead to a seg fault event. – user3629249 Apr 18 '17 at 16:43

2 Answers2

1

There are various issues I think I find with your code. I shall elaborate below:

1) You are defining the functions (rand_matrix, input_matrix, stampa_matrix) post the function calls in main(). This might compile fine in gcc but fails in VC. Function prototyping is also a good idea.

2) Return type is not present across all three functions (implicit int is deprecated already; please refer this - C function calls: Understanding the "implicit int" rule)

3) As pointed by mab, you need to use double pointers in declaration as follows: void rand_matrix(int matrix[][MAX], int b, int a) or void rand_matrix(int matrix[MAX][MAX], int b, int a)

4) Function call should be like following: rand_matrix(mat, x, y); and not rand_matrix (mat [x], x, y); In the first call, you are passing the address of the row 0 whereas the second passes the address of x'th row wherein x is row itself causing access bounds violation (remember array elements starts from index 0)

Finally, please follow code indentation for proper understanding. Good luck!

Community
  • 1
  • 1
vivekn
  • 35
  • 6
-2

In the code, mat is declared as pointer-to pointer-to integer by int mat[MAX][MAX];. So, each suffix of [MAX] in the declaration adds another pointer-to in the underlying C data type.

When referencing the object, e.g. by mat[i][j] = 3, a similar thing happens. Each suffix [i] resolves one pointer-to of the underlying C data type. When all layers of pointer-to are removed, one can actually access the int at that storage location.

The argument to the functions input_matrix() and stampa_matrix() in the code is mat[x]. That would be a pointer-to int data type.

However, two layers of pointer-to are removed in the functions, by matrix[i][j]. So there is a mismatch between declared data type of mat and its actual use in the functions input_matrix() and stampa_matrix().

To resolve the problem, give the mat argument like this:

/* ... */
if(user)
    input_matrix(mat, x, y);

stampa_matrix(mat, x, y);
/* ... */

So now you pass a pointer-to pointer-to int into the functions. Within the functions, you correcly resolve pointer-to pointer-to by matrix[i][j], and access the actual int.

In the declarations of the functions, the x and y in the square brackets are not necessary. Also, it is always good to add the return data type (here: void) in front of the function name.

void input_matrix (int matrix[][], int b, int a)
{
    /* ... */
}

void stampa_matrix (int matrix[][] , int b, int a)
{
    /* ... */
}

In the list of function arguments, the important thing is only their data type. Again int matrix[][] means pointer-to pointer-to int in this context.

There is another thing that strikes me in the code:

Indentation is not consistent, and partially only 1 space. I suggest to use 4 space or 1 tab for clear visibility. If this causes problems with the remaining length of the line, it is an indication that the code should be separated into different files or modules.

It is good practice to adopt a consistent style early on. Then subsequent code maintenance is much easier.

mab
  • 1
  • 3
  • all this text and still not pointing out the real problem: two-dimensional arrays passed as parameter must have their size specified in the prototypes, as BLUEPIXY commented. – Jean-François Fabre Apr 17 '17 at 06:04