Author |
Topic: USE OF atoi() function (Read 3833 times) |
|
syashid
Newbie
Posts: 4
|
|
USE OF atoi() function
« on: Oct 2nd, 2011, 7:42am » |
Quote Modify
|
plz explain how to divide a number by 3 using only atoi() . also explain this function .how does it works.
|
|
IP Logged |
|
|
|
Grimbal
wu::riddles Moderator Uberpuzzler
Gender:
Posts: 7527
|
|
Re: USE OF atoi() function
« Reply #1 on: Oct 3rd, 2011, 1:39am » |
Quote Modify
|
For "how does it work", just google "man atoi" and read the 1st link. For example: char *str = "123"; int n = atoi(str); returns the integer 123 in n. I don't see any way to divide an integer by 3 where atoi would be of some help. You could convert the integer to a string in base 3, drop the last digit and re-convert the string to an integer, but you can't use atoi, it only does base 10.
|
|
IP Logged |
|
|
|
SMQ
wu::riddles Moderator Uberpuzzler
Gender:
Posts: 2084
|
|
Re: USE OF atoi() function
« Reply #2 on: Oct 3rd, 2011, 5:43am » |
Quote Modify
|
Here's a simple -- if not very efficient -- implementation of atoi in case you want to search for a way to use it to divide by 3: int atoi(const char * p) { int n; n = 0; while (*p >= 48 && *p <= 57) { n = n*10 + *p - 48; ++p; } return n; } --SMQ
|
|
IP Logged |
--SMQ
|
|
|
syashid
Newbie
Posts: 4
|
|
Re: USE OF atoi() function
« Reply #3 on: Oct 3rd, 2011, 8:41am » |
Quote Modify
|
actually it has been asked in adobe. so a liitle help w'll b good
|
|
IP Logged |
|
|
|
Grimbal
wu::riddles Moderator Uberpuzzler
Gender:
Posts: 7527
|
|
Re: USE OF atoi() function
« Reply #4 on: Oct 3rd, 2011, 10:03am » |
Quote Modify
|
What language are we talking about?
|
|
IP Logged |
|
|
|
websharer
Newbie
Gender:
Posts: 2
|
|
Re: USE OF atoi() function
« Reply #5 on: Jan 6th, 2012, 5:10pm » |
Quote Modify
|
on Oct 3rd, 2011, 10:03am, Grimbal wrote:What language are we talking about? |
| It's C++.
|
|
IP Logged |
|
|
|
TenaliRaman
Uberpuzzler
I am no special. I am only passionately curious.
Gender:
Posts: 1001
|
|
Re: USE OF atoi() function
« Reply #6 on: Jan 6th, 2012, 10:54pm » |
Quote Modify
|
on Jan 6th, 2012, 5:10pm, websharer wrote: are you sure its atoi and not itoa?
|
|
IP Logged |
Self discovery comes when a man measures himself against an obstacle - Antoine de Saint Exupery
|
|
|
Stefan Kneifel
Newbie
Gender:
Posts: 25
|
|
Re: USE OF atoi() function
« Reply #7 on: Feb 2nd, 2012, 12:25pm » |
Quote Modify
|
Maybe this way: Code: #include <stdio.h> #include <stdlib.h> int main (int argc, char** argv) { unsigned long x, y; char c[16]; while (--argc) { x = y = strtoul(*++argv, 0, 0); x = (x << 4) + y; x = (x << 4) + y; x = (x << 4) + y; sprintf (c, "%lu0", x); x = atoi(c) + y; x >>= 17; printf ("%lu / 3 = %lu\n", y, x); } return EXIT_SUCCESS; } |
| Works up to 49152 / 3 = 16384. However, I think a simple division would be faster
|
« Last Edit: Feb 2nd, 2012, 12:26pm by Stefan Kneifel » |
IP Logged |
|
|
|
SMQ
wu::riddles Moderator Uberpuzzler
Gender:
Posts: 2084
|
|
Re: USE OF atoi() function
« Reply #8 on: Feb 3rd, 2012, 5:16am » |
Quote Modify
|
atoi isn't doing any division in that algorithm. The two lines sprintf (c, "%lu0", x); x = atoi(c) + y; are equivalent to x = x*10 + y;. You're multiplying by 43691 = ({[(16 + 1)*16 + 1]*16 + 1}*10 + 1) then dividing by 131072, and if atoi can be said to be doing anything it's multiplying by 10. The only division happens in the x >>= 17 step. --SMQ
|
|
IP Logged |
--SMQ
|
|
|
|