0

I tried to run this but it keeps give me zero value. What is wrong in this code?

#include<stdio.h>

main(){
    int i,min,max,arr[3]={10,20,40};
    int *ptr_arr;
    ptr_arr=&arr[3];

    for(i=0;i<3;i++){
      if(max>*ptr_arr)
        max=*ptr_arr;
      if(min>*ptr_arr)
        min=*ptr_arr;
    }

    printf("The Maximum Number Is %d\n ",max);
    printf("The Minimum Number Is %d ",min);
}
3
  • this line: ptr_arr=&arr[3]; is accessing an address beyond the end of the array. This results in undefined behaviour and could lead to a seg fault event. Commented Sep 6, 2015 at 17:21
  • the for loop is not traversing the array so what ever uninitialized/trash values are at arr[3], min and max on the stack are just compared 3 times. Suggest: eliminate ptr_arr, initialize max to MIN_INT, initialize min to INT_MAX, use arr[i[ for the comparison value Commented Sep 6, 2015 at 17:25
  • there are only 2 valid declarations of main() (and one optional). The valid declarations are: int main( void ) and int main( int argc, char *argv[] ) The optional declaration is int main() which is the same as int main( void ) Note that char *argv[] is the same as char **argv When compiling, always enable all warnings, then your compiler would have told you about the problem. Commented Sep 6, 2015 at 17:31

4 Answers 4

2
ptr_arr=&arr[3];  // points to index which is beyond no. of index of array

As declaration of arr is arr[3]={10,20,40};so it's valid indexes are 0,1 and 2 .So there is no index 3(array indexing starts with 0).

Also min and max what value does they have ? Uninitialized , so how can your code give correct output.

Make the following changes -

     int min=arr[0],max=0;
     ...
     ptr_arr=arr;   // points to address of array's first element

And in for loop see condition and increment pointer-

     if(max>*ptr_arr)  // change condition to max<=*ptr_arr
     ...
     ptr_arr++;

See worning example here-https://ideone.com/r3nv8R

Sign up to request clarification or add additional context in comments.

10 Comments

Thank you for your help. I fixed it, and now it give me the same number. It give me the minimum number which is 10 for max and min. Do you know why?
@Moayad See my answer I told to change the if condition.
Thank you. I have just some questions. First why did you use #include <limits.h>? and can i put ptr_arr++ at the third statment in the array ?
@Moayad limits.h header which contains marco's like INT_MAX as told by alk Sir. What you mean by this can i put ptr_arr++ at the third statment in the array ? is unclear to me .
i mean in the increment in loop for example can i write for(i=0;i<=3;ptr_arr++) instead of write under the if ? and i have another question why you declare min=a[0]; like this why you didn't declare it like this min=0; and sorry for too many questions i'm new in programming
|
1
  1. The code misses to initialise min and max to meaningful values.

    Do

    int min = INT_MAX;
    int max = -INT_MAX;
    

    to have the macro above available #include <limits.h>.

  2. ptr_arr gets initialised wrongly to point beyond the array.

    Do

    int * ptr_arr = arr; /* Here the array "decays" to address of its 1st element. */
    

    or

    int * ptr_arr = &arr[0]; /* Explicitly use the address of the array 1st element. */
    
  3. The code misses to increase ptr_arr to successively access all array's element while looping.

    Add

    ++ptr_arr;
    

    as last statement to the loop.


Also read your code closely to find a nasty typo in comparing.


main() should at least read int main(void).

4 Comments

Thank you a lot man. I did what you said but it gave me this error."error: 'INT_MAX' was not declared in this scope int min=INT_MAX;" and why we give min and max INT_MAX ?
@alk You might want to check conditions once .
@ameyCU: I leave this very bit to the OP.
@alk Very well Sir !! :)
0

Just few notes on your code:

you should give min and max an initial value(a value from the array), why? because the initial garbage value might be beyond your array values(say 9999 for example) which yields a wrong result, because your array doesn't have such a value.

Also, im not sure why you need ptr_arr ?

Here is a modified version of your code:

#include<stdio.h>

int main(){
    int i,min,max,arr[3]={10,20,40};
    max = arr[0]; //initialize 'max' to be the first element of the array
    min = arr[0]; //initialize 'min' to be the first element of the array
    for(i=0;i<3;i++){
      if(arr[i] >= max)
        max = arr[i];
      if(arr[i] <= min)
        min = arr[i];
    }

    printf("The Maximum Number Is %d\n ",max);
    printf("The Minimum Number Is %d ",min);
    return 0;
}

Notice also in the if statement i added <= and >=.

Comments

0

Tested and working:

#include <stdio.h>

int main(void) {
  int i = 0,min ,max,arr[3]={20,40,10};

  int *ptr_arr;
  ptr_arr=&arr[0];
  min = max = arr[0];

  for(i=0;i<3;i++){

   if(max < ptr_arr[i]) {
     max=ptr_arr[i];
   }
  
   if(min>ptr_arr[i]){
     min=ptr_arr[i];
   }
 }

 printf("The Maximum Number Is %d\n ",max);
 printf("The Minimum Number Is %d ",min);

}

OUTPUT:

The Maximum Number Is 40

The Minimum Number Is 10

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.