Find maximum and minimum number in array c++ code

Leave a Comment
This Tutorial contains:
  • 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
Concept used:

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
c++ code

#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;
int main()
{
const int size=50;
// array with size of 50
int array[size];
// for random numbers
srand(time(0));
    for(int i=0;i<size;i++){
    // Initializing Array number is less than 100
    array[i]=rand()%100;
    // Displaying array value
    cout<<array[i]<<endl;
    }
            // initializing max, min
                int max=array[0];
                int min=array[0];
/* scanning array to find
   minimum and maximum
   number  */
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];
      }
}
       // displaying output
        cout<<"Maximum Number is :"<<max<<endl;
        cout<<"Minimum Number  is:"<<min<<endl;

return 0;
}


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

How to use rand(), srand() and time() functions to generate pseudo random numbers in c++ examples

Leave a Comment
This tutorial contains
  • rand(); srand() and time() concept and how to use these functions
  • srand() example with rand() with in a for loop
  • rand() srand() and time program code with for loop
Random numbers are not truly random why?
Computers actually cannot generate truly random numbers because they are not human. Computer follows some algorithms to generate random numbers to make it more random.

 rand() with srand() function
 rand() function is a function in header file stdlib.h which simply returns a random number.
 srand();
prototype:  void srand(unsigned int seed);Return type: void
Takes an unsigned int argument as seed for example srand(10); which simply changes the algorithm

Lets discuss it with code examples

srand(); will  just change the algorithm if we run the program again and again it will generate the same random number with a same pattern

like below program




#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
srand(10);
for(int i=1;i<=5;i++)
  cout<<rand()%10<<endl;
return 0;

}

it generates 1,9,2,4,7 again and again
lets change the code with a for loop srand(i);


#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
  for(int i=1;i<=5;i++){
  srand(i);
  cout<<rand()%10<<endl;
  }
return 0;
}

after running it again and again it is also generating a sequence again and again
like 1,5,8,1,4

now lets use srand(); two times in a program and see what happened

#include<iostream>
#include<cstdlib>
using namespace std;
int main(){
  srand(1);
  int random_number=rand();
  cout<<random_number<<endl;
srand(2);
random_number=rand();
cout<<random_number<<endl;
return 0;
}

sample output
41
45

so change the value of srand() changing the random number.

time() function
  • This functime comes from a header file which is <ctime.h>
  • It simply allows us to get the system or computers current time in seconds
  • Its is very use full function with srand() we will call it within srand() and will get different random number every time because seconds value is changing  
for example this program is almost generating a random number

time() function c++ code


#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;

int main()
{
srand(time(0));
  for(int i=1;i<=5;i++){
  cout<<rand()%10<<endl;
  }
return 0;
}


Read More...

Random number generator in c++ programming

Leave a Comment
This tutorial contains
  • How to generate a random number using rand()
  • Simple example using rand() function
  • Generate random number in a specific range dice example

To generate a random number in c++ program first we have to include a header file which is
#include<cstdlib> because we have to use a function from that library which is “rand();” this functions simple returns a random number

Let use that function

#include<iostream>
#include<cstdlib>
using namespace std;
int main(){
  cout<<rand()<<endl;
return 0;
}


It will return a random number for example
41
 Let use a loop and generate some more random numbers using that function

#include<iostream>
#include<cstdlib>
using namespace std;
int main(){
 for(int i=1;i<=10;i++)
    cout<<rand()<<endl;
return 0;
}

It will generate 10 random  numbers
What if we want to generate random numbers with in the specific range like we want to generate random numbers between 1 to 10 or 50 to 100

for example range is 1 to 10

We think something like that cout<<rand()%10;

How it will work
It will take a random number divide it by 10 and return us the number the number can be from 0 to 9
After getting the output we see that if we add 1 in the result value we can get our required range values
So we will change it like that << 1 + ( rand() % 10 ) ;

Lets take another example of a dice it can generate numbers 1 to 6 randomly

 we will simple something like that
But is it write or not


for(int i=1;i<=6;i++)

    cout<< rand()%6<<endl;


And there is a problem it will return us number from 0 to 5 to make the logic of our program we will change it like that
for(int i=1;i<=6;i++)

    cout<< 1+ (rand()%6)<<endl;

So this will work fine in this case 0 is eliminate and whatever number will return 1 will be added in it like if 0 is generated it will result as 1 if 5 is generated it will return us 6
  
Hope this tutorial will help to understand the basic concept of random number
Read More...

Maximum or largest number in array c++ code

Leave a Comment
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...