0
#include<stdio.h>

int findMax(int **,int m,int n);

int main()
{
    int n;
    int a[20][20];
    int i, j, max;
    printf("\nEnter the number of rows in the array");
    scanf("%d", &m);
    printf("\nEnter the number of columns in the array");
    scanf("%d", &n);
    printf("\nEnter the elements of the matrix");
    for(i=0; i<m; i++)
    {
        for(j=0; j<n; j++)
        {
            scanf("%d", a[i][j]);
        }
        printf("\n");
    }
    printf("\nThe matrix is\n");
    for(i=0; i<m; i++)
    {
        for(j=0; j<n; j++)
        {
         printf("%d", a[i][j]);
        }
        printf("\n");
    }
    max = findMax((int **)a, m, n);
    printf("\nThe maximum element in the matrix is %d", max);
    return 0;
}

int findMax(int **a, int m, int n)
{
    int i, j, max;
    max = a[0][0];
    for(i=1; i<=m; i++)
    {
        for(j=1; j<=n; j++)
        {
            if(a[i][j] > max)
                max = a[i][j];
        }
    }
    return max;
}

The program should display the maximum element in the matrix. The maximum value should be found in the function block. While executing this program the control is passed to the function block, but the values aren't assigned and the entire block doesn't get executed and the program is terminated. What is wrong in this program?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
newbie
  • 5
  • 1
  • 3
  • I'm assuming the missing variable `m` in `main` is just a cut'n'paste error, otherwise this would never have compiled. – kdopen Apr 27 '15 at 16:19

2 Answers2

0

The problems are

  1. There is no variable m defined in main()
  2. max = findMax((int **)a,m,n); is wrong. You'll get a SEGFAULT if you pass it like this. The function expects a pointer to a pointer. A two dimensional array is not the same as that. So, casting it to a int** will only get you a segmentation fault later in your code.
  3. for(i=1;i<=m;i++) and for(j=1;j<=n;j++) will skip a lot of elements of the array ( it will skip more than the first element, that is, it will skip all the values at a[0][some other index] and a[some index][0] ).

So, you should change your function to

int findMax(int a[][20],int m,int n)

and call it with

max = findMax(a,m,n);

and also change those for loops to normal loops like

for(i=0;i<m;i++)
for(j=0;j<n;j++)

Here's a working implementation of your code

#include<stdio.h>
int findMax(int a[][20],int m,int n);
int main()
{
  int n,m;
  int a[20][20];
  int i,j,max;
  printf("\nEnter the number of rows in the array");
  scanf("%d", &m);
  printf("\nEnter the number of columns in the array");
  scanf("%d", &n);
  printf("\nEnter the elements of the matrix\n");
  for(i=0;i<m;i++)
  {
    for(j=0;j<n;j++)
    {
      scanf("%d", &a[i][j]);
    }
  }
  printf("\nThe matrix is\n");
  for(i=0;i<m;i++)
  {
    printf("\n");
    for(j=0;j<n;j++)
    {
      printf("%d\t", a[i][j]);
    }
  }
  max = findMax(a,m,n);
  printf("\nThe maximum element in the matrix is : %d", max);
  return 0;
}

int findMax(int a[][20],int m,int n)
{
  int i,j,max;
  max = a[0][0];
  for(i=0;i<m;i++)
  {
    for(j=0;j<n;j++)
    {
      if(a[i][j] > max)
        max = a[i][j];
    }
  }
  return max;
}
Arun A S
  • 6,421
  • 4
  • 29
  • 43
  • @newbie , no. They are different. The first one is a 2D array, and the second one is a pointer to a pointer ( both are different ) – Arun A S Apr 28 '15 at 09:26
  • @newbie , you might want to read this [Is 2d array a double pointer?](http://stackoverflow.com/questions/7586702/is-2d-array-a-double-pointer), or just google it for more details. – Arun A S Apr 28 '15 at 09:35
  • @newbie , **NO** , they are not the same. Please read the link I commented. – Arun A S Apr 28 '15 at 09:46
  • am submitting this code to an online compiler..its runs properly wen i test it but when i submit it for evaluation am still getting the feedback as compile time error. – newbie Apr 28 '15 at 09:52
  • Compilation Errors - sfns.c:10:5: error: conflicting types for ‘findMax’ sfns.c:9:6: note: previous declaration of ‘findMax’ was here – newbie Apr 28 '15 at 09:56
  • @newbie , I don't really know the exact cause for that error, but maybe you could change the function declaration to match the function definition ( I made that edit in my answer, so you can try it ) – Arun A S Apr 28 '15 at 09:59
  • Write a program to find the maximum element in a matrix using functions.Assume that the maximum number of rows and columns in the matrix is 10.Function Definitions: int findMax (int **a, int m, int n) this is the jist of the problem statement.i used the corrections you made in the code. are there anymore changes to be done to the code?? – newbie Apr 28 '15 at 10:03
  • @newbie , I'm really sorry to say this, but I am not very good at pointers, So I can't help you any further with this. You might need to learn more about pointers and 2D arrays, as well as Dynamic Programming before you can solve this. – Arun A S Apr 28 '15 at 10:22
  • @newbie, a similar question to yours was asked [here](http://stackoverflow.com/questions/29925206/whats-wrong-with-the-c-code/29925284?noredirect=1#29925297) and the solution might help you. – Arun A S Apr 28 '15 at 17:19
0

There are numerous problems with this code:

int findMax(int **a,int m,int n)
{
 int i,j,max;
 max = a[0][0];
 for(i=1;i<=m;i++)
 {
  for(j=1;j<=n;j++)
  {
   if(a[i][j] > max)
      max = a[i][j];
  }
 }
 return max;
}

Starting with the prototype, int ** states you are passing in a pointer to a pointer to an int - otherwise known as a an array of int pointers. This would be declared as

int * a[n];

That is not the same as a two dimensional array of int.

As your array a is declared to have a fixed size (20 x 20) the prototype should be

int findMax(int a[20][20], int m, int n)

Now the compiler knows how to index it correctly and has the correct type for an individual element such as a[0][0]

Next, C uses zero based arrays - which you acknowledge with the statement max = a[0][0], but your loops otherwise ignore the zeroth row and column, while indexing past the end of the array to the mth and nth entries.

They need to be

for (i=0; i < m; i++)
    for (j = 0; j< m; j++)
        ...
kdopen
  • 8,032
  • 7
  • 44
  • 52