This Tutorial contains:
Write a program to find maximum and minimum number in array using for loop and if statement.
To initialize array use random numbers
Program should display all random elements of array and minimum and maximum number in array
on the output console.
Program Explanation:
Dry running the code with a simple input so it will be easy to understand the working of program.
let there are five random values in array
55, 3, 2, 44, 100
we will run our main loop in which two if statements are used
before the start of the loop
max=55; min=55;
i=0; current array elements 55, 3, 2, 44, 100
min > array[i]
55 > 55 => condition false
max < array[i]
55 > 55 => condition false
------------------------------------------------------
i=1; current array elements 55, 3, 2, 44, 100
min > array[i]
55 > 3 => condition TRUE UPDATE min =3;
max < array[i]
55 < 3 => condition false max=55
------------------------------------------------------
i=2; current array elements 55, 3, 2, 44, 100
min > array[i]
3 > 2 => condition TRUE UPDATE min =2;
max < array[i]
55 < 2 => condition false
------------------------------------------------------
i=3; current array elements 55, 3, 2, 44, 100
min > array[i]2 > 55 => condition false
max < array[i]
55 > 44 => condition false
------------------------------------------------------
i=4; current array elements 55, 3, 2, 44, 100
min > array[i]2 > 100 => condition false
max < array[i]
55 < 100 => condition TRUE UPDATE max =100;
------------------------------------------------------
Hope this will be helpful.
find more codes here C++ Simple Examples
- C++ code to find maximum and minimum number in array
- Program basic working what is used in program and how
- Dry run of the program with respect to variable values for better under standing
- Arrays in c++
- for loop
- if statement
- Random numbers
Write a program to find maximum and minimum number in array using for loop and if statement.
To initialize array use random numbers
Program should display all random elements of array and minimum and maximum number in array
on the output console.
Program Explanation:
- Array size is fixed to 100 to change the size just change the value of size integer
- In the for loop array is initializing with a random number less than 100 to change the range just replace 100 by any desired number and it will produced numbers between 0 to a number you replaced
- In another for loop which programs go through from start of the array to the end and with contains two if statements which checks for both minimum and maximum number in array
#include<iostream>
|
Dry running the code with a simple input so it will be easy to understand the working of program.
let there are five random values in array
55, 3, 2, 44, 100
we will run our main loop in which two if statements are used
for(int i=0;i<size;i++){
// finding minimum number in array
if(min>array[i]){
min=array[i];
}
//finding maximum number in array
if(max<array[i]){
max=array[i];
}
}
before the start of the loop
max=55; min=55;
i=0; current array elements 55, 3, 2, 44, 100
min > array[i]
55 > 55 => condition false
max < array[i]
55 > 55 => condition false
------------------------------------------------------
i=1; current array elements 55, 3, 2, 44, 100
min > array[i]
55 > 3 => condition TRUE UPDATE min =3;
max < array[i]
55 < 3 => condition false max=55
------------------------------------------------------
i=2; current array elements 55, 3, 2, 44, 100
min > array[i]
3 > 2 => condition TRUE UPDATE min =2;
max < array[i]
55 < 2 => condition false
------------------------------------------------------
i=3; current array elements 55, 3, 2, 44, 100
min > array[i]2 > 55 => condition false
max < array[i]
55 > 44 => condition false
------------------------------------------------------
i=4; current array elements 55, 3, 2, 44, 100
min > array[i]2 > 100 => condition false
max < array[i]
55 < 100 => condition TRUE UPDATE max =100;
------------------------------------------------------
Hope this will be helpful.
find more codes here C++ Simple Examples
Thank you.
ReplyDeletework hard
Delete