-->





Translate

Showing posts with label largest. Show all posts
Showing posts with label largest. Show all posts

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
Read More...

Maximum or largest number in array c++ code

3 comments
Write a C++ program which find largest or maximum number in array using for loop and if statement

Concept used
  • Array in c++
  • for loop
  • if statement

Post contains c++ code and Dry run of code

How program works
  • Program first take size of array from user
  • Then input element or array one by one
  • then show the maximum number in array

c++ code: Using CodeBlocks Compiler

    #include<iostream>
    using namespace std;
    int main()
    {

    cout<<"Enter The Size Of Array:   ";
    int size;
    cin>>size;
    int array[size], key,i;

    // Taking Input In Array
    for(int j=0;j<size;j++){
    cout<<"Enter "<<j<<" Element : ";
    cin>>array[j];
    }
    //Your Entered Array Is
    for(int a=0;a<size;a++){
       cout<<"array[ "<<a<<" ]  =  ";
       cout<<array[a]<<endl;
    }

       int  maximum=array[0];
       for(int i=0;i<size;i++){

          if(array[i]>maximum){
              maximum=array[i];
          }
    }

   cout<<"\n\nMaximum Number is in array is :

   "<<maximum;
   return 0;
    
    }


Sample Output
Maximum or largest number in array c++ code
Sample input output


Dry Run Of Program:
Let size of array is equal to 5
and user enters following numbers
5
6
-7
99
78






Lets dry run main for loop which contains an if statement

which is
     int maximum=array[0];
         for(int i=0;i<size;i++){

          if(array[i]>maximum){
              maximum=array[i];
          }

we have an integer which is maximum = array[0];
so value of maximum= 5;

5, 6, -7, 99, 78

at i=0;
array[i]=5; maximum=5
if(array[i]>maximum)
     5         >    5

condition false


5, 6, -7, 99, 78
at i=1
array[i]=6; maximum=5
if(array[i]>maximum)
     6         >    5

condition True    then maximum=6


5, 6, -7, 99, 78
at i=2
array[i]=-7;  maximum=6
if(array[i]>maximum)
     -7         >    5

condition false



5, 6, -7, 99, 78
at i=3
array[i]= 99;  maximum=6
if(array[i]>maximum)
     99        >   6

condition True  maximum= 99


5, 6, -7, 99, 78
at i=4
array[i]=78;  maximum=99
if(array[i]>maximum)
     78         >    99

condition false



final result is  maximum= 99

so 99 is the maximum number in array


Recommended
There always more than one way to code a program for fast learning change the logic of the program do experiment with code one of the most important is dry run the program on paper it will be very helpful for understand it also improves the logic building

This program will help to understand the working of array, for loop, if statement and assignment operator

More Programs here C++ Simple Examples
Read More...