Thread: magic number

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    3

    magic number

    Hey guys,

    just wondering if anyone could help me

    calculate the magic number take each letter in the person's full name and determine it's postion in the alphabet ( A is 1, Z is 26)
    calculate then sum of all the numbers. you most resolve the magic number to a single digit. therefore take 65, and add it's digits together ( 6+5=11). continue so until one digit remains 1 + 1 = 2)

    hope this makes sense

    here's the code i have

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    When your code is small like that, it's better to just put it in your post, but don't forget to use code tags (they format your code nicely so it's easy to read). Here's how:

    http://cboard.cprogramming.com/showthread.php?t=13473

    Now what exactly is it that you want us to help you with? Like what are the problems with your code? What's it doing that it shouldn't be doing, and what errors are you getting?

    edit:

    Just a couple of things I see right off the bat:

    Code:
    magic = fullname
    You're missing a semicolon, and depending on how fullname is declared (i.e. what type it is), you may not be able to do this.

    You're also missing a closing brace after you return a value.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    3
    ok It's part of an asignment they want me to write a code to figure out the magic number

    he discribes the how to get a magic number like this

    to calculate the magic number take each letter in the person's full name and determine it's postion in the alphabet ( A is 1, Z is 26)
    calculate then sum of all the numbers. for example "abe Dazz" would be 1+2+5+4+1+26+26=65 you most resolve the magic number to a single digit. therefore take 65, and add it's digits together ( 6+5=11). continue so until one digit remains 1 + 1 = 2).

    the fullname is declared as a string

    i'm not running it through a compiler, just gotta hand it in. what i think the code would be. I'm sure it's not perfect , just wondering if anyone could help. Sorry it's so hard to explain over the net, but thanks for any input

    Code:
    int magicNumber()
          {
           char chrLetter;
           int x=0;
           int length;
           int i = 0;
           int magic = 0;
           int sum = 0;
                
                magic = fullname;
                length = fullname.length();
          
                while(sum < 10)
                {
                  for ( i = 0 ; i < length; i ++ )
                  {     
                      chrLetter= fullname[i];
                      chrLetter= toupper(chrLetter);
                      magic= (chrLetter - 64 ); 
                      number = (int)magic[i];
                      sum = sum + number;
                  } 
                }
    
    
          return 0;

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Well when you convert a character to an integer, A is not 1. It's something around 96 (assuming you're using ASCII) because of the way the character encoding works. Look into that. I'm afraid I can't be specific because I've never used C++ strings much. Once you've converted the string into an array of integers, I would use a single loop to cycle through summing each one. I'm not sure where this, "while (sum < 10)" thing is coming from. I think you're getting the logic a little confused with what the constructs actually do. Once all the numbers are summed, I'd use the modulus (%) operator to convert that number to an array of single digit numbers, sum them, and repeat until the number was less than ten (maybe that's where your while loop comes from - but it still needs to be modified). Then I'd return that number, not 0. Then I would have a closing brace, which you're still missing.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    3
    ok great..I think that makes alot more sense,, really appreciate the help

  6. #6
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Interesting,

    Although the method you outline is sometimes referred to as the nine's digit method to check multiplication by hand.

    To illustrate say I have to multiply:

    Code:
    3425x23=78775  (by hand)
    How do I check if the answer is correct without actually doing the math again? Well, I use the nines digit method:
    Code:
    digit sum: (3425) 3+4+2+5= 14 = 1+4 = 5
    
    digit sum:(23)  2+3 = 5
    
    Multiply these together = 5*5 =25  
    
    digit sum of the multipliers: 2+5= 7
    
    Now finally digit sum of the answer:(78775) 7+8+7+7+5= 34 =3+4=7
    Since the digit sum of the multipliers and the answer is the same.
    The answer is likely to be correct. The reason why it is called the nines digit method is because every time to encounter a nine when adding you just ignore it.


    Try these couple of examples if you still don't believe me:

    14x5= 70

    2345*943=2211335

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    therefore take 65, and add it's digits together ( 6+5=11). continue so until one digit remains 1 + 1 = 2).
    Just a little trick here, as an application of what has been written by treenef.
    In base 10, to recursively sum the digits, you just need to take the number modulo 9, and if the result is 0, set the result to 9.
    In your case, you might loop through the string, mapping the characters to the required integers, and calculating the sum as you go.
    One you're done, you just make the modulo calculation using the sum, check for 0, and you're done.

    Of course, if you're working in say, base 16, then you would take the number modulo 15 (or 0x0F, to be consistent) instead, taking care to set the result to 0x0F if the result is 0.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    I have no intention of doing your homework

    However, here are some ideas to get you started.

    I have left out how to do the reducing algo. You do this since
    this is the most important part of the program.

    /************************************************** ****

    A program by treenef to calculate the magic
    number of a person's name

    ************************************************** ****/







    Code:
    #include <iostream>
    #include <string.h>
    #include <math.h>
    #include <ctype.h>
    
    int sub_prog(int);
    int reduce_to_digit_sum(int);
    
    using namespace std;
    
    int main()
    { 
        char name[81];
        cout<<"Enter a name:";
        cin.getline(name,81); //use cin.getline to account for white spaces
        
        int size_of=strlen(name);
        
        //Convert Capital to lower case
        for (int a=0; a<size_of; a++)
        {
            name[a]=(char)tolower(name[a]);
           
        }    
        
       
        char alaphabet[28]={"/abcdefghijklmnopqrstuvwxyz"};
        
        int num=0;
        for (int a=0; a<size_of; a++)
        {
            for (int b=1; b<27; b++)
            {
                if (name[a]==alaphabet[b])
                {
                     num=num+b; // accumulate 
                }
             } 
             
         }  
         
         cout<<num;
         int stop;
         cin>>stop;
         
         // call function sub prog send num into it
         sub_prog(num);
         int pause;
         cin>>pause;
         
     }    
         
    
    
    //function declaration            
    int sub_prog(int digit_sum) 
    {    
           
           if (digit_sum>=10)
           {  
               //call function reduce to digit sum
               //passing 'digit_sum' into it
               reduce_to_digit_sum(digit_sum);
           }    
               
           else
           cout<<"Your magic number is: "<<digit_sum;    
               
               
    }        
                
    //function declaration    
    int reduce_to_digit_sum(int digit_sum)
    {
        //128
        //1+2+8 =11 =>send this to sub_prog
        // This is the algorithm that you need to think up
        // No more help.
        // Tip use the modulus function '%'
        // Note:If I do this for you then I am effectively doing 
        // your homework!!! :)
        
        
        sub_prog(digit_sum); // looping back to sub_prog
    }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I have left out how to do the reducing algo. You do this since
    this is the most important part of the program.
    Agreed.

    That said, andygales, I think that you should try out using treenef's framework (for practice), but in the end using modulo 9 directly would be more efficient.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  3. 4 x 4 Magic Square
    By amorvisa in forum C Programming
    Replies: 2
    Last Post: 10-17-2007, 11:27 PM
  4. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM