#include<stdio.h>
int findMax(int **a,int r,int c);
int main()
{
int a[10][10],i,j,max,r,c;
printf("Enter the number of rows in the matrix\n");
scanf("%d",&r);
printf("Enter the number of columns in the matrix\n");
scanf("%d",&c);
printf("Enter the elements in the matrix\n");
for(i=1;i<=r;i++)
{ for(j=1;j<=c;j++)
scanf("%d",&[i][j]);
}
printf("The matrix is\n");
for(i=1;i<=r;i++)
{ for(j=1;j<=c;j++)
scanf("%d",&a[i][j]);
}printf("\n");}
max=findMax((int **)a,r,c);
printf("The maximum elements in the matrix is %d\n",max);
return 0;
}
int findMax(int **a,int r,int c)
{
int t,i,j;
t=a[1][1];
for(i=1;i<r;i++)
{ for(j=1;j<c;j++)
{ if(a[i][j]>t)
t=a[i][j];
}
}
return (t);
}
Here I attached my coding, I need to find the maximum element present in the matrix using function, I am doing the coding, calling function is not executed, I don't know why, Help me to figure it out.
Change
int findMax(int **a,int r,int c)
to
int findMax(int (*a)[10],int r,int c)
And also,
for(i=1;i<r;i++)
{
for(j=1;j<c;j++)
{
if(a[i][j]>t)
t=a[i][j];
}
}
to
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
if(a[i][j]>t)
t=a[i][j];
}
}
EDIT:
I think, your code should be like this:
#include<stdio.h>
int findMax(int (*a)[10],int r,int c);
int main()
{
int a[10][10],i,j,mx,r,c;
printf("Enter the number of rows in the matrix\n");
scanf("%d",&r);
printf("Enter the number of columns in the matrix\n");
scanf("%d",&c);
printf("Enter the elements in the matrix\n");
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
scanf("%d",&a[i][j]);
}
printf("The matrix is\n");
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
printf("%d ",a[i][j]);
printf("\n");
}
printf("\n");
mx=findMax(a,r,c);
printf("The maximum elements in the matrix is %d\n",mx);
return 0;
}
int findMax(int (*a)[10],int r,int c)
{
int t,i,j;
t=a[1][1];
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
if(a[i][j]>t)
t=a[i][j];
}
}
return (t);
}
Hope, it will help. :)
You can not pass a 2d array (int a[10][10]) to a pointer to pointer.
int findMax(int **a, int r, int c)
should be
int findMax(int (*a)[10], int r, int c) /* Pointer to array of 10 ints */
Use the heap if you don't know the size of the 2d array beforehand (and note that arrays are base 0):
#include <stdio.h>
#include <stdlib.h>
int findMax(int *a, int r, int c);
int main(void)
{
int *a, r, c, i, j, max;
printf("Enter the number of rows in the matrix\n");
scanf("%d", &r);
printf("Enter the number of columns in the matrix\n");
scanf("%d", &c);
a = malloc(r * c * sizeof(*a));
if (a == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
printf("Enter the elements in the matrix\n");
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++)
scanf("%d", &a[i * c + j]);
}
printf("The matrix is\n");
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++)
printf("%d", a[i * c + j]);
}
printf("\n");
max = findMax(a, r, c);
printf("The maximum elements in the matrix is %d\n", max);
free(a);
return 0;
}
int findMax(int *a,int r, int c)
{
int t, i, j;
t = a[0];
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
if(a[i * c + j] > t)
t = a[i * c + j];
}
}
return t;
}
Or you can use a variable-length-array if you are under C99:
#include <stdio.h>
int findMax(int r, int c, int (*a)[]);
int main(void)
{
int i, j, max, r, c;
printf("Enter the number of rows in the matrix\n");
scanf("%d", &r);
printf("Enter the number of columns in the matrix\n");
scanf("%d", &c);
int a[r][c];
printf("Enter the elements in the matrix\n");
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++)
scanf("%d", &a[i][j]);
}
printf("The matrix is\n");
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++)
printf("%d", a[i][j]);
}
printf("\n");
max = findMax(r, c, a);
printf("The maximum elements in the matrix is %d\n", max);
return 0;
}
int findMax(int r, int c, int (*a)[c])
{
int t, i, j;
t = a[0][0];
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++) {
if(a[i][j] > t)
t = a[i][j];
}
}
return t;
}
This code snippet works for you.
printf("Enter the elements in the matrix\n");
for(i=0;i<m;i++){
a[i]=malloc(sizeof(int)*c);
for(j=0;j<n;j++)
scanf("%d", &a[i][j]);
}
Then your function call max=findMax(a,r,c); will work.
#include<stdio.h>
#include<stdlib.h>
int findMax(int **a,int m,int n)
{
int i,j,larg;
larg=a[0][0];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(larg<a[i][j])
larg=a[i][j];
}
}
return larg;
}
int main()
{
int m,n,i,j,larg;
printf("Enter the number of rows in the matrix\n");
scanf("%d",&m);
printf("Enter the number of columns in the matrix\n");
scanf("%d",&n);
printf("Enter the elements in the matrix\n");
int **a=(int**)malloc(m*sizeof(int *));
for(i=0;i<m;i++)
a[i]=(int *)malloc(n*sizeof(int));
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d\n",&a[i][j]);
larg=findMax(a,m,n);
printf("The matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("The maximum element in the matrix is %d\n",larg);
return 0;
}
Related
i have tried doing it with functions and arrays but have failed in-order to do it with pointers.
I want to do it with the help of pointers instead of arrays, but i am having problem with passing and calling the values of arrays using pointers.
below is the code.
#include <stdio.h>
void final_array(int arr[], int size);
void array(int arr[], int i, int size);
int main()
{
int num, size[100];
int i, j;
int arr[100][100];
printf("Enter the number of arrays: \t");
scanf("%d", &num);
num = num < 100 ? num: 100;
//feeding elements.
for (i = 0; i<num; i++)
{
printf("\nEnter the size of the array: \t");
scanf("%d", &size[i]);
printf("\nEnter the array: ");
size[i] = size[i] < 100 ? size[i] : 100;
array(&arr[i][0], i, size[i]);
}
for(i=0; i<num; i++)
{
final_array(&arr[i][0], size[i]);
printf("\n");
}
printf("\nPress Enter key to exit.\n");
getchar();
return 0;
}
void array(int arr[], int i, int size)
{
int j;
for (j = 0; j<size; j++)
{
printf("\nEnter arr[%d][%d]: \t",i, j);
scanf("%d", &arr[j]);
}
}
void final_array(int arr[], int size)
{
int j;
for(j=0; j<size; j++)
{
printf("%d\t", arr[j]);
}
}
I guess this should solve your problem.Let me know if it's correct.
#include <stdio.h>
#include <stdlib.h>
void final_array(int *arr, int size);
void array(int *arr, int i, int size);
int main()
{
int num;
int i, j;
int **arr=(int **)malloc(100 * sizeof(int *));
for (i=0; i<100; i++)
arr[i] = (int *)malloc(100 * sizeof(int));
printf("Enter the number of arrays: \t");
scanf("%d", &num);
int *size=malloc(sizeof(int)*num);
num = num < 100 ? num: 100;
//feeding elements.
for (i = 0; i<num; i++)
{
printf("\nEnter the size of the array: \t");
scanf("%d", &size[i]);
printf("\nEnter the array: ");
size[i] = size[i] < 100 ? size[i] : 100;
array(&arr[i][0], i, size[i]);
}
for(i=0; i<num; i++)
{
final_array(&arr[i][0], size[i]);
printf("\n");
}
printf("\nPress Enter key to exit.\n");
getchar();
return 0;
}
void array(int *arr, int i, int size)
{
int j;
for (j = 0; j<size; j++)
{
printf("\nEnter arr[%d][%d]: \t",i, j);
scanf("%d", &arr[j]);
}
}
void final_array(int *arr, int size)
{
int j;
for(j=0; j<size; j++)
{
printf("%d\t", arr[j]);
}
}
I did this program for a homework and it crashes without any error when run.
Also after correcting it, any suggestions to increase the efficiency of my coding approach are appreciated.
First I declared m,n,p,q as global variables an I passed only the arrays to the functions, but the program behaved weird.
Then I included the dimensions of the arrays as arguments in every function and declared it everywhere. CRASH
*VLA SUPPORTED
//functions on matrices
#include<stdio.h>
int i, j;
void getMatrix(int m, int n, int values[m][n]);
void displayMatrix(int m, int n, int values[m][n]);
void transposeMatrix(int m, int n, int values[m][n]);
void addMatrices(int m, int n, int p, int q, int A[m][n], int B[p][q]);
void multiplyMatrices(int m, int n, int p, int q, int A[m][n], int B[p][q]);
int main()
{
int m, n, p, q, A[m][n], B[p][q];
printf("Enter the no. of Rows of the first Matrix : ");
scanf("%d", &m);
printf("Enter the no. of Columns of the first Matrix : ");
scanf("%d", &n);
printf("Enter the elements of the first matrix: \n");
getMatrix(m, n, A);
printf("The entered Matrix:\n");
displayMatrix(m, n, A);
printf("The transpose of the entered Matrix:\n");
transposeMatrix(m, n, A);
printf("Enter the no. of Rows of the second Matrix : ");
scanf("%d", &p);
printf("Enter the no. of Columns of the second Matrix : ");
scanf("%d", &q);
printf("Enter the elements of the secong matrix: \n");
getMatrix(p, q, B);
printf("The entered Matrix:\n");
displayMatrix(p, q, B);
printf("The transpose of the entered Matrix:\n");
transposeMatrix(p, q, B);
printf("Addition of the Matrices:\n");
addMatrices(m, n, p, q, A, B);
printf("Multiplication of the Matrices:\n");
multiplyMatrices(m, n, p, q, A, B);
return 0;
}
void getMatrix(int m, int n, int values[m][n])
{
for(i = 0; i < m; ++i)
for(j = 0; j < n; ++j)
scanf("%d", &values[i][j]);
}
void displayMatrix(int m, int n, int values[m][n])
{
for(i = 0; i < m; ++i)
{
for(j = 0; j < n; ++j)
printf("%3d ", values[i][j]);
printf("\n");
}
}
void transposeMatrix(int m, int n, int values[m][n])
{
int transpose[n][m];
for(i = 0; i < n; ++i)
for(j =0; j < m; ++j)
transpose[i][j] = values[j][i];
displayMatrix(n, m, transpose);
}
void addMatrices(int m, int n, int p, int q, int A[m][n], int B[p][q])
{
int C[m][n];
if(m == p && n == q)
{
for(i = 0; i < m; ++i)
for(j = 0; j < n; ++j)
C[i][j] = A[i][j] + B[i][j];
displayMatrix(m, n, C);
}
else
printf("Cannot add these Matrices!\n");
}
void multiplyMatrices(int m, int n, int p, int q, int A[m][n], int B[p][q])
{
int C[m][q], k, sum = 0;
if(n == p)
{
for(i = 0; i < m; ++i)
for(j = 0; j < q; ++j)
{
for(k = 0; k < n; ++k)
sum += A[i][j] * B[j][i];
C[i][j] = sum;
sum = 0;
}
displayMatrix(m, q, C);
}
else
printf("Cannot multiply these Matrices!\n");
}
Initiliazie m and n so that you don't get UB when using them in array index in VLA.
Unsed loop in multiply matrices
for(k = 0; k < n; ++k)
sum += A[i][j] * B[j][i];
will be
for(k = 0; k < n; ++k)
sum += A[i][k] * B[k][j];
Don't use global variables unless you need to. It is good practice to make index variables of for loop local.
for(int i=0;i<n;i++)
...
This program is supposed to take 2 arrays and perform the dot product on each of the elements in the array.
My program is fine if the index of n is less than 5; however, once the index of the array is greater than 5 only the first element in the first array is wrong ( I checked by adding a printf statement in the function). I don't know how to fix this bug.
#include <stdio.h>
void multi_vec(int *v1, int *v2, int *v3, int n);
int main(void)
{
int n, i;
int v1[n];
int v2[n];
int v3[n];
printf("Enter the length of the two vectors\n");
scanf("%d", &n);
printf("Enter numbers for the first array\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &v1[i]);
}
printf("Enter numbers for the second array\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &v2[i]);
}
multi_vec(v1, v2, v3, n);
for (i = 0; i < n; i++) {
printf("%d", v3[i]);
}
printf("\n");
return 0;
}
void multi_vec(int *v1, int *v2, int *v3, int n)
{
int i;
for (i = 0; i < n; i++) {
*(v3+i) = *(v1+i) * *(v2+i);
}
}
Correct code
#include <stdio.h>
void multi_vec(int *v1, int *v2, int *v3, int n);
int main(void)
{
int n, i;
printf("Enter the length of the two vectors\n");
scanf("%d", &n);
int v1[n],v2[n],v3[n]; //you didn't initialize n
printf("Enter numbers for the first array\n"); //printf statements had extra ',n'
for (i = 0; i < n; i++) {
scanf("%d", &v1[i]);
}
printf("Enter numbers for the second array\n"); //printf statements had extra ',n'
for (i = 0; i < n; i++) {
scanf("%d", &v2[i]);
}
multi_vec(v1, v2, v3, n);
for (i = 0; i < n; i++) {
printf("%d ", v3[i]);
}
printf("\n");
return 0;
}
void multi_vec(int *v1, int *v2, int *v3, int n)
{
int i;
for (i = 0; i < n; i++) {
*(v3+i) = *(v1+i) * *(v2+i);
}
}
Alright, so from what I've seen, it seems these errors usually show up whenever there's a semicolon missing, or a misplaced curly-brace or something of that nature. I'm surely just missing it somehow, but I'm not able to find where the error is, or if it is due to an error of that sort. This error in pointing toward line 235, so the end of the last function.
This is the code
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAX 100
void display_menu();
int check_option(int option);
int check_size(int size);
void initialize_2Darray(int x[][MAX], int size);
void print_2Darray(int x[][MAX], int size);
void initialize_1Darray(int y[], int size);
void print_1Darray(int y[], int size);
int search_min(int x[][MAX], int r, int c, int size);
int count_match(int x[][MAX], int y[], int size, int r);
int closest_row(int x[][MAX], int y[], int size);
void sort_1Darray(int y[], int size);
void sort_2Darray(int x[][MAX], int size);
int main()
{
int size, r, c, option;
int exit = 0; //exits program when = 1
int x[MAX][MAX], y[MAX];
srand(time(NULL));
printf("Enter the size: ");
scanf("%d", &size);
while(check_size(size) == 0) {
printf("\nInvalid input, enter the size of the array again: ");
scanf("%d", &size);
}
while(exit != 6) {
initialize_2Darray(x, size);
initialize_1Darray(y, size);
display_menu();
scanf("%d", &option);
while(check_option(option) == 0) {
printf("Invalid option, enter again: ");
scanf("%d", &option);
}
//Search Min Operation
if(option == 1) {
print_2Darray(x, size);
printf("Enter the row: ");
scanf("%d", &r);
printf("\nEnter the col: ");
scanf("%d", &c);
printf("The smallest number present in row %d and col %d is %d", r, c, search_min(x, r, c, size));
}
//Count Matches Op.
else if(option == 2) {
printf("Count Matches Operation\n\n2D array\n");
print_2Darray(x, size);
printf("\n1D array\n");
print_1Darray(y, size);
printf("\nEnter the row: ");
scanf("%d", &r);
if(count_match(x, y, size, r) > 0)
printf("There are %d matches from 1D array present in 2D array", count_match(x, y, size, r));
else
printf("There are no numbers from 1D array present in 2D array");
}
//Closest Row Op.
else if(option == 3) {
printf("\nClosest Row Operation\n\n2D array\n");
print_2Darray(x, size);
printf("\n1D array\n");
print_1Darray(y, size);
printf("Row closest to the 1D array is row %d", closest_row(x, y, size));
}
//Sort 1D Array Op.
else if(option == 4) {
printf("Sort 1D Array Operation\n\n1D Array before sorting:\n");
print_1Darray(y, size);
sort_1Darray(y, size);
printf("\n\n1D Array after sorting:\n");
print_1Darray(y, size);
}
//Sort 2D Array Op.
else if(option == 5) {
printf("\nSort 2D Array Option\n\n2D Array before sorting:\n");
print_2Darray(x, size);
sort_2Darray(x, size);
printf("\n\n2D Array after sorting:\n");
print_2Darray(x, size);
}
//Exit
else if(option == 6)
exit = 6;
}
return 0;
}
void display_menu()
{
printf("\nArray operations, your options are:\n\n1: Search Min\n2: Count Matches\n3: Closest Row\n4: Sort 1D Array\n5: Sort 2D Array\n6: Exit\nEnter the operation you want to perform: ");
}
int check_option(int option)
{
if(option > 0 && option < 7)
return 1;
else
return 0;
}
int check_size(int size)
{
if(size > 0 && size <= 100)
return 1;
else
return 0;
}
void initialize_2Darray(int x[][MAX], int size)
{
int i, s; //counters
for(i=0; i < size; i++) {
for(s=0; s < size; s++) {
x[i][s] = rand()%10;
}
}
}
void print_2Darray(int x[][MAX], int size)
{
int i, s; //counters
for(i=0; i < size; i++) {
printf("\n");
for(s=0; s < size; s++) {
printf("%d ", x[i][s]);
}
}
printf("\n");
}
void initialize_1Darray(int y[], int size)
{
int i, r;
for(i=0; i < size; i++) {
r = rand()%10;
y[i] = r;
}
}
void print_1Darray(int y[], int size)
{
int i;
//Prints array values until (s)ize is reached
for(i=0; i < size; i++) {
printf("%d ", y[i]);
}
}
int search_min(int x[][MAX], int r, int c, int size)
{
int i, j; //counters
int min = 9;
for(i=0; i < size; i++) {
if(x[r][i] < min) {
min = x[r][i];
}
}
for(j=0; j < size; j++) {
if(x[j][c] < min) {
min = x[j][c];
}
}
return min;
}
int count_match(int x[][MAX], int y[], int size, int r)
{
int i, j, count;
for(i=0; i < size; i++) {
for(j=0; j < size; j++) {
if(y[i] == x[r][j])
count++;
}
return count;
}
int closest_row(int x[][MAX], int y[], int size)
{
int l, i, j; //counters
int sum = 0;
int dif = 0;
int row, totaldif; //best matching row & total dif
for(l=0; l < size; l++) {
for(i=0; i < size; i++) {
for(j=0; j < size; j++) {
dif = abs(y[j] - x[i][j]);
sum += dif;
}
if(sum < totaldif) {
totaldif = sum;
row = i;
}
sum = 0;
}
}
return row;
}
void sort_1Darray(int y[], int size)
{
int a, b, temp;
//Loops through the array, swapping values until in proper order of ascension
for(a = 0; a < size; a++) {
for(b = 0; b < size-1; b++) {
if(y[b] > y[b+1]) {
temp = y[b+1];
y[b+1] = y[b];
y[b] = temp;
}
}
}
}
void sort_2Darray(int x[][MAX], int size)
{
int a, b, c, temp;
//Loops through the array, swapping values until in proper order of ascension
for(c=0; c < size; c++) {
for(a = 0; a < size; a++) {
for(b = 0; b < size-1; b++) {
if(x[a][b] > x[a][b+1]) {
temp = x[a][b+1];
x[a][b+1] = x[a][b];
x[a][b] = temp;
}
}
}
}
}
Run your code through indent. It can help you find this sort of problem very quickly.
Here's the output from your code. As you can see, the problem is caused by a missing brace at the end of count_match():
int
count_match(int x[][MAX], int y[], int size, int r)
{
int i , j, count;
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
if (y[i] == x[r][j])
count++;
}
return count;
}
/*** CLOSING BRACE MISSING HERE ***/
int closest_row(int x[][MAX], int y[], int size){
int l , i, j;
//counters
The section of function is never entered in the following code
please help me out!
#include<stdio.h>
int findMax(int *a[], int m, int n)//this function is not entering
{
int i,j,k=0;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(k<a[i][j])
k=a[i][j];
printf("hi");//this hi is also not printing
//this hi is just for testing
return k;
}
//This function correct it if possible
int main()
{
int m,n,a[50][50],i,j,k=0;
printf("Enter the number of rows in the matrix\n");
scanf("%d",&m);
printf("Enter the number of columns in the matrix\n");
scanf("%d",&n);
printf("Enter the elements in the matrix\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
printf("The matrix is");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
}
k=findMax((int **)a,m,n);//statements after this is never running but no
//compilation errors
printf("\nThe maximum element in the matrix is %d",k);
return 0;
}
please help me out!!
Thanks to you in advance!!
what you are doing here: int findMax(int *a[], int m, int n) is trying to point at int 50 times, but you want to point at a variabile type int 50 times. therefor you want to decleare the pointer before you decleare the array.
use therefor:
int findMax(int (*a)[], int m, int n)
this should work now without the casting
just turn it into:
k=findmax(a,m,n);
here is a simple tutorial on the subject.
int findMax(int *a[], int m, int n)
int *a[] is an array of pointers to int, you want a pointer to an array of 50 int's:
int findMax(int (*a)[50], int m, int n)
or
int findMax(int a[][50], int m, int n)
You don't need the cast, call it using:
k = findMax(a, m, n);
Or you can use a flat array, an example:
#include <stdio.h>
void foo(int *arr, int rows, int cols)
{
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++)
printf("%d ", arr[i * cols + j]);
printf("\n");
}
}
#define ROWS 3
#define COLS 3
int main(void)
{
int arr[ROWS * COLS];
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++)
arr[i * COLS + j] = i * COLS + j;
}
foo(arr, ROWS, COLS);
return 0;
}
If you dont know the sizes of the 2d array beforehand you can use malloc:
#include <stdio.h>
#include <stdlib.h>
void foo(int *arr, int rows, int cols)
{
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++)
printf("%d ", arr[i * cols + j]);
printf("\n");
}
}
int main(void)
{
int i, j, rows, cols;
int *arr;
printf("Num of rows:");
scanf("%d", &rows);
printf("Num of cols:");
scanf("%d", &cols);
arr = malloc(rows * cols);
if (arr == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++)
arr[i * cols + j] = i * cols + j;
}
foo(arr, rows, cols);
free(arr);
return 0;
}
#include <stdio.h>
#include <limits.h>
#define MATRIX_SIZE 50
int findMax(int a[MATRIX_SIZE][MATRIX_SIZE], int m, int n){
//Valid any of the following is
//int a[MATRIX_SIZE][MATRIX_SIZE]
//int a[][MATRIX_SIZE]
//int (*a)[MATRIX_SIZE]
int i, j, k=INT_MIN;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(k<a[i][j])
k=a[i][j];
return k;
}
int main(void){
int m, n, a[MATRIX_SIZE][MATRIX_SIZE], i, j, k=0;
printf("Enter the number of rows(<=50) in the matrix\n");
scanf("%d",&m);
printf("Enter the number of columns(<=50) in the matrix\n");
scanf("%d",&n);
if(m < 1 || n < 1 || m > 50 || n > 50){
printf("invalid input!\n");
return -1;
}
printf("Enter the elements in the matrix\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("The matrix is");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
}
k=findMax(a, m, n);
printf("\nThe maximum element in the matrix is %d\n", k);
return 0;
}