Thread: Complex C questions

  1. #1
    Registered User
    Join Date
    Nov 2010
    Location
    London, UK
    Posts
    18

    Smile Complex C questions

    I am ok at programming but I don't get how to compare the number back to check if it's composed of the same numbers in this problem. Even general tips would b super!,
    i have so far made a function for multiplying the user inputted number to obtain the number that will need testing for whether it is an anagram or notI am however aware that it has quite a lot wrong with it :P I have other code but this is a little annoying. Also how can I compare the numbers within the user inputted number (if that makes sense)

    Code:
    void multiplyNumber(int userInput)
    
    while(multiplyBy<9)
    {
    	int multiplyBy = 2;
    	userInput*multiplyBy = anagramToTest1;
    	multiplyBy++;
    
    }
    An anagram number is a number that can be multiplied by at least one single digit number (other than 1)
    to become an anagram of itself. Any anagrams formed by multiplying an anagram number by a digit are
    said to be generated by that anagram number. Two numbers are anagrams of each other if they can both
    be formed by rearranging the same combination of digits.
    For example:
    • 1246878 is an anagram number; multiplying by 6 generates 7481268 or by 7 generates 8728146.
    These numbers all contain a single 1, 2, 4, 6, 7 and two 8s.
    • 1246879 is not an anagram number.
    1(a)
    Write a program which reads in a single number (between 1 and 123456789 inclusive)
    and determines if it is an anagram number.
    If the number is not an anagram number you should output the word NO. If it is an
    anagram number you should output each single digit it can be multiplied by to make
    an anagram of itself.
    Last edited by alexbcg; 11-23-2010 at 01:28 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    The clue is in the first bullet point/example your teacher gave:
    These numbers all contain a single 1, 2, 4, 6, 7 and two 8s.
    You need to count the number of each digit (0-9) in the original and again in the product of original * some_digit. If the counts of all the digits match, you have an anagram.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You could create two arrays that keep counts of the digits in each number:
    Code:
    void count_digits(int num, int *values)
    {
      while(num)
      {
        values[num % 10]++;
        num /= 10;
      }
    }
    
    int main(void)
    {
      int num1 = 1246878;
      int num2 = 8728146;
      int num1values[10];
      int num2values[10];
      int i;
    
      count_digits(num1, num1values);
      count_digits(num2, num2values);
    
      for(i = 0;i < 10;++i)
        if(num1values[i] != num2values[i])
       {
          puts("Not an anagram number");
          break;
        }
    
      if(i == 10)
        puts("It is an anagram");
    
      return 0;
    }
    That would test one number against another anyway. I haven't tested the code so there might be errors. You get the idea though.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    We shouldn't be doing the entire homework assignment for people. As a poster of 2450+ posts you should know that.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by nonoob View Post
    We shouldn't be doing the entire homework assignment for people. As a poster of 2450+ posts you should know that.
    It's not the entire homework assignment. Look again.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    London, UK
    Posts
    18
    Quote Originally Posted by itsme86 View Post
    You could create two arrays that keep counts of the digits in each number:
    Code:
    void count_digits(int num, int *values)
    {
      while(num)
      {
        values[num % 10]++;
        num /= 10;
      }
    }
    
    int main(void)
    {
      int num1 = 1246878;
      int num2 = 8728146;
      int num1values[10];
      int num2values[10];
      int i;
    
      count_digits(num1, num1values);
      count_digits(num2, num2values);
    
      for(i = 0;i < 10;++i)
        if(num1values[i] != num2values[i])
       {
          puts("Not an anagram number");
          break;
        }
    
      if(i == 10)
        puts("It is an anagram");
    
      return 0;
    }
    That would test one number against another anyway. I haven't tested the code so there might be errors. You get the idea though.
    Wow thanks so much , so basically I was having trouble with having to compare the numbers you get from the multiple steps (like testing for division by 2-9) and seeing if they're anagrams, and this piece of code (forgive my stupidity but I'm a bit of a beginner at this :P ) will allow me to compare the individual numbers contained within the one imputed by the user to the product of my multiplication? If it's not too much to ask (I'm a bit of a beginner so it's probably obvious to someone like you) , how exactly does is it doing this?

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Basically, it's keeping a count of each digit of the number in an array. When count_digits() returns, values[0] will have the number of 0s in the number, values[1] will have the number of 1s, etc. Then you just compare the arrays.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    London, UK
    Posts
    18
    Quote Originally Posted by nonoob View Post
    We shouldn't be doing the entire homework assignment for people. As a poster of 2450+ posts you should know that.
    I'm really sorry if that's what it came across as, but I was just trying to understand this because I'm sort of a beginner, and on the contrary I do not want anyone to do it for me (that in itself serves no purpose) but merely a bit of help along with an explanation so I will not have to ask about the topic again and can do this myself instead of asking people.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    London, UK
    Posts
    18
    that is so awesome thank you so much you just made things so much clearer! I actually understand it now and can complete my program Ur the best and thanks so much for the help! (I am now a better programmer than I was 10 minutes ago :P )

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    London, UK
    Posts
    18
    Ok I'm getting somewhere here, however, to make my code more efficient and better, if for example I have created a function (just a small bit of my program) how can I , without copying it out many times, make sure that when the userInput is multiplied by the multiplyBy value, every time this loops it will store the result of the subsequent operation in different variable names (e.g anagramToTest1,2,3,4 etc...)



    Code:
    void multiplyNumber(int userInput)
    
    while(multiplyBy<9)
    {
    	int multiplyBy = 2;
    	userInput*multiplyBy = anagramToTest1;
    	multiplyBy++;
    
    }
    Last edited by alexbcg; 11-23-2010 at 01:23 PM.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    << !! Posting Code? Read this First !! >>
    Then edit your post if you still can, and make your code as readable as Post #3
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    London, UK
    Posts
    18
    Quote Originally Posted by Salem View Post
    << !! Posting Code? Read this First !! >>
    Then edit your post if you still can, and make your code as readable as Post #3
    Sorry I wasn't aware of this practice

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by alexbcg View Post
    Ok I'm getting somewhere here, however, to make my code more efficient and better, if for example I have created a function (just a small bit of my program) how can I , without copying it out many times, make sure that when the userInput is multiplied by the multiplyBy value, every time this loops it will store the result of the subsequent operation in different variable names (e.g anagramToTest1,2,3,4 etc...)



    Code:
    void multiplyNumber(int userInput)
    
    while(multiplyBy<9)
    {
    	int multiplyBy = 2;
    	userInput*multiplyBy = anagramToTest1;
    	multiplyBy++;
    
    }
    You could create an array and change your loop a bit:
    Code:
    int anagramsToTest[7];
    int i;
    
    for(i = 0;i < 7;++i)
    {
      multiplyBy = i + 2;
      ...
    }
    i would obviously become an index into the anagramsToTest array.

    Your original loop had a bug in it that would cause the same anagram calculation every time. You had a multiplyBy variable that seemingly masked another multiplyBy variable, and that new variable was set to 2 on every iteration of the loop and then used in the calculation. Of course, you also had the lvalue on the right hand side of the assignment operator so you would have gotten a compile error anyway.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. complex calculator
    By rafay_07 in forum C++ Programming
    Replies: 16
    Last Post: 11-07-2010, 04:40 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Why am I getting 'undelcared identifier' ???
    By Bill83 in forum C++ Programming
    Replies: 2
    Last Post: 02-15-2006, 01:00 PM
  4. arithmetic operator friend functions
    By linucksrox in forum C++ Programming
    Replies: 7
    Last Post: 02-06-2006, 11:39 PM
  5. Problem from texbook
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-26-2002, 04:55 AM

Tags for this Thread