Write C++ program to find maximum and minimum element in array


I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


  1. #include <iostream>
  2. #define MAX_SIZE 100 //Maximum size of the array
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.  
  8. int arr[100];
  9. int i, max, min, size;
  10.  
  11. // Reading array sizr & elements in the array
  12.  
  13. cout<<"Enter size of the array: ";
  14. cin>>size;
  15. cout<<"Enter elements in the array: ";
  16. for(i=0; i<size; i++)
  17. {
  18. cin>>arr[i];
  19. }
  20.  
  21. /* Supposes the first element as maximum and minimum */
  22. max = arr[0];
  23. min = arr[0];
  24.  
  25. /*
  26.   * Finds maximum and minimum in all array elements.
  27.   */
  28. for(i=1; i<size; i++)
  29. {
  30. // Finding max number
  31. //if cuurent element of array is greater than max
  32. if(arr[i]>max)
  33. {
  34. max = arr[i];
  35. }
  36.  
  37. // Finding min number
  38. // If current element of array is smaller than min
  39. if(arr[i]<min)
  40. {
  41. min = arr[i];
  42. }
  43. }
  44. //Finding the maximum and minimum element
  45.  
  46. cout<<"Maximum element is: "<<max<<endl;
  47. cout<<"Minimum element is: "<<min<<endl;
  48. return 0;
  49. }
  50.  

Result

C++ language techstudy.org

     





Related Topics




Popular Posts