Hello, I'm really new to C++ and this forum, but I have read the "rules" regarding homework and I'm just looking for some guidance. I need to create a function that asks for the user to input an integer, finds the number of digits and digits at position, sum digits, average digits and find max and min from the number. What I am really trying to create is the finddigitsatpositon function. The rest I have either done or understand.
So, the following is what I have...keep in mind the user will have inputted an integer:
int FindDigitAtPosition ( int x, double counter )
{
for ( i = 1; i <= counter; ++i )
{
x = x % 10;
if ( max <= x )
{
max = x;
}
else
{
max = max;
}
cout << x << ":" << max <<endl;
}
return max;
}
What I cannot seem to understand is the % and loops. What I am wanting to do is create a loop that will loop as many times as my counter (which was a part of my findnumberofdigits function) tell my loop to do so. This part work. The next thing ....I think is that I am wanting to catch the remainder of x % 10 on each loop and compare it to max (which is initialize to 0 ) and keep the larger number. What I know isn't happening is my x only captures the initial remainder from the last digit. Any suggestions???
Thanks so much....
Kay
Hi,
I'm not sure what you want to achieve with this function, but if I'm correct you want to split a multi-digit number into one digit numbers and get the largest digit. If that's right, I think you're on the right track.
However you overwrite the input number in each loop with
x = x % 10;
% gives the integer remainder of a division, e.g. from 14 this will give 4, 29 % 10 => 9. You should store the remainder in another variable, and divide your original number with 10 after that.
Another suggestion: if count is the number of digits in the multi-digit number, it doesn't have to be a double (a floating point number), it can be an integer as well (even better, an unsigned integer).
And one more thing: the line
max = max;
should be removed: it's assignment to self (bad thing most of the time), and doesn't do anything anyway.
Good luck with the assignment!