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)
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 = 100A + 10B + 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) :
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.