-->





Maximum or largest number in array c++ code

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

3 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. I encountered a problem in this.
    On compiling I got this 'Constant Expression Required' Error and a highlight on the variable size.
    Please do help in ruling out this problem.
    Thanks in Advance.

    ReplyDelete
    Replies
    1. Josemon Baby:
      To make the code more simple set the constant size to array and it will be fine
      like that.
      #include
      using namespace std;
      int main()
      {

      int array[5], key,i;

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

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

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

      cout<<"\n\nMaximum Number is in array is : "<<maximum;
      return 0;

      }

      Delete