I tried to read and print a matrix using an external function (from another c file), the thing is that I want to read the matrix dimensions in the function and store them in the main function, how can I do this?
Do I need to return an array with the m and n dimensions of the matrix or can I access the variables that I created in main and change their value within the external function? (I prefer if someone would explain the second) I don't actually know how to use pointers and stuff. Sorry for my English, I'm not a native speaker, also thanks for your response
The second and the third functions are in an external function.c file
int main(){
//----------variables:
int num_of_rows, num_of_columns;
int matrix[10][10];
//-------------------->END
//---------->Read a matrix from user
read_matrix(num_of_rows, num_of_columns, matrix);
//-------------------->END
//---------->Print a matrix
print_matrix(num_of_rows, num_of_columns, matrix);
//-------------------->END
//----------START ==> exit_program:
printf("\n Press any key to exit the program: ");
_getch();
return 0;
//-------------------->END
}//END
//START ==> read a matrix from user
void read_matrix(int num_of_rows, int num_of_columns, int matrix[10][10]){
//---------->variables
int i,j;
//-------------------->END
//---------->reading the matrix dimensions
printf("\nPlease specify the number of rows:");
scanf("%d", &num_of_rows);
printf("\nPlease specify the number of columns: ");
scanf("%d", &num_of_columns);
//-------------------->END
//---------->reading the matrix elements
printf("\nPlease introduce the matrix elements below:\n");
for(i=0; i<num_of_rows; i ){
for(j=0; j<num_of_columns; j ){
printf("matrix[%d][%d]= ", i, j);
scanf("%d", &matrix[i][j]);
}
}
//-------------------->END
}//END
//START ==> Print a matrix
void print_matrix(int num_of_rows, int num_of_columns, int matrix[10][10]){
//---------->variables
int i,j;
//-------------------->END
//---------->
for(i=0; i<num_of_rows; i ){
for(j=0; j<num_of_columns; j ){
printf("matrix[%d][%d]= %d", i, j, matrix[i][j]);
}
}
//-------------------->END
}//END
CodePudding user response:
Parameters are passed by value in C.
In read_matrix, the num_of_rows parameter is a local variable. Even if you modify it, the caller won't see anything change. Same for num_of_columns.
You want this:
void read_matrix(int *num_of_rows, int *num_of_columns, int matrix[10][10]) {
// ^ add * ^ add *
...
scanf("%d", num_of_rows); // << remove the &
printf("\nPlease specify the number of columns: ");
scanf("%d", num_of_columns); // << remove the &
...
for (i = 0; i < *num_of_rows; i ) {
// ^add *
for (j = 0; j < *num_of_columns; j ) {
// ^add *
and in main:
read_matrix(&num_of_rows, &num_of_columns, matrix);
// ^ ^ add the &s
This is basic knowledge that is covered in your C learning material. Most likely in the chapter dealing with pointers and the one dealing with function calls.
CodePudding user response:
Here's the catch. I tested your code and it works nicely. The problem is, what you want to achieve is only possible by using dynamic memory allocation. Take a look at the malloc and free functions.
You can read more about it on:
In C language you are responsible for allocation a space in memory for variable length data structures. Pointers just store an address to a specific memory space allocated to the desired data type.
e.g.:
int n, *p;
p = (int*) malloc(n * sizeof(int));
In this example, you are just giving p an address to the first integer from n integers you just allocated.
p will work just like a vector when using for loops because behind the scenes, it's exactly what happens when you traverse your fixed length vectors and matrices, but this time you were in control of it's length in runtime.
CodePudding user response:
#include<stdio.h>
void main()
{
int x[3][3], p, q, max;
printf("Enter the elements of matrix: \n");
for(p=0;p<3;p )
{
for(q=0;q<3;q )
scanf("%d", &x[p][q]);
}
max=x[0][0];
printf("The matrix is as follows: \n");
for(p=0;p<3;p )
{
for(q=0;q<=3;q )
scanf(" %d", &x[p][q]);
}
for(p=0;p<3;p )
{
for(q=0;q<3;q )
{
if(x[p][q]>max)
max=x[p][q];
}
printf("\n");
}
printf("Maximum number in the matrix is: %d", max);
}
Output:
Enter the elements of matrix:
23 65 12
12 23 56
12 10 32
The matrix is as follows:
23 65 12
12 23 56
12 10 32
Maximum number in the matrix is: 65
Explore more matrix programs.
