To determine the largest of any group of numbers, we must first determine the largest of any two numbers. For that we use the following simple algorithm:
If number A is greater than number B, then return number A otherwise return number B.
In C++ we can encode this algorithm using the following template function:
T& max( T& a, T& b) { return( a>b?a:b ); }
Using this one basic function we can build more complex functions to return the largest of any quantity of numbers. For instance, to return the largest of any three numbers, we would use the following:
T& max( T& a, T& b, T& c ) { return( max( max( a, b ), c )); }
To return the largest of any four numbers, we would use the following:
T& max( T& a, T& b, T& c, T& d) { return( max( max( a, b, c ), d )); }
We could continue in this fashion, but there is a much simpler approach: place all the values in a vector and return the maximum value of the vector. The following example demonstrates this with a vector containing 10 random values:
#include
#include
#include
template
T& max(T& a, T& b ) { return( a>b?a:b ); }
template
T max( std::vector
{
std::vector
int iResult = *it;
while( ++it != v.end() )
iResult = max( iResult, *it );
return( iResult );
}
int main()
{
srand(( unsigned ) time( NULL ));
std::vector
printf( "Array: " );
for( std::vector
{
*it = rand() % 100 + 1;
printf( "%d ", *it );
}
printf( "\n");
printf( "Largest: %d\n", max(v) );
return( 0 );
}
8798797
start input A & B if A>B print A is greatest if B>A print B is greatest stop james ola writes.....SOT.
what is algorithm and its use there and analyze an algorithm
You can represent an algorithm by three different ways: 1. Pseudo Code 2. Structured flow charts 3. Actual code
write an algorithm to compute the weekly average rainfall given the daily rainfall for four weeks
Yes. But why?
Step1- Read a,b,c. Step2-if a>b continue step 5. Step3- b>c then print “b is the largest " and continue step 7. Step4- continue step 6 Step5-if a>c then print “a is the largest " then continue step7. Step6- print “z is largest". Step7- end.
That would be 999.
8798797
algorithm is a way to solve your problem
The largest single digit in our numerical system is 9. Once you write 10, you have two digits.
In the decimal system, the largest digit in any place is 9.
The following algorithm works for any number of integers: Assume the first number is the maximum - maximum = (first number). Compare your assumed maximum with the second number. If the second number is larger than the assumed maximum, replace the old assumed maximum with the second number. Repeat for the third number, for the fourth, etc. - always copying the nth. element to the assumed maximum if you find one that is larger than your previous maximum.
read num1 read num2 sum = num1 num2 print highest value
start input A & B if A>B print A is greatest if B>A print B is greatest stop james ola writes.....SOT.
666 - which in base 10 would be 294+42+8=344
The largest number with three digits is 999.