Finds the largest and the smallest value in the array. - C++ Data Type

C++ examples for Data Type:Array

Description

Finds the largest and the smallest value in the array.

Demo Code

#include <iostream>
using namespace std;
#include <stdlib.h>
const int SIZE = 15;
void main()//from  ww w. j a  v a  2s  .co  m
{
   int ara[SIZE];
   int high_val, low_val, ctr;
   // Fills array with random numbers from 0 to 99.
   for (ctr=0; ctr<SIZE; ctr++)
   {
      ara[ctr] = rand() % 100;
   }
   // Prints the array to the screen.
   cout << "Here are the " << SIZE << " random numbers:\n";
   for (ctr=0; ctr<SIZE; ctr++)
   {
      cout << ara[ctr] << "\n";
   }
   cout << "\n\n";       // Prints a blank line.
   high_val = ara[0];    // Initializes first element to both high and low.
   low_val  = ara[0];
   for (ctr=1; ctr<SIZE; ctr++)
   {
      // Stores current value if it is higher than the highest.
      if (ara[ctr] > high_val)
      {
         high_val = ara[ctr];
      }
      if (ara[ctr] < low_val)
      {
         low_val = ara[ctr];
      }
   }
   cout << "The highest number in the list is " << high_val << "\n";
   cout << "The lowest number in the list is " << low_val << "\n";
   return;
}

Result


Related Tutorials