Thread: improve my number splitting prog

  1. #1
    Registered User Mark S.'s Avatar
    Join Date
    May 2005
    Location
    England
    Posts
    16

    improve my number splitting prog

    My following program splits a decimal number into the individual digits with the relevant multiples of 10.

    My c++ skills are limited and i would like to see how other people would approach this task ,as I'm sure I've is over complicated things.

    Especially this thing:-

    int digit = (num % mult) / (mult / 10);

    Thanks for any constructive advice.

    Code:
    void SplitNumber(void)
    {
    	int num = rand() % 10000 + 1;
    	int nums[16] = {0};
    	int mult = 10;
    	int totalCheck = 0;
    	int numOfDigits = 0;
    
    	do
    	{
    		int digit = (num % mult) / (mult / 10);
    		nums[numOfDigits] = digit;
    		totalCheck += digit * (mult / 10);
    		mult *= 10;
    		numOfDigits++;
    	}while(totalCheck != num);
    
    	cout << "Original number = " << num << " is equal to the accumulation of\n";
    	//	 << "Total = " << totalCheck << '\n';
    
    	mult = 1;
    	for(int i = 0; i < numOfDigits; i++)
    	{
    		cout << nums[i] << " * "
    			 << mult  << " = "
    			 << nums[i] * mult << '\n';
    
    		mult *= 10;
    	}
    
    }
    Mark S.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Code:
    #include <iostream>
    #include <cmath>
    
    int split(const int num, int iter);
    
    int main()
    {
    	int num;
    
    	std::cout<<"Give me a number: ";
    	std::cin>>num;
    	std::cin.ignore(1);
    	std::cout<<split(num,1)<<"x1"<<std::endl;
    	
    	return 0;
    }
    
    int split(const int num, int iter)
    {
    	if(num/10<1)
    	{
    		iter++;
    		return num;
    	}
    	else
    	{
    		std::cout<<split(num/10,++iter)<<'x'<<pow(10,iter)<<'\n';
    		return num-(static_cast<int>(num/10)*10);
    	}
    }
    yeah, don't do that.
    Last edited by major_small; 08-31-2006 at 04:48 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i would like to see how other people would approach this task
    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    
    using namespace std;
    
    string split_num ( int num )
    {
      stringstream s;
      s<< num;
      return s.str();
    }
    
    int main()
    {
      int num;
    
      cout<<"Enter a number: ";
    
      if ( cin>> num ) {
        string s = split_num ( num );
    
        for ( string::size_type i = 0; i < s.size(); i++ )
          cout<< ( s[i] - '0' ) * 2 <<'\n';
      }
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Calculating next prime number
    By anilemon in forum C Programming
    Replies: 8
    Last Post: 04-17-2006, 10:38 AM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  5. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 01:10 PM