-->




Cpp Function to Find Largest and smallest number in array

1 comment
This Cpp tutorial contains
 Compiler used: CodeBlocks Cpp Compiler

Write a program which generates some random number and store in array after it program pass array to a function which calculates maximum and minimum number in array and display it use any Cpp Compiler to code.

How program works
  • Declare array of size 10
  • Using for loop assign array indexes with random values between 1 and thousand
  • Call the function and pass array and its size as argument
  • Function declares two integers max and min and assign both integers with arrays first index value
  • Then with in for loop there are two if condition first check is for minimum number and second check is for maximum number
  • Finally program display the output values of both integers min and max

      Cpp Code
  1.  #include <iostream>
  2. #include <stdlib.h>
  3. using namespace std;
  4. void FindMaxMin(int *array, int size)
  5. {        int min,max;
  6.          min=max=array[0];
  7.          for(int i=0;i<size;i++)
  8.          {
  9.              if(array[i]<min)
  10.                min=array[i];
  11.             else if(array[i]>max)
  12.                max=array[i];
  13.          }
  14.  cout<<"Minimum Number  = "<<min<<endl;
  15.  cout<<"Maximum Number = "<<max<<endl;
  16. }
  17. int main()
  18. {
  19.     int array[10];
  20.     for(int i=0;i<=9;i++)
  21.         {
  22.          array[i]=rand()%1000+1;
  23.          cout<<"array ["<<i<<"]"<<"= "<<array[i]<<endl;
  24.         }
  25.     FindMaxMin(array,10);
  26. return 0;
  27. }
 Sample Output
Cpp Function To Find Largest And Smallest Number in Array


Another Example without Function more simple
Find Maximum and Minium Number in Array Cpp Code

More Cpp Program Tutorials
C++ Simple Examples

1 comment:

  1. PLEASE HELP... ERROR KEEP DISTURBING ME :(


    #include
    #include

    #define a(x) (1/(1+exp(-x)))
    #define b(x,t) (t*cos((double)t)+(5-15*t*sin((double)t))/(1+pow(t,2)))
    #define N 22

    using namespace std;

    void main()
    {
    int i;
    double x, t, f, g, h, Maxf, Minf, Maxg, Ming, Maxh, Minh;
    double x[N+1], t[N+1], f[N+1], g[N+1], h[N+1];

    for (x=0.0; x<=1.0; x+=0.1)
    for (t=0.0; t<=1.0; t+=0.05)
    {
    f = a(x);
    g = b(x,t);
    h = log(pow(f,2)+pow(g,2));

    cout << "x=" << x << "\t t=" << t;
    cout << "\t f(x)=" << f << "\t g(x)=" << g << "\t\t h(f,g)=" << h << endl;
    }

    srand(time(0));
    for (i=1; i<=N; i++)
    {
    f[i]=1/((double)1+rand()%10);
    g[i]=1/((double)1+rand()%10);
    h[i]=1/((double)1+rand()%10);
    }

    Maxf=f[1]; Minf=f[1];
    Maxg=g[1]; Minh=g[1];
    Maxh=h[1]; Minh=h[1];

    for (i=1;i<=N;i++)
    {

    if (Maxf<=f[i])
    Maxf=f[i];
    if (Minf>=f[i])
    Minf=f[i];

    if (Maxg<=g[i])
    Maxg=g[i];
    if (Ming>=g[i])
    Ming=g[i];

    if (Maxh<=h[i])
    Maxh=h[i];
    if (Minh>=h[i])
    Minh=h[i];
    }


    cout << "The Maximum number of f(x) is " << Maxf << endl;
    cout << "The Minimum number of f(x) is " << Minf << endl;
    cout << "The Maximum number of g(x,t) is " << Maxg << endl;
    cout << "The Minimum number of g(x,t) is " << Ming << endl;
    cout << "The Maximum number of h(f,g) is " << Maxh << endl;
    cout << "The Minimum number of h(f,g) is " << Minh << endl;


    cin.get();
    }

    ReplyDelete