Di Posting Oleh : Simple Learning
Kategori : array cpp_tutorial function largest number smallest using
This Cpp tutorial contains
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
Cpp Code
Another Example without Function more simple
Find Maximum and Minium Number in Array Cpp Code
More Cpp Program Tutorials
C++ Simple Examples
- Cpp array with random integer values
- Passing array to function
- if statement with in for loop
- Calculate result in function and display
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
- #include <iostream>
- #include <stdlib.h>
- using namespace std;
- void FindMaxMin(int *array, int size)
- { int min,max;
- min=max=array[0];
- for(int i=0;i<size;i++)
- {
- if(array[i]<min)
- min=array[i];
- else if(array[i]>max)
- max=array[i];
- }
- cout<<"Minimum Number = "<<min<<endl;
- cout<<"Maximum Number = "<<max<<endl;
- }
- int main()
- {
- int array[10];
- for(int i=0;i<=9;i++)
- {
- array[i]=rand()%1000+1;
- cout<<"array ["<<i<<"]"<<"= "<<array[i]<<endl;
- }
- FindMaxMin(array,10);
- return 0;
- }
Another Example without Function more simple
Find Maximum and Minium Number in Array Cpp Code
More Cpp Program Tutorials
C++ Simple Examples
0 Response to "Cpp Function to Find Largest and smallest number in array"
Post a Comment