Jump to content
  • Advertisement
Sign in to follow this  
sheep19

Breaking a three-digit integer. Is this a good way?

This topic is 3754 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I recently had a computer science exam and I messed it up a bit. There was an exercise to break a three digit number to three numbers. I didn't do it..because it's different to be under the pressure than in your room. So I sat and did it now. I'm asking if this is good way to do it(it works by the way)
#include <iostream>

int main()
{
	int num;
	std::cout << "Enter a number between 100 and 999: ";
	do
	{
		std::cin >> num;
		if( num < 100 || num > 999 )
			std::cout << "100-999 please. Enter it again: ";

	} while ( num < 100 || num > 999 );

	int i, j, k;

	for( int a = 900; a >= 100; a -= 100 )
	{
		if( num / a == 1 )
		{
			i = a / 100;
			break;
		}
	}

	for( int a = 90; a >= 10; a -= 10 )
	{
		if( (num - i * 100) / a == 1 )
		{
			j = a / 10;
			break;
		}

		if( a == 20 )
			j = 0;
	}

	k = num - (i * 100) - (j * 10);

	std::cout << "i = " << i << ", j = " << j << ", k = " << k << "\n";
	
	system("pause");
	return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
123 = (1 * 10 + 2) * 10 + 3

123 % 10 = 3
123 / 10 = 12

12 % 10 = 2
12 / 10 = 1


Therefore, a simpler way is:

int i = num % 10;
int j = (num / 10) % 10;
int k = num / 100;


Share this post


Link to post
Share on other sites
Suggested exercise: Write a program that will take a number and break it into it's digits. The program should work no matter how many digits the number has (within the limits of an integer, of course).

For example, if the input is 23, the output should be 2, 3. If it's 2053, the output should be 2, 0, 5, 3, etc.

Share this post


Link to post
Share on other sites
Quote:
Original post by Gage64
Write a program that will take a number and break it into its digits.

#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>

int main()
{
std::cout << "enter a number: ";
int i = 0;
std::cin >> i;

std::stringstream ss;
ss << i;
std::string s = ss.str();

std::copy(s.begin(), s.end() - 1, std::ostream_iterator<char>(std::cout, ", "));
std::cout << *(s.end() - 1);
}

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
*** Source Snippet Removed ***


First, the suggestion was geared towards the OP so that he'll get a better understanding of this, along with some additional practice. Posting a solution kind of gets in the way of that, don't you think?

Second, the idea was to use a method like that described by ToohrVyk (and clearly that is what should have been used in the test). In your code, you are letting the STL do all the work for you, and therefore bypassing any need to think about how to solve this yourself (unless the objective is to get some practice using the STL, but clearly that is not the case here).

Share this post


Link to post
Share on other sites
My english is so bad, it cost me more than 10 minutes to get the means,and then
find it so easy.

cout<< num/100<<endl;
cout<<(num%100)/10<<endl;
cout<<num%10<<endl;


Share this post


Link to post
Share on other sites
Ok, here's mine:


#include <iostream>
#include <vector>
#include <cmath>

int main()
{
int num, size = 0, a;
std::cout << "Enter a number: ";
std::cin >> num;

std::vector< int > digits;

for( int i = 10;; i*= 10 ) // get how many digits the number has
{
if( num / i == 0 )
{
a = i / 10;
size = log10( float(i));
digits.reserve(size);
break;
}
}

for( int i = a; i != 0; i/=10 ) // put the digits in the vector
digits.push_back( num / i % 10 );

for( std::vector< int >::const_iterator iter = digits.begin(); iter != digits.end(); ++iter ) // display
std::cout << *iter << " ";
std::cout << "\n";

system("pause");
return 0;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!