LinuxQuestions.org i need to split a decimal number in such a way that each unit is between 1 and 26
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

 03-07-2010, 01:26 AM #1 anurupr Member   Registered: Mar 2010 Posts: 71 Rep: i need to split a decimal number in such a way that each unit is between 1 and 26 i need to split a decimal number in such a way that each unit is between 1 and 26. for eg if i input a number 2346 it should split it into 23 , 4 , 6 i've been trying for hours to do this but in vain. i've tried this code Code: ```#include int temp[7]; int prev; using namespace std; void decsplitter(int t,int fl) { if(fl==1) { for(int i=0 ; i<2; i++) { temp[i]=t%10; t/=10; } } for(int i=0;i<3;i++) { cout << temp[i] << endl; } if(fl==0) cout << "FL==0"<2)) { nd=n/2; flag=1; } else if((n%2!=0) || n < 2 || n==2) { nd=n; flag=0; } else int k=0; t=d; if(flag==1) { for(int i=0;i<2;i++) { temp[i]=t%100; temp[i+2]=t/100; if(temp[i]>26) { cout << temp[i] << endl; decsplitter(temp[i],1); } else { cout << temp[i] << endl; decsplitter(temp[i],0); } } } if(flag==0) { for(int i=0 ; i<2; i++) { temp[i]=t%10; t/=10; } } } int main() { int dec = 2346; decchecker(dec); return 0; }```
 03-07-2010, 06:09 AM #2 kbp Senior Member   Registered: Aug 2009 Posts: 3,790 Rep: I'm assuming you want to translate a number into letters of the alphabet.... the problem is that the data can be interpreted in too many different ways if it's not structured somehow eg, 2346 == 23,4,6 == 2,3,4,6 1234 == 12,3,4 == 1,2,3,4 How can the script know which one you want ? You need to structure the incoming data so it's not ambiguous hope this helps cheers
03-07-2010, 08:58 AM   #3
pixellany
LQ Veteran

Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep:
Quote:
 Originally Posted by anurupr i need to split a decimal number in such a way that each unit is between 1 and 26. for eg if i input a number 2346 it should split it into 23 , 4 , 6
My first reaction is WHY?? Is this a homework problem?

First, as already stated, you need to be clear about the criteria. Your example implies that the rule is:
pick the largest number of digits that fall in the range 1-26, clip them, repeat. Thus--eg--124234216 would be split:
12,4,23,4,21,6

Also, does it need to be done in C? BASH or Python might be easier.

 03-07-2010, 09:47 AM #4 pixellany LQ Veteran   Registered: Nov 2005 Location: Annapolis, MD Distribution: Mint Posts: 17,809 Rep: Moved to Programming
 03-07-2010, 05:11 PM #5 devnull10 Member   Registered: Jan 2010 Location: Lancashire Distribution: Slackware Stable Posts: 572 Rep: I have thrown something together in c for you which will do this. I have just use a temporary array of 100 ints rather than dynamically allocating and re-allocating memory as the size of the argument grows. If you want more, either increase the array size or go the dynamic route. It will always pick the largest number first (ie, for 123 it would pick 12 then 3 rather than 1 then 23). It works on the assumption that there will never be more than two characters making up a number less than 26 - if you have zeros then 02 is assumed to be the number two. It will also assume that 00 is the number zero. Yes, there are complications and cases where it may not work, but for basic stuff this will do the job. It's not the cleanest of code because I have just thrown it together. I know the tmp pointers could have been done without malloc'ing but I had done it a different way first and couldn't be bothered re-writing it! Code: ```#include #include #include int main(int argc, char **argv) { int i=0; int results[100]; int n=0; char *tmpone, *tmptwo; tmpone=(char*)malloc(sizeof(char)); tmptwo=(char*)malloc(sizeof(char)*2); while (i
 03-07-2010, 05:31 PM #6 Sergei Steshenko Senior Member   Registered: May 2005 Posts: 4,481 Rep: Well, 26 is the number of letters in English alphabet, so maybe the OP in fact needs a radix 10 -> radix 26 conversion, but it's my wild guess.
 03-07-2010, 05:37 PM #7 devnull10 Member   Registered: Jan 2010 Location: Lancashire Distribution: Slackware Stable Posts: 572 Rep: ^^ in which case, the following will do that: Code: `echo "obase=26 [numberhere]" | bc`
 03-07-2010, 08:10 PM #8 graemef Senior Member   Registered: Nov 2005 Location: Hanoi Distribution: Fedora 13, Ubuntu 10.04 Posts: 2,379 Rep: As others have said you need to get your requirements clear, and you need to do that before you start any coding. For example: would 123 be split into 12 and 3 (selecting from left to right) or 1 and 23 (selecting from right to left) Have you considered if a brute force approach is sufficient; will it always give you the minimum number of pairings? Does it matter?
 03-07-2010, 10:36 PM #9 Dan04 Member   Registered: Jun 2006 Location: Texas Distribution: Ubuntu Posts: 207 Rep: Looks like an attempt to decode numbers into words. If so, I'd get the word list by reading the "/usr/share/dict/words" file, translate each one to its numeric equivalent, and build a decoding map with that. There's going to be some ambiguity; e.g. 13135 could be either "acme" or "mace".

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post mcbenus Programming 10 12-24-2009 06:44 PM casperdaghost Linux - Newbie 4 08-22-2009 06:32 AM DoME69 Programming 4 07-31-2008 09:06 PM john_crichton Programming 2 06-26-2008 10:23 PM Linh Programming 4 05-20-2004 03:21 PM

LinuxQuestions.org

All times are GMT -5. The time now is 07:26 AM.

 Contact Us - Advertising Info - Rules - Privacy - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -