LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 03-07-2010, 01:26 AM   #1
anurupr
Member
 
Registered: Mar 2010
Posts: 71

Rep: Reputation: 16
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 <iostream>
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"<<endl;
}
int decchecker(int d)

{
  
int flag;
 
 int n=0;
 int t; 

 t=d;

 while(t!=0)
 {
  t/=10;
  n++;
 }
 int nd;
 
 if((n%2==0)&& (n>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;

}
 
Old 03-07-2010, 06:09 AM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
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
 
Old 03-07-2010, 08:58 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741
Quote:
Originally Posted by anurupr View Post
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.
 
Old 03-07-2010, 09:47 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741
Moved to Programming
 
Old 03-07-2010, 05:11 PM   #5
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 553

Rep: Reputation: 116Reputation: 116
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<stdio.h>
#include<stdlib.h>
#include<string.h>

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<strlen(argv[1])) {
    int one, two;
        
    /*One Digit*/
    strncpy(tmpone,&argv[1][i],1);
    one=atoi(tmpone);
    /*Two digits*/
    strncpy(tmptwo,&argv[1][i],2);
    two=atoi(tmptwo);
        
    if (two<=26) {
      results[n++]=two;
      i+=2;
    }
    else {
      results[n++]=one;
      i++;
    }
  } /*While Loop*/
  
  free(tmpone);
  free(tmptwo);
  
  /*Loop through the results array*/
  for (i=0;i<n;i++) {
    printf("Result %i = %i\n",i,results[i]); 
  }
  
  return 0;
}
A few example runs:

Code:
$ ./x 2346
Result 0 = 23
Result 1 = 4
Result 2 = 6
$ ./x 1843154848976318646848615313573575
Result 0 = 18
Result 1 = 4
Result 2 = 3
Result 3 = 15
Result 4 = 4
Result 5 = 8
Result 6 = 4
Result 7 = 8
Result 8 = 9
Result 9 = 7
Result 10 = 6
Result 11 = 3
Result 12 = 18
Result 13 = 6
Result 14 = 4
Result 15 = 6
Result 16 = 8
Result 17 = 4
Result 18 = 8
Result 19 = 6
Result 20 = 15
Result 21 = 3
Result 22 = 13
Result 23 = 5
Result 24 = 7
Result 25 = 3
Result 26 = 5
Result 27 = 7
Result 28 = 5
$ ./x 0125451012104                     
Result 0 = 1
Result 1 = 25
Result 2 = 4
Result 3 = 5
Result 4 = 10
Result 5 = 12
Result 6 = 10
Result 7 = 4
$ ./x 01254510012104
Result 0 = 1
Result 1 = 25
Result 2 = 4
Result 3 = 5
Result 4 = 10
Result 5 = 1
Result 6 = 21
Result 7 = 4

Last edited by devnull10; 03-07-2010 at 05:13 PM.
 
Old 03-07-2010, 05:31 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
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.
 
Old 03-07-2010, 05:37 PM   #7
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 553

Rep: Reputation: 116Reputation: 116
^^ in which case, the following will do that:

Code:
echo "obase=26 [numberhere]" | bc
 
Old 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: Reputation: 148Reputation: 148
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?
 
Old 03-07-2010, 10:36 PM   #9
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
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".
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
split file based on number of string apperance mcbenus Programming 10 12-24-2009 06:44 PM
grep regex number decimal capital letter casperdaghost Linux - Newbie 4 08-22-2009 06:32 AM
perl: how could i split part of number... DoME69 Programming 4 07-31-2008 09:06 PM
split integer into arbitrary number of addends john_crichton Programming 2 06-26-2008 10:23 PM
Have problem converting a decimal number to octal Linh Programming 4 05-20-2004 03:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration