470,571 Members | 2,417 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,571 developers. It's quick & easy.

Question regarding an assignment...

10
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:

Expand|Select|Wrap|Line Numbers
  1. int FindDigitAtPosition ( int x, double counter )
  2. {
  3.        for ( i = 1; i <= counter; ++i )
  4.        {    
  5.     x = x % 10;
  6.  
  7.     if ( max <= x )
  8.     {
  9.                 max = x;
  10.     }
  11.     else
  12.     {
  13.                max = max;
  14.     } 
  15.     cout << x << ":" << max <<endl;
  16.     }
  17.  
  18.     return max;
  19. }
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
Feb 29 '08 #1
4 1244
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!
Feb 29 '08 #2
gwenky
10
I agree that my x is just resetting each loop through...so I put in "13" the output would be :

3
3
3

...this is where I'm not sure how to create a variable to capture that digit and continue on...

int FindDigitAtPosition ( int x, double counter )
{
for ( i = 1; i <= counter; ++i )
{
x = x % 10;

if ( max <= x )
{
max = x;
}
cout << x << ":" << max <<endl;
}

return max;
}


would it look something like

while ( i < counter)
{
digit = x % 10;

if ( max <= x )
{
max = x;
}
}
????

Thanks.....
Feb 29 '08 #3
I agree that my x is just resetting each loop through...so I put in "13" the output would be :

3
3
3

...this is where I'm not sure how to create a variable to capture that digit and continue on...

int FindDigitAtPosition ( int x, double counter )
{
for ( i = 1; i <= counter; ++i )
{
x = x % 10;

if ( max <= x )
{
max = x;
}
cout << x << ":" << max <<endl;
}

return max;
}


would it look something like

while ( i < counter)
{
digit = x % 10;

if ( max <= x )
{
max = x;
}
}
????

Thanks.....
Yes, something like that, but declare digit before the while loop (e.g. int digit;) and you should divide x with 10 after you have the digit. Also, you need to increment i somewhere in the loop if you wan't it to finish.
Mar 1 '08 #4
gwenky
10
thanks so much!! it was the x/10 component that I was missing...now it all makes sense!
Mar 2 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by rahul8143 | last post: by
52 posts views Thread by junky_fellow | last post: by
9 posts views Thread by kiran.agashe | last post: by
5 posts views Thread by Remco van Engelen | last post: by
1 post views Thread by Nathan | last post: by
30 posts views Thread by Logos | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.