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; 
}

Recommended Answers

All 3 Replies

>>what's wrong with these 3 codes
I don't know -- what do YOU think is wrong with them?

As for the second program, the defulat data type of a const is an integer, which has no decimals. So this is how to declare the const const float taxRate = 0.175; Then the calculation becomes very simple return priceExclVAT * (taxRate + 100.0F);

As for the first code:
instead of this: if ((number > 99) || (number < 1000)) I think you mean this: if ((number > 99) && (number < 1000)) which means that the number has to be between 100 and 999.

also: d2 = (num / 100) mod 10; The mod operator is written as a % not as 'mod', so change it to: d2 = (num / 100) % 10; You also might want to pass the parameters to the function by reference. Now a value is calculated, but thrown away on exiting the function...
Adak has made a very useful post on the subject of functions

In the third example you are checking for the largest number in the array not the smallest;

Try preloading your check value num with the contents of the first array element.

int num = tab[0];

        for ( pos = 0, pos < size, pos++)
	{
		if ( num > tab[pos])
			num = tab[pos];
	}
	cout << "\nThe smallest value is " << num;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.