get MAX and MIN with pointers from an array in c


This example will help you if you

already have the array

A function will receive the array by using pointers

If not you only have to make a cycle and ask for numbers and scan them on the main

  1. #include <stdio.h>
  2. void prnt(int *);
  3. main()
  4. {int array[]={3,4,6,1,2,7,8,10,12,5};
  5. prnt(array);
  6. }
  7. void prnt(int *array){
  8. int i, c;
  9. int *minimum, *m;
  10. minimum = array;
  11. m= minimum;
  12. for ( c = 1 ; c < 10 ; c++ )
  13. {
  14. if ( *(array+c) < *minimum ){
  15. minimum = (array+c);
  16. }
  17. }
  18. for ( c = 1 ; c < 10 ; c++ ){
  19. if ( *(array+c) > *m ){
  20. m = (array+c);
  21. }
  22. }
  23. printf("Max %d.\n", *m);
  24. printf("Min %d.\n", *minimum);
  25. }

Comentarios

Entradas populares de este blog