Weird counting system

Pages: 12
So i've got a problem from a computer science class. I can't figure out the algorithm. Basically it goes like this.
if i have a system where i have an "alien aged 3" and he has 3 fingers on each hand left side is -3 - 2 - 1 his head counts as 0 and the right hand is 1 2 3
I need to sum up inputs

So I take his (age * 2 + 1) and that gives you the 'base'

Age: 3 alien = base 7

-3 -2 -1 0 1 2 3 would be the counting system

so lets say i have this sequence of inputs from the user here's what the answer is when you add in this strange number system, keep in mind the user can only put 6 integers in.

6 variable input 1 -2 3 1 0 -1
6 variable input + 2 0 3 3 -1 -3
-------------
= 3-1 0 -3 -2 3

so -1 + -3 = 3 the way this works is you count -3 from -1 and it goes to the 3 so 3 carry -1 to 0 + -1 which is -2 now i get how that works my problem is this

I need to make an algorithm where i don't use any loops any if statements and just use variables and pure math to solve this. Does anyone have any idea how to do this ? i would greatly appreciate the help.
Last edited on
First try and work out what it does. (I should use output tags round your description to make things line up.)

Work from the right, as in normal columnar addition.
-1 -3 = -4. This is outside range, so add 7 (giving 3) and compensate by carrying -1 to the next column.

0 -1 and the carry -1 gives -2. This is OK.

1 + 3 = 4. This is outside range, so subtract 7 (giving -3 : is your model answer correct?) and compensate by carrying +1 to the next column.

3 + 3 and the carry 1 gives 7. Outside range, so subtract 7 (giving 0 : I think you've transposed numbers in your model answer) and carry +1.

-2 +0 and carry 1 gives -1. This is OK.

1 +2 =3. This is OK.


Algorithm: put your numbers in some form of array of integers (e.g. vector or valarray). The [0] index corresponds to rightmost "digit". Add up corresponding elements. If answer is greater than 3 then subtract 7 and carry 1 to next column. If answer is less than -3 then add 7 and carry -1 to next column. Be careful if either your numbers have different numbers of digits or if you have to carry more than one.

CHECK YOUR MODEL ANSWER, PLEASE.

If you can't use any if statements then you will have to do some clever work with % and / operators to work out the carried quantities. Difficult to see a tidy and extensible way of doing it without loops, although you could just about do it if you are allowed whole-array operations (e.g. in a valarray).

BTW, if your alien only works with -3 to 3 then you will be driving him up the wall by asking him to put 6 integers in!
Last edited on
Number systems work by assigning N symbols to each digit. We have an N=10 system, where our symbols are:

    0 1 2 3 4 5 6 7 8 9

N is called the radix or base.
The symbols we call digits.

The age is just a thing to mess with your head. What is being said is: for this problem use base N. (Where N = age * 2 + 1).

Then the digits are -age ... age: -3 -2 -1 0 1 2 3.
You could easily write it 0 1 2 3 4 5 6.
Or A B C D E F G.

So you aren't inputting variables, you are inputting digits.


At this point you need to think about number conversion — which is something you should have been covering in class. To build a number, add and multiply.

    678

is:

    (((((0 * 10) + 6) * 10) + 7) * 10) + 8

You could write that in code as:

1
2
3
4
5
6
7
8
9
10
11
12
13
int result = 0;

// first digit
result *= 10;
result += 6;

// second digit
result *= 10;
result += 7;

// third digit
result *= 10;
result += 8;

Take a look over those two again and see if you can get why that 0 * 10 is in there. Hint: repetition == loops.

Knowing your alien's age gives you an advantage in creating a value: you can convert the alien digits to useful zero-based digits:

    -3 -2 -1  0  1  2  3
              ↓
     0  1  2  3  4  5  6

How did we do this? Give a think. Don't slap yourself in the head too hard when you get it.

Right now is a good time to convert the examples in your post from alien digits to human zero-based digits.


Add your two numbers. The computer can do this the normal way.

int x = n + m;

On paper, you'll have to remember that it is base 7, so 10<sub>7</sub> == 7<sub>10</sub>. (Or, in alien aged 3, that's “-2-3”.)


Once the computer has added the two numbers you need to convert x back into the alien digit system. This comes in the same two parts that converting it from the alien system did, just in reverse order.

Get the last digit (using remainder % base) and adjust to from the human zero-based digit to the alien digit.
Divide the number by the base.
Repeat until your number is zero.

There is exactly one edge case: if the number is zero at the start, forget all the dividing and remaindering, just print an alien zero: -3.


I know this is a bit to take in. It is hard enough to understand now numbers work without the wonky alien digit stuff. Keep in mind, though, that the alien digit stuff is ONLY used to do two things:
  • compute the radix
  • change digit representations
This stuff only happens at the beginning and end of your conversion-add-conversion.


For an additional consideration, consider hexadecimal:

  • radix = 16
  • digits = 0 1 2 3 4 5 6 7 8 9 A B C D E F
  • digit values = 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

So "7C" is:

    7 12

which is:

    (((0 * 16) + 7) * 16) + 12

Good luck!
closed account (48T7M4Gy)
Still a few things to sort out - 6 digits, reverse order and shift to -3 to +3 range ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

int ADC(int x, int y, int &CO) //ADD WITH CARRY
{
    int total = (x + y + CO);
    CO = total / 7;
    return total % 7;
}

int main()
{
    int a0 = 0, a1 = 0, a2 = 0; // 1st
    int b0 = 0, b1 = 0, b2 = 0; // 2nd
    int t0 = 0, t1 = 0, t2 = 0, t3 = 0; // TOTAL
    
    
    std::cout << "Enter  a0 a1 a2: ";
    std::cin >> a0 >> a1 >> a2;
    
    std::cout << "Enter  b0 b1 b2: ";
    std::cin >> b0 >> b1 >> b2;
    
    int CO = 0;
    t0 = ADC(a0, b0, CO);
    
    t1 = ADC(a1, b1, CO);
    t2 = ADC(a2, b2, CO);
    
    t3 = CO;
    
    std::cout << a0 << a1 << a2 << '\n';
    std::cout << b0 << b1 << b2 << '\n';
    std::cout << t0 << t1 << t2 << t3 << '\n';
    
    return 0;
}


Enter  a0 a1 a2: 2 2 5
Enter  b0 b1 b2: 6 5 5
225
655
1141
Program ended with exit code: 0
@kemort
What about input of more than three “digits” (like “1 -2 3 1 0 -1”)?
What about an alien aged 12?
closed account (48T7M4Gy)
@Duthomas
What about ... ? These are questions you may well ask. Mine is a proof of concept only. Of course @lastchance chimed in first so its an extension of that in code if you like. I chose 3 digit numbers because I'm lazy an I realized after that 2 digit numbers were sufficient but I was too lazy to delete a3, b3 and t4 etc. I felt like showing arrays and therefore loops or ifs are'nt needed.

With yours, I obviously didn't incorporate that, that's @OP's problem. Maybe the shift comment was too subtle or cryptic perhaps? Maybe an exercise for you?

Aliens don't grow old - they're out by 4
The add with carry is perfectly fine, and works well.

The issue I have is more a matter of addressing the purpose of an assignment like this.

(The purpose is the same as other number theory conversion homeworks. This one just adds the alien stuff in for the likely purposes of: (1) force the student to think about how numbers actually work and (2) make it harder for students to cheat with the very common assignment.)
closed account (48T7M4Gy)
I think the fact that @OP asked for help on where to even start and the comprehensive set of replies fom all of us, even to the extent that some information in the question should be ignored, gives some indication of purpose. Of course, the bigger question for divergent thinkers is how does @OP know that we aren't part of the red herring too and the correct answer is something else completely.
Ah, you're an existentialist.

No, no collection of replies has any effect on the purpose of the assignment. The professor has a specific set of objectives that must be satisfied through his course; often these are set by by the school/university as contract terms for his employment, either directly (university board says you must teach this and it is in your contract) or indirectly (university board appoints a subject steering committee which designs and reviews course objectives and your contract says you will follow those objectives).

Further, humans familiar with a pattern are particularly adept at recognizing that pattern. Humans who are not familiar with relevant patterns simply cannot recognize it.

Teaching number representation is a very common pattern with very common objectives. This particular assignment has an added dimension of uniqueness to it (by shifting the input digits); otherwise it is in all respects absolutely normal.

The objectives are typically along these lines:
  • Demonstrate ability to construct an operable C++ program.
  • Demonstrate ability to comprehend objectives and construct a working solution.
  • Demonstrate understanding of radix and how it relates to digit place value.
  • Translate that understanding to the ability to construct and destruct/analyze numbers by digit.

Softer objectives may exist, including:
  • Understand that number representation is not the same as number value; number representation may change (as it is affected by radix); number value does not change.
  • Understand that number representation may affect the ease with which operations may be performed on the number
  • Understand that number representations are polynomial
  • Understand that digit representations are arbitrary

Understanding information theory is absolutely and directly related to some number theory. You cannot have a firm grasp on computing otherwise.

For OP’s assignment, I see nothing which should be ignored. The difficulty for the novice is separating things into their distinct parts: digit conversion, base conversion, expression evaluation (in this case, addition), base conversion, digit conversion.

Further, while it is always possible that anyone can totally mistake the purpose and extent of an assignment — and I have done this — that error is usually due to OP’s presentation. In other words, we are getting OP’s assignment criteria second-hand, which means that it is filtered through OP’s present understanding, or misunderstanding.

That said, as someone who is very familiar with the pedagogy from which questions like these come, and also having seen very many variations of the assignment, it is entirely reasonable to believe that my interest in helping OP understand the principles behind the assignment also align at least somewhat with his professor’s. OP may not know that, but the better OP understands the assignment the better chance he has of being able to solve it according to the (unknown) instructions given him.
Hello, @Duthomas

I am not quite following your first answer in this thread - which is niggling me, obviously.

You posit a simple mapping
    -3 -2 -1  0  1  2  3   (alien)
              ↓
     0  1  2  3  4  5  6   (human)

This is all well and good if we simply have the number representation, but I am not sure that it describes the number arithmetic. And if the arithmetic is not isomorphic then I'm not sure that you can just do the addition in a normal number base.

The reason is that 0 is a neutral element in normal (base 7 or 10) arithmetic - add it to a number and nothing will change. However, the alien-mapped value, -3, is not a neutral element. Add it to something and that something will change.

I believe (as does the OP) that when the alien adds
-1 and -3
he will get "-4", or with his limited set of symbols, 3 carry -1; that is:
-1 3

I wonder if you could explain that for me in your version of the arithmetic.


I am also slightly stumped by the requirement that one cannot use if statements or loops. I can just about see a way to do it with the ternary operator and recursion, or possibly something variadic, but is that cheating?

Never mind, in just one more year this alien will be able to grow enough fingers to recognise that the answer to life, the universe and everything is ... 42. All will then be solved.

(Could somebody check the OP's model answer please. EDIT: now impossible; he seems to have removed it. How frustrating! Did anyone keep a copy, or still have it cached?)
Last edited on
Well, seems as if OP has very ignorantly removed his question. I don't know if I wanna try to help anymore. You can only get kicked in the face so many times...


@lastchance
You are confounding value and representation. The digits representing a number can be anything. Here's a base 6 system:

    ♟ ♞ ♝ ♜ ♛ ♚

In this system I can add zero (♟) to anything to get the same value:

    ♞ + ♟ = ♞

In this base 6 system, our value ‘ten’ is ♞♜.

We humans, however, are typically used to the Arabic numeral system. Hence:

    ♟ ♞ ♝ ♜ ♛ ♚
           ↓
    0  1  2  3  4  5
 

And

    1 + 0 = 1

And ‘ten’ is 136.


In OP’s alien system, “-3” is zero. Hence:

    -2 + -3 = -2

(That is, 1 + 0 = 1.)

The bad property of this system is our prior associations. The good property is that the digit to digit value conversion is simply a +3 on each digit — which is something very easy to do in the assignment (students don’t have to do some other symbol→value table lookup).

In other words, “However, the alien-mapped value, -3, is not a neutral element” is not true. It is an assumption on your part because the aliens are using our natural digits in a way that makes no sense to us. In ‘alien age 3’, “-3” is zero.


“I am also slightly stumped by the requirement that one cannot use if statements or loops.”
I do not recall reading that anywhere in the OP. It is, of course, entirely possible that I missed it. But as the OP has decided that only he gets to have useful help here on a public forum I suppose we will never know. It would have been a really horrible requirement to cripple OPs ability to solve the assignment.


For what it is worth, I prefer using things like A B C D ... because it is a lot easier for students to grasp the idea of digit representation ≠ digit value while remaining very easy to transform that representation to a digit value. Using -3 for zero is very confusing at best.

And kudos for asking an intelligent question!
Thanks, @Duthomas, and sadly my browser cache does not retain the OPs post.

My point was that -3 is not the alien's neutral element. If he added -1 and -3 in his representation, he did not get -1; instead he got +3 and carry -1.

With your example, if the alien added -2 and -3 he would not get -2. In his arithmetic he would get, at least according to the OP's rules, +2 and carry -1.

Perhaps the OP has gone back to check his original question and would be decent enough to reinstate it.

Overloading the + operator might have been a really neat way of doing this problem.
Last edited on
closed account (48T7M4Gy)
@Duthomas, Why are you directing all that at me?
Well, seems as if OP has very ignorantly removed his question.

This happens sometimes. A reason for that might be that is it homework the the OPs don't want to be found out. In other forums newbies are not allowed to edit their posts until they have reached a certain numbers of posts. Would be good if we had sth. similar here.
Once I reported a post but nothing happend so I guess we have to live with it.
@kemort
Er... because you asked...?

@lastchance
You aren’t listening.

Alien “-1” is not equal to -1.
Alien “-3” is not equal to -3.

Alien “-3” is equal to ZERO.

Just because the aliens are using a minus sign and an Arabic digit 3 means absolutely nothing.

    “-2” (value=1)   +   “-3” (value=0)   =   “-2” (value=1)

The aliens could have just as easily used chess pieces, or emojis. Instead they chose to use minus signs and digits. To an alien,

    “-3” means 0
    “-2” means 1
    “-1” means 2
    “0” means 3
    “1” means 4
    “2” means 5
    “3” means 6

It doesn’t have to make sense to us. Minus signs and Arabic digits are just scribbles on a paper, or an arrangement of light on a computer display. The value we give those scribbles can be anything.

Thus, according to a three-year-old alien, -2 + -3 = -2.

According to us, it’s gibberish.

[edit]
We do the very same thing with hexadecimal digits.

    D + 7 = 14

Someone who knows their alphabet may argue that “D + 7” makes absolutely no sense. If it did, shouldn’t D + 7 be K?

No, because “D” means 13.

    “D” (value 13)   +   “7” (value 7)   =   “14” (value 1*16+4 = 20)

Hope this helps
Last edited on
closed account (48T7M4Gy)
@kemort
Er... because you asked...?


I'm not aware of any question I raised with you @Duthomas.
Okay. My bad.
The "evil" Google does not forget:
corestorm wrote:
So i've got a problem from a computer science class. I can't figure out the algorithm. Basically it goes like this.
if i have a system where i have an "alien aged 3" and he has 3 fingers on each hand left side is -3 - 2 - 1 his head counts as 0 and the right hand is 1 2 3
I need to sum up inputs

So I take his (age * 2 + 1) and that gives you the 'base'

Age: 3 alien = base 7

-3 -2 -1 0 1 2 3 would be the counting system

so lets say i have this sequence of inputs from the user here's what the answer is when you add in this strange number system, keep in mind the user can only put 6 integers in.

6 variable input 1 -2 3 1 0 -1
6 variable input + 2 0 3 3 -1 -3
-------------
= 3-1-3 0 -2 3

so -1 + -3 = 3 the way this works is you count -3 from -1 and it goes to the 3 so 3 carry -1 to 0 + -1 which is -2 now i get how that works my problem is this

I need to make an algorithm where i don't use any loops any if statements and just use variables and pure math to solve this. Does anyone have any idea how to do this ? i would greatly appreciate the help.
Thanks, Keskiverto!
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <iomanip>

int ADC(int xx, int yy, int &CO, int* d, int* a, int base) //ADD WITH CARRY
{
    int SUM = ( *(&d[3]+xx) +  *(&d[3]+yy) +  CO );
    int REM = SUM % base;
    CO = SUM / base;
    
    return a[REM];
}

int main()
{
    const int base = 7;
    int *d = new int[base]{ 0,  1,  2,  3,  4,  5,  6}; //decimal
    int *a = new int[base]{-3, -2, -1,  0,  1,  2,  3}; //alien
    
    int a0 = 0, a1 = 0, a2 = 0; // 1st
    int b0 = 0, b1 = 0, b2 = 0; // 2nd
    int t0 = 0, t1 = 0, t2 = 0; // TOTAL
    
    std::cout << "Enter  a0 a1 a2: ";
    std::cin >> a0 >> a1 >> a2;
    
    std::cout << "Enter  b0 b1 b2: ";
    std::cin >> b0 >> b1 >> b2;
    
    int CO = 0;
    
    t0 = ADC(a0, b0, CO, d, a, base);
    t1 = ADC(a1, b1, CO, d, a, base);
    t2 = ADC(a2, b2, CO, d, a, base);
    
    std::cout
    << std::setw(3) << a0
    << std::setw(3) << a1
    << std::setw(3) << a2 << '\n';
    
    std::cout
    << std::setw(3) << b0
    << std::setw(3) << b1
    << std::setw(3) << b2 << '\n';
    
    std::cout
    << std::setw(3) << t0
    << std::setw(3) << t1
    << std::setw(3) << t2
    << std::setw(3) << a[CO] << '\n';
    
    delete[] a;
    delete[] d;
    
    return 0;
}


Enter  a0 a1 a2: 1 1 1
Enter  b0 b1 b2: 3 3 3
  1  1  1
  3  3  3
  0  1  1 -2
Program ended with exit code: 0


Last edited on
Pages: 12