wu :: forums
« wu :: forums - USE OF atoi() function »

Welcome, Guest. Please Login or Register.
Apr 29th, 2024, 12:59am

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: Grimbal, Eigenray, ThudnBlunder, towr, Icarus, SMQ, william wu)
   USE OF atoi() function
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: USE OF atoi() function  (Read 3833 times)
syashid
Newbie
*





   


Posts: 4
USE OF atoi() function  
« on: Oct 2nd, 2011, 7:42am »
Quote Quote Modify 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: male
Posts: 7527
Re: USE OF atoi() function  
« Reply #1 on: Oct 3rd, 2011, 1:39am »
Quote Quote Modify 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: male
Posts: 2084
Re: USE OF atoi() function  
« Reply #2 on: Oct 3rd, 2011, 5:43am »
Quote Quote Modify 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 Quote Modify Modify

actually it has been asked in adobe. so a liitle help w'll b good
IP Logged
Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7527
Re: USE OF atoi() function  
« Reply #4 on: Oct 3rd, 2011, 10:03am »
Quote Quote Modify Modify

What language are we talking about?
IP Logged
websharer
Newbie
*





   
WWW

Gender: male
Posts: 2
Re: USE OF atoi() function  
« Reply #5 on: Jan 6th, 2012, 5:10pm »
Quote Quote Modify 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: male
Posts: 1001
Re: USE OF atoi() function  
« Reply #6 on: Jan 6th, 2012, 10:54pm »
Quote Quote Modify Modify

on Jan 6th, 2012, 5:10pm, websharer wrote:

It's C++.

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: male
Posts: 25
Re: USE OF atoi() function  
« Reply #7 on: Feb 2nd, 2012, 12:25pm »
Quote Quote Modify 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 Wink
« Last Edit: Feb 2nd, 2012, 12:26pm by Stefan Kneifel » IP Logged
SMQ
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 2084
Re: USE OF atoi() function  
« Reply #8 on: Feb 3rd, 2012, 5:16am »
Quote Quote Modify Modify

Huh 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

Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board