Di Posting Oleh : Simple Learning
Kategori : array code cpp find largest max-number maximum number
Write a C++ program which find largest or maximum number in array using for loop and if statement
Concept used
Post contains c++ code and Dry run of code
How program works
c++ code: Using CodeBlocks Compiler
Sample 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
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
More Programs here C++ Simple Examples
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> "<<maximum; |
Sample Output
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 buildingThis program will help to understand the working of array, for loop, if statement and assignment operator
More Programs here C++ Simple Examples
0 Response to "Maximum or largest number in array c++ code"
Post a Comment