0

The program must give the largest element of an array(A) with the help of recursive void TMax.But it's not working.I think that there's an error because of pointers of Max and I can't correct it.Can you help me,please?

#include <stdio.h>
#include <stdlib.h>

void TMax(int A[], int N,int *Max)
{
    if(N==0){
       *Max=A[0];
    }
    else
    {
        *Max=A[N];
         if(A[N]>*Max)
        {
            *Max=A[N];
        }
         TMax(A,N-1,*Max);
    }
}

int main()
{
    int A[] = { 1, 2, 999, 4, 20};
    int N = sizeof(A) / sizeof(A[0]);
    int k=A[N];

   TMax(A,N,&k);

    printf("%d",k);
}
4
  • First of all A[N] leads to undefined behavior since you are accessing beyond the array A limit Commented Sep 29, 2018 at 10:22
  • Your compiler must complain (so turn warnings on!): TMax(A,N-1,*Max); should be TMax(A,N-1,Max); And int k=A[N]; should be int k=A[N-1]; Commented Sep 29, 2018 at 10:23
  • int k = A[N] is array out of range, the acceptable range is [0 , N-1] Commented Sep 29, 2018 at 10:25
  • Pump up the compiler's warning level, read the warnings issued during compilation, understand them and then fix the code accordingly, do not cast to do so. Do so until no more warnings appear during compilation. Commented Sep 29, 2018 at 10:28

3 Answers 3

1
  1. You are passing int to int * which is wrong and will lead to undefined behavior.

    Even the compiler is warning about the same.

    warning: passing argument 3 of 'TMax' makes pointer from integer wi
    thout a cast [-Wint-conversion]
              TMax(A,N-1,*Max);
    
  2. You are accessing the array out of bound, max index is N-1.

  3. Your base case is assigning *Max=A[0]; in that case your mas will be always zero hence remove the assignment and just return..

Sample code:

void TMax(int A[], int N,int *Max)
{
    if(N==0){
       return;
    }
    else
    {
        if(A[N]>*Max)
        {
            *Max=A[N];
        }
         TMax(A,N-1,Max);
    }
}

And from main you call this way.

TMax(A,N-1,&k);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you.Your code is working,now I have another problem)))... when I have a "k" which is not initilized, it's not working.Is there a way to have a void that will work with not initialized "k"(I mean in the main I have "int k" in place of "int k=A[0]" .
You should initialize k to INT_MIN otherwise your k will have junk value.
1

You have several errors:

in Tmax:

    if(N==0){
       *Max=A[0];
    }

You want to test whether the array is empty. Then it has no elements and even A[0] does not exist. Remember that array indexes in C go from 0..n-1. So also:

    *Max=A[N-1];

and in main too: int k=A[N-1];

in Tmax, when you recursively call Tmax, you must pass Max, as it is already a pointer, and not *Max, which would be the value of Max, so:

    TMax(A,N-1,Max);

Comments

1

sizeof(A) / sizeof(A[0]) is going to return you size of array. In your case it is going to return 5 and you are trying to assign index 5 value of array in k but in the case of array index start form 0 to n-1 so k is going to contain garbage value .Try to make proper change in the value of N

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.