DreamBig
DreamBig

Reputation: 19

Maximum And Minimum With Pointer

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);
}

Upvotes: 0

Views: 7241

Answers (4)

Shahzad Barkati
Shahzad Barkati

Reputation: 2526

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

Upvotes: 0

alk
alk

Reputation: 70931

  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).

Upvotes: 1

ameyCU
ameyCU

Reputation: 16607

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

Upvotes: 2

ThunderWiring
ThunderWiring

Reputation: 738

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 >=.

Upvotes: 0

Related Questions