Pages

Tuesday

C Program to Find Maximum and Minimum of Array Elements using Pointers

Here's a C program to find maximum and minimum value stored in an integer array using a function that takes pointer as arguments. This program uses C concepts like Array in C, Call by Reference and Pointer in C.


# include <stdio.h>
# include <conio.h>

void minmax(int array[], int length, int *min,int *max);

int main(){
  int length, int_array[10], max = 0, min = 0;
  printf("Enter the no of elements to store in array (max 10): ");
  scanf("%d", &length);
  for(int i = 0;i < length;i++){
    printf("\nEnter element no %d: ", (i + 1));
    scanf("%d", &int_array[i]); 
  }
  minmax(int_array, length, &min, &max);  
  printf("\nMin = %d, Max = %d", min, max);
  return 0;
} 

void minmax(int array[], int length, int *min,int *max){
  *min = array[0];
  *max = array[0];
  for(int i = 1;i < length;i++){
    if(array[i] > *max){
      *max = array[i];
    }
    if(array[i] < *min){
      *min = array[i];
    }
  }
}


Output of above program:

Enter the no of elements to store in array (max 10): 10
Enter element no 1: 5
Enter element no 2: 15
Enter element no 3: 2
Enter element no 4: 6
Enter element no 5: 5
Enter element no 6: 8
Enter element no 7: 9
Enter element no 8: 6
Enter element no 9: -1
Enter element no 10: 0
Min = -1, Max = 15

1 comment:

  1. Hello There,

    I’ve often thought about this C Program to Find Maximum and Minimum of Array Elements using Pointers. Nice to have it laid out so clearly. Great eye opener.

    We have two numbers (binary) which lenght is n=2^k, k is from N.
    We need to multiplicate this two numbers with method divide and conquer.
    x =1011 * 2^4 +0110.
    How to do that in c++?

    Please keep providing such valuable information.

    Many Thanks,

    Please keep providing such valuable information.

    Thanks,
    Preethi

    ReplyDelete