I have an issue passing a matrix to a function in C. There is the function I want to create:
void ins (int *matrix, int row, int column);
but I noticed that in contrast to the vectors, matrix give me an error. How can I pass my matrix to a function so?
EDIT --> there is the code:
// Matrix
#include <stdio.h>
#define SIZE 100
void ins (int *matrix, int row, int column);
void print (int *matrix, int row, int column);
int main ()
{
int mat[SIZE][SIZE];
int row, col;
printf("Input rows: ");
scanf ("%d", &row);
printf("Input columns: ");
scanf ("%d", &col);
printf ("Input data: \n");
ins(mat, row, col);
printf ("You entered: ");
print(mat, row, col);
return 0;
}
void ins (int *matrix, int row, int column);
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
printf ("Row %d column %d: ", i+1, j+1);
scanf ("%d", &matrix[i][j]);
}
}
}
void print (int *matrix, int row, int column)
{
int i;
int j;
for(i=0; i<row; i++)
{
for(j=0; j<column; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
You need to pass a pointer with as much levels of indirection (*) as the number of dimensions of your matrix.
For example, if your matrix is 2D (e.g. 10 by 100), then:
void ins (int **matrix, int row, int column);
If you have a fixed dimension (e.g. 100), you can also do:
void ins (int (*matrix)[100], int row, int column);
or in your case:
void ins (int (*matrix)[SIZE], int row, int column);
If both your dimensions are fixed:
void ins (int matrix[10][100], int row, int column);
or in your case:
void ins (int matrix[SIZE][SIZE], int row, int column);
If you have a modern C compiler you can do the following for 2D matrices of any sizes
void ins (size_t rows, size_t columns, int matrix[rows][columns]);
Important is that the sizes come before the matrix, such that they are known, there.
Inside your function you then can access the elements easily as matrix[i][j] and the compiler is doing all the index calculations for you.
it would also possible to leave the first dimension empty, the same as (*matrix):
void ins (int matrix[][100], int row, int column);
Much better way to use malloc function and create dynamically allocated array and do whatever you want to do using 2d array:
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
void fun(int **arr,int m,int n)
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", &arr[i][j]);
}
}
}
int main()
{
int i,j,m,n;
printf("enter order of matrix(m*n)");
scanf("%d*%d",&m,&n);
int **a=(int **)malloc(m*sizeof(int));
for(i=0;i<n;i++)
a[i]=(int *)malloc(n*sizeof(int));
fun(a,m,n);
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
return 0;
}
output:
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
void fun(int **arr,int m,int n)
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", &arr[i][j]);
}
}
}
int main()
{
int i,j,m,n;
printf("enter order of matrix(m*n)");
scanf("%d*%d",&m,&n);
int **a=(int **)malloc(m*sizeof(int));
for(i=0;i<n;i++)
a[i]=(int *)malloc(n*sizeof(int));
fun(a,m,n);
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
return 0;
}
You can try this as well:
void inputmat(int r,int c,int arr[r * sizeof(int)][c * sizeof(int)])
Related
I am trying to create a program to find the transpose of a matrix my dynamic memory allocation. However, while entering the elements of the matrix I can't input more than one element, its only taking the a[0][0] as the input and the program is ending after that.
#include <stdio.h>
#include <stdlib.h>
void createMatrix(int **, int, int);
void inputElements(int **, int, int);
void transpose(int **, int **, int, int);
void display(int **, int, int);
void main()
{
int **matrix, **trans, rows, cols;
printf("\nEnter number of rows in the matrix: ");
scanf("%d", &rows);
printf("\nEnter number of columns in the matrix: ");
scanf("%d", &cols);
createMatrix(matrix, rows, cols);
createMatrix(trans, cols, rows);
inputElements(matrix, rows, cols);
transpose(matrix, trans, rows, cols);
printf("\nMATRIX:\n");
display(matrix, rows, cols);
printf("\nTRANSPOSE OF THE MATRIX:\n");
display(trans, rows, cols);
}
void createMatrix(int **a, int r, int c) //for allocating memory for the matrix
{
int i, j;
a = (int **)malloc(sizeof(int *) * r);
for(i = 0; i < r; i++)
a[i] = (int *)malloc(sizeof(int) * c);
}
void inputElements(int **a, int r, int c) //for entering matrix elements
{
int i, j, t;
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
printf("\nEnter matrix element[%d][%d]: ", i + 1, j + 1);
fflush(stdin);
getchar();
scanf("%d", &(a[i][j]));
}
}
}
void transpose(int **a, int **t, int r, int c) //for finding out the transpose of the matrix
{
int i, j;
for (i = 0; i < c; i++)
{
for (j = 0; j < r; j++)
t[i][j] = a[j][i];
}
}
void display(int **a, int r, int c) //for displaying the matrix
{
int i, j;
for (i = 0; i < r; i++)
{
printf("\n");
for (j = 0; j < c; j++)
printf("\t%d", a[i][j]);
}
}
There are many posts about scanf in loops I've seen, but I wasn't able to connect my issue with any of them.
I am trying to type in a matrix and then print it, using functions.
The program crashes when it gets to the read_mat function. What am I getting wrong?
#include <stdio.h>
#include <stdlib.h>
int** insert_mat(int **mat, int r, int c);
int** read_mat(int** mat, int r, int c)
{
mat = (int**)calloc(r,sizeof(int*));
for (int i=0; i<r; i++)
{
mat[i]=(int*)calloc(c,sizeof(int));
}
insert_mat(mat, r, c);
return mat;
}
int** insert_mat(int **mat, int r, int c)
{
for (int i=0; i<r; i++)
{
for(int j=0; j<c; j++)
{
printf("\nmat[%d][%d] = ",i , j);
scanf("%d", &mat[i][j]);
}
}
return mat;
}
void print_mat(int **mat, int r, int c)
{
printf("\n");
for (int i=0; i<r; i++)
{
for(int j=0; j<c; j++)
{
printf("%d ",mat[i][j]);
}
printf("\n");
}
}
int main()
{
int **mat;
int r1 =2;
int c1 = 2;
read_mat(mat, r1, c1);
print_mat(mat, r1, c1);
return 0;
}
I also tried printing elements of mat in main (ex: printf("%d", mat[0][0]);) and it is also not working. So maybe the error is when I insert the elements? But I am not sure what I do wrong.
I'm trying to scan values into a matrix that is passed by reference to a function and that's not compiled.
What's wrong?
I think the problem is in line of scanf but I don't know how to fix it.
#include <stdio.h>
#include <stdlib.h>
int** initMatrix(int lines, int columns) {
int i;
int** matrix;
matrix = (int**) calloc(lines, sizeof(int*));
for (i = 0; i < lines; i++) {
matrix[i] = (int*) calloc(columns, sizeof(int));
}
return matrix;
}
void fillMatrixValues(int*** matrixA, int lines, int columns) {
int i, j;
for (i = 0; i < lines; i++) {
for (j = 0; j < columns; j++) {
scanf("%d", matrixA[i][j]);
}
}
}
void printMatrix(int** matrix, int lines, int columns) {
int i, j;
for (i = 0; i < lines; i++) {
for (j = 0; j < columns; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}
void main() {
int** matrixA;
int lines = 2, columns = 2;
matrixA = initMatrix(lines, columns);
fillMatrixValues(&matrixA, lines, columns);
printMatrix(matrixA, lines, columns);
}
void main -> undefined behaviour
Use
int main(void)
or
int main(int argc, char ** argv)
You should check return value of scanf
if (scanf("%d", matrixA[i][j]) != 1)
{
// failed
}
Next problem
void fillMatrixValues(int ***matrixA, int lines, int columns)
fillMatrixValues(&matrixA, lines, columns)
to
void fillMatrixValues(int **matrixA, int lines, int columns)
fillMatrixValues(matrixA, lines, columns)
I feel like I've attempted every combination I know of to get this to work and can't figure it out. How can I scanf() into an int** passed as a pointer to a function? I tried searching but couldn't find this, if it's a duplicate please let me know and I'll delete. It begins to run and after entering a few values it segfaults.
Here's my code, I think it's messing up on the scanf() line of the setMatrix() function:
#include <stdio.h>
#include <stdlib.h>
// create zero initialized matrix
int** callocMatrix(int rmax, int colmax) {
int **mat = calloc(rmax, sizeof(int*));
for(int i = 0; i < rmax; i++) mat[i] = calloc(colmax, sizeof(int));
return mat;
}
// fill matrix
void setMatrix(int ***mat, int r, int c){
printf("Insert the elements of your matrix:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
printf("Insert element [%d][%d]: ", i, j);
scanf("%d", mat[i][j]); // problem here??
printf("matrix[%d][%d]: %d\n", i, j, (*mat)[i][j]);
}
}
return;
}
// print matrix
void printMatrix(int ***mat, int r, int c){
for (int i=0; i<r;i++){
for (int j=0; j<c;j++) {
printf("%d ", (*mat)[i][j]);
}
printf("\n");
}
}
int main(int argc, char *argv[]) {
int r = 3, c = 3;
int **mat = callocMatrix(r, c);
setMatrix(&mat, r, c);
printMatrix(&mat, r, c);
}
There is no need to use triple pointer ***. Passing two-dimensional array will work as is. Here is the code:
#include <stdio.h>
#include <stdlib.h>
// create zero initialized matrix
int** callocMatrix(int rmax, int colmax) {
int **mat = calloc(rmax, sizeof(int*));
for(int i = 0; i < rmax; i++) mat[i] = calloc(colmax, sizeof(int));
return mat;
}
// fill matrix
void setMatrix(int **mat, int r, int c){
printf("Insert the elements of your matrix:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
printf("Insert element [%d][%d]: ", i, j);
scanf("%d", &mat[i][j]); // no problem here
printf("matrix[%d][%d]: %d\n", i, j, mat[i][j]);
}
}
}
// print matrix
void printMatrix(int **mat, int r, int c){
for (int i=0; i<r;i++){
for (int j=0; j<c;j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
}
int main(int argc, char *argv[]) {
int r = 3, c = 3;
int **mat = callocMatrix(r, c);
setMatrix(mat, r, c);
printMatrix(mat, r, c);
}
Should be:
scanf("%d", &(*mat)[i][j]);
You're passing a pointer to you matrix object, so you need to dereference it (with *) just as you do with printf. scanf then needs the address of the element to write into, so you need the &
I am writing a Theater program using a dynamic 2D array. To store a booking details I use structure.
When I try to initialize one of the variables inside each index of 2D array, I receive unhandled exception error.
Function with error:
void initializing_tickets(ticket **arrayPtr, int row, int col){
int i, j, counter;
for(i = 0; i < row; i++)
{
for(j = 0; j < col; j++)
{
(*(arrayPtr + i) + j) -> id = 0; // debugger explains that expression cannot be evaluated
printf("%d ", (*(arrayPtr + i) + j) -> id);
}
printf("\n");
}
} //end of initializing_tickets()
My program so far:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
/* Structures */
typedef struct Theater
{
int id;
int status;
char name[20];
char phone[15];
} ticket;
/* Global variables */
ticket **array2D; // pointer to a 2D array
/* Constructors */
void create_Theater(int *row, int* col);
void test();
void loop_array(ticket **arrayPtr, int row, int col);
void initializing_tickets(ticket **arrayPtr, int row, int col);
int main(void)
{
int row = 0, col = 0;
create_Theater(&row,&col);
initializing_tickets(array2D, row, col);
//loop_array(array2D, row, col);
//test();
printf("\n\n");
system("pause");
return(0);
}
void create_Theater(int *row, int* col)
{
int r = 0, c = 0;
int i;
assert(row);
assert(col);
*row = r;
*col = c;
printf("Please enter the row dimensions of the Theater\n");
scanf("%d", row);
printf("Please enter the column dimensions of the Theater\n");
scanf("%d", col);
array2D = (ticket**)malloc(r*sizeof(ticket*));
for (i = 0;i<r;i++)
{
array2D[i] = (ticket*)malloc(c*sizeof(ticket));
}
} // end of create_Theater()
void initializing_tickets(ticket **arrayPtr, int row, int col){
int i, j, counter;
for(i = 0; i < row; i++)
{
for(j = 0; j < col; j++)
{
(*(arrayPtr + i) + j) -> id = 0; // debugger explains that expression cannot be evaluated
printf("%d ", (*(arrayPtr + i) + j) -> id);
}
printf("\n");
}
} //end of initializing_tickets()
void loop_array(ticket **arrayPtr, int row, int col)
{
int i, j;
for(i = 0; i < row; i++)
{
printf("Row is ok");
for(j = 0; j < col; j++)
{
printf("Col is ok");
}
}
}
I think array is not allocated properly to a memory, but I can't find a mistake.
Thank you for attention!
Change your function to:
void create_Theater(int* row, int* col){
int i;
assert(row);
assert(col);
printf("Please enter the row dimensions of the Theater\n");
scanf("%d", row);
printf("Please enter the column dimensions of the Theater\n");
scanf("%d", col);
array2D = (ticket**)malloc((*row)*sizeof(ticket*));
for (i = 0;i<(*row);i++)
{
array2D[i] = (ticket*)malloc((*col)*sizeof(ticket));
}
} // end of create_Theater()
It works.