# magic number

• 04-04-2005
andygales
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
• 04-04-2005
sean
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:

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.
• 04-04-2005
andygales
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;

• 04-04-2005
sean
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.
• 04-04-2005
andygales
ok great..I think that makes alot more sense,, really appreciate the help
• 04-05-2005
treenef
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
• 04-05-2005
laserlight
Quote:

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.
• 04-05-2005
treenef
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

}

//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

sub_prog(digit_sum); // looping back to sub_prog
}

• 04-05-2005
laserlight
Quote:

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.