# Combing Number (Not With Addition)

By RoboDragon

January 29, 2011

January 29, 2011

I'm trying to create a count down timer. But in this countdown timer you can set the time. I was wandering how to combine 2 numbers, but not by adding, just putting the together.

Example:

you press 2 and 5 to make 25 minutes.

how would you combine varriable x = 2 and y = 5 into z = 25

or if varriables are not good

how would you combine myArray[] = {2, 5} into newArray[] = {25} or myArray[] = {25} (same array)

Thanks for the help if you can!

## I think you are asking more

I think you are asking more than just math. You don't know the 2 actually means 20 until the user presses the 5 and if another number is pressed then 2 means 200. What you can do is use an accumulator. Start with the accumulator as zero. Whenever you get a number as input, multiply whatever is in the accumulator by 10 and add the new input value to it, keeping the result in the accumulator. When you get whatever input that tells you to use what was keyed, the number is in the accumulator. You don't need an array for this unless you have another need to buffer the input (but I am breaking your "rule" and using addition :) ). This method works for a number of just about any size, but the only editing you can do is accept or cancel. Works great for a lot of simple devices though.

Consider this pseudocode (may compile; untested):

int iAccum = 0;

void StartAccum() {iAccum = 0;}

int AddAccum(int iDigit)

{

iAccum *= 10;

iAccum += iDigit;

return iAccum;

}

int GetAccum() {return iAccum;}

When you start your input routine, call StartAccum(). Each time you get a digit, call AddAccum(). When you have all the digits, you can just use the return from passing in the last one for a fixed size input or call GetAccum() if you get an acceptance indication. If you have a cancel function, just call StartAccum() again.

## z = 100*A + 10*B + C;

As Patrick pointed out you can weight each variable by multiplying each digit by factors of 10^n starting with n=0 for the rightmost digit. Hope that is clear.

z = 100*A + 10*B + C;

This could be made to take a variable number of inputs by using a for loop. Another approach I like to use is up/down buttons that advance fast when a button has been held down for more than a couple of seconds, but that works best with a display.

You may even want to do something like this for counting down if you have minutes and seconds (C code) :

void calc_display_nums( int tot_sec ) {

int min = tot_sec / 60;

int sec = tot_sec % 60;

display_nums[0] = min / 10; // tens digit (minutes)

display_nums[1] = min % 10; // ones digit (minutes)

display_nums[2] = sec / 10; // tens digit (seconds)

display_nums[3] = sec % 10; // ones digit (seconds)

}

which keeps track of the time as a total number of seconds to count down but displays the values in minutes and seconds format.

## x*=10x+=y;= 25. That is how

x*=10;

x+=y;

= 25.

That is how i do it. Yeah you can do it with 1 line of code also.