Hello I had to write a program (well still have) that would allocate memory in function for storing numbers that you have to input then print a matrix (rows and columns are the same size). Most importantly the program has to be written using pointers, local variables, functions and C 89 standard.
#include <stdio.h>
#include <stdlib.h>
void Matrix_Input(int *m, int ***Matrix);
void Matrix_Output(int m, int **Matrix);
int main()
{
int m;
int **Matrix;
int i;
Matrix_Input(&m, &Matrix);
Matrix_Output(m, Matrix);
for (i = 0; i < m; i++) /*free memory*/
free(*(Matrix+i));
free(Matrix);
return 0;
}
void Matrix_Input(int *m, int ***Matrix)
{
int i, j;
printf("Input number of rows/columns: \n");
scanf("%d", m);
*Matrix = malloc(*m* sizeof(int*)); /*allocate memory*/
for (i = 0; i < *m; i++)
*(*Matrix+i) = malloc(*m* sizeof(int));
printf("Input integers: \n");
for (i = 0; i < *m; i++)
for (j = 0; j < *m; j++)
scanf("%d", &((*Matrix)[i][j]));
}
void Matrix_Output(int m, int **Matrix)
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < m; j++)
printf("%5d", Matrix[i][j]);
printf("\n");
}
}
The program works fine, but I was asked not to use triple pointers here(for input function):
void Matrix_Input(int *m, int ***Matrix)
Teacher told me to use double pointers for input function just like I did for output like this:
void Matrix_Input(int *m, int **Matrix)
And this is where everything goes wrong since I only know how to allocate with triple pointers. I have to leave input as a separate function, can't put it in main.
Could someone help me out? Please.