1) The following C++ program reads in a number, checks that it is a three-digit integer and, if it is, echoes it on the screen in the reverse order.
It uses a C++ function, called isolateDigits, which takes in a 3-digit integer number and returns the three digits that make up the original number, setting the actual parameter digit1 to the first digit in the number, digit2 to the second one and digit3 to the last.
int main()
{
int number, digit1, digit2, digit3;
cin >> number;
if ((number > 99) || (number < 1000))
{
isolateDigits( number, digit1, digit2, digit3);
cout << digit3 << digit2 << digit1;
}
return 0;
}
void isolateDigits( int num, int d1, int d2, int d3)
{
d1 = num / 100;
d2 = (num / 100) mod 10;
d3 = num mod 10;
}
2. The following C++ function, called priceInclVAT, takes a float parameter representing the original price (excl. VAT) and returns the new price including VAT
float priceInclVAT( float priceExclVAT)
{
const taxRate = 17.5;
VAT == priceExclVAT * (100 / taxRate);
return( priceExclVAT + VAT)
}
3. The following C++ function called findSmallest displays the smallest value currently stored in an array of integer values.
It takes two parameters. The first one, called tab, represents the array and the second one, called size, represents the number of integer values currently held in that array.
void findSmallest( const int tab, int size)
{
int num = 0;
for ( pos = 0, pos < size, pos++)
{
if ( num < tab[pos])
num = tab[pos];
}
cout << "\nThe smallest value is " << num;
}