Go Down

Topic: Programming tomb!!! (Read 1 time) previous topic - next topic

ejahable

Aug 01, 2011, 01:58 am Last Edit: Aug 01, 2011, 02:01 am by ejahable Reason: 1
I will be 4ft underground if I don't get this to work. I spent 5 days already trying to figure out solutions but is just not working.  :(

I need an object that receives two parameters:
  1) a number (0 to 99999 "long type")
  2) a place number (0 to 5 "int type")  
with these parameters it returns the digit of the number determine by the place number.

example 1:
1)  number:           56789
2)  place number:  1
 it returns 9    

example 2:
1)  number:           56789
2)  place number:  2
 it returns 8

I believe that I made my point, I added my code below. Nevertheless I can't get it to work without overflowing. Any suggestions will be appreciated.

Thanks in advance.

Code: [Select]

int getDigit(long number,int place){
 if(place == 1){return (number-((int(number*0.1))*10));}
 else if(place == 2){return (int((number-((int(number*0.01))*100))*0.1));}
 else if(place == 3){return (int((number-((int(number*0.001))*1000))*0.01));}
 else if(place == 4){return (int((number-(long(int(number*0.0001)))*10000)*0.001));}
 else{return (int(number*0.0001));}
}

crimony

How about:

Code: [Select]
#include <math.h>

int getDigit(long number, int place)
{
    return ((number / long(pow(10, place - 1))) % 10);
}

ejahable

So I used your code like such:

Code: [Select]
#include <math.h>
boolean boo = true;
void setup(){
  Serial.begin(9600);
}
void loop(){
  while(boo){
    delay(4000); 
    Serial.println(getDigit(56789L,1));
    Serial.println(getDigit(56789L,2));
    Serial.println(getDigit(56789L,3));
    Serial.println(getDigit(56789L,4));
    Serial.println(getDigit(56789L,5));
    boo = false;
    }
}

int getDigit(long number, int place)
{
    return ((number / long(pow(10, place - 1))) % 10);
}


and got as result:
9
8
3
6
5

Your code is ingenious but it stills has problems. Thanks

crimony

I don't have my Arduino here so I can't test, but it works fine on Linux using int as long and short as int. Must be an Arduino integer or math library issue.

You will need to split the functionality up and see at what point it is failing:

Code: [Select]

int getDigit(long number, int place)
{
    double p = pow(10, place - 1);
    long l = long(p);
    long n = number / l;
    long m = n % 10;

    // Debug output
    Serial.print("getDigit() called, number = ");
    Serial.print(number);
    Serial.print(", place = ");
    Serial.print(place);
    Serial.print(", l = ");
    Serial.print(l);
    Serial.print(", n = ");
    Serial.print(n);
    Serial.print(", m = ");
    Serial.println(m);

    return int(m);
}

johnwasser

pow() returns a float.  Arduino is MUCH happier with integers:

Code: [Select]


int getDigit(long number, int place)
{
   for (; place>1; place--)
      number /= 10;   // Shift right one decimal digit

   return number % 10;  // Return rightmost decimal digit
}

[/quote]
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Nick Gammon


I will be 4ft underground if I don't get this to work. I spent 5 days already trying to figure out solutions but is just not working.  :(


6 ft surely?

Try converting to a string:

Code: [Select]
int getDigit (long number, int place)
{
char buf [12];
ltoa (number, buf, 10);
// sanity clause
if (place <= 0 || place > strlen (buf))
   return 0;
// extract digit
return (buf [strlen (buf) - place] - '0');
}  // end of getDigit

void setup ()
{
  Serial.begin (115200);
 
  for (int i = 1; i <= 10; i++)
    Serial.println (getDigit (56789L, i));

  Serial.println ();
 
  for (int i = 1; i <= 10; i++)
    Serial.println (getDigit (2147483648L, i));
 
}  // end of setup

void loop ()
{}


Example output:

Code: [Select]
9
8
7
6
5
0
0
0
0
0

8
4
6
3
8
4
7
4
1
2
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

crimony

Quote
pow() returns a float.  Arduino is MUCH happier with integers


Agreed, but I set myself the goal of trying to do it in one line (not counting the #include).

David Pankhurst

This feels suspiciously like a homework assignment, but I'll bite:

Code: [Select]
unsigned int homework(unsigned long x,unsigned int m)
{
  while (--m)
    x/=10;
  return x%10;
}


Will work for 1-max digits in number, unsigned only; no guarantees outside of that...

David Pankhurst

... just noticed johnwasser already wrote code that works, so didn't need to include mine - however, it's already done, it's another variation, so I'll leave it be (sorry john - should have checked more carefully).

Nick Gammon


Code: [Select]

...
// sanity clause
...



http://www.marx-brothers.org/info/quotes.htm

http://en.wikipedia.org/wiki/There_Ain't_No_Sanity_Clause
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

ejahable

Thanks all of you, I really appreciated!! and no its not for school.  :smiley-yell: 
lol, you all guys really know this stuff. Kudos!!  8)

Go Up