Question regarding an assignment...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gwenky
    New Member
    • Feb 2008
    • 10

    Question regarding an assignment...

    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 finddigitsatpos iton 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:

    [CODE=cpp]int FindDigitAtPosi tion ( 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;
    }[/CODE]

    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 findnumberofdig its 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
    Last edited by Ganon11; Feb 29 '08, 02:57 PM. Reason: Please use the [CODE] tags provided.
  • Harinezumi
    New Member
    • Feb 2008
    • 32

    #2
    Originally posted by gwenky
    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 finddigitsatpos iton 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 FindDigitAtPosi tion ( 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 findnumberofdig its 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!

    Comment

    • gwenky
      New Member
      • Feb 2008
      • 10

      #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 FindDigitAtPosi tion ( 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.....

      Comment

      • Harinezumi
        New Member
        • Feb 2008
        • 32

        #4
        Originally posted by gwenky
        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 FindDigitAtPosi tion ( 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.

        Comment

        • gwenky
          New Member
          • Feb 2008
          • 10

          #5
          thanks so much!! it was the x/10 component that I was missing...now it all makes sense!

          Comment

          Working...