Hello! I need help with this program. I have to output the geometric mean of the first 2 digits and last 2 digits of string
for example if user input "84834992" it should take 84 as a and 92 as b
and output geo mean
#include <iostream>
int last_two_digits( int n )
{
if( n < 0 ) return last_two_digits(-n) ;
elsereturn n % 100 ;
}
int first_two_digits( int n )
{
if( n < 0 ) return first_two_digits(-n) ;
while( n > 100 ) n /= 10 ;
return n ;
}
void get_input( int& a, int& b )
{
std::cout << "enter an integer: " ;
int number ;
// for now, we will assume that the user does enter an integer
// and not some nonsense like "%&%&BGUGUIG"
// as it stands, invalid input would yield 0,0 for a,b
std::cin >> number ;
std::cout << "number == " << number << " " ;
a = first_two_digits(number) ;
b = last_two_digits(number) ;
}
int main()
{
int a ;
int b ;
get_input( a, b ) ;
std::cout << "a == " << a << " and b == " << b << '\n' ;
// compute and print the geometric mean of a and b
}
Valid input would be rejected: eg. -45678 or +2345 (also valid numbers entered with leading or trailing white space.)
Would give wrong results if the input is 00123456
Alternatively, if "first 2 digits and last 2 digits of string" is interpreted literally, input of 5ab6efgh78i9 should be accepted and should yield 56 and 89.
To add further to the apparent impossibility of this exercise who can say the first two characters are decimals? And what is the number base if they aren't? How do you work out the sqrt(5a * i9) in an unknown base?
Is there such a thing as a complex geometric mean? I imagine there is but I haven't checked.
No wonder Western civilization is in decline with such badly framed educational exercises :(
See http://www.oxfordmathcenter.com/drupal7/node/18 on what a digit is. Hex digits are mentioned a few paragraphs down in the article. The point being a digit isn't necessarily a decimal.
You left it hanging about complex geometric means. I'll check one day into this reference and see where it goes, I won't be too fussed if it goes nowhere :) http://planetmath.org/complexarithmeticgeometricmean
Let's not get bogged down gunner because I think most people would accept your response as what is required of students even though there is nothing wrong with going that extra mile (and a half! even) in being absolutely purist and looking at outliers. After all it's part of the deal in good robust programming which mary9734 is probably yet to experience and I sometimes fail on miserably.
'ma' is a number in some numbering system - pick which one you like because in itself it says nothing about it's base beyond being hexadecimal if we follow the normal convention. There's a hint it might go to at least 'y' as the final digit but not even that is guaranteed. However, if the convention is followed conversion to another base is easily done.
I agree that digits, as numerical symbols, aren't negative in themselves, but they can be used in the appropriate sequence and configuration to represent -ve numbers.
In this problem strings can handle the hexadecimal numbering system since any problem considering this numbering system would state clearly how to handle A-F if they appear in the string i.e as digit or alphabet or upper case as digit, lower case as alphabet &c
I would say strings can handle any numbering system whatever way the digits, symbols or cases are arranged. It's fundamental number theory gunner. Hex is understood to be acceptable in upper or lower case regimes, albeit perhaps not mixed.
And besides, upper and lower case are convenient ways to extend a number base system. Use the Greek alphabet or any other squiggles to extend even further if you have a need/application/whatever. It's open-ended.
The beauty about this OP problem is that it hasn't been made clear 'only decimals need apply' so as esoteric as it is if this is a beginner exercise, JLBorges is right in making sure in the interests of robustness
I assume you comment is also in the vein of recognizing now that alpha characters (symbols) can be digits in this context. :)