Base Conversion Program Help Please

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1672 Views - Last Post: 20 October 2008 - 03:05 PM Rate Topic: -----

#1 Raven-Mage  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-October 08

Base Conversion Program Help Please

Posted 20 October 2008 - 01:57 AM

Ok guys i usually dont ask for help with these things but this has me boggled. Im working on writing a program for base conversion using NO arrays. So far i have:
#include <iostream>

int main()
{
	int base;
	
	std::cout<<"Please enter what base to convert from: ";
	std::cin>>base;

switch(base)
 {
case 4:
  {
	int x, first, second, third, fourth, fifth, test1, test2, test3, test4, test5;
	int result;

	std::cout<<"Imput integer: ";
	std::cin>>x;
	
	first=x%4;
	test1=x/4;
	 if(test1<=0)
		 {
		 result=first;
		 std::cout<<result<<"\n";
		 break;
		 }
	 else
	 {
	  second=test1%4;
	  test2=test1/4;
	   if(test2<=0)
		   {
		   result=(second*10)+first;
		   std::cout<<second<<first<<"\n";
		   break;
		   }
	   else
	   {
		third=test2%4;
		test3=test2/4;
		if(test3<=0)
			{
			result=(third*100)+(second*10)+first;
			std::cout<<result<<"\n";
			break;
			}
		else
		{
		 fourth=test3%4;
		 test4=test3/4;
		 if(test4<=0)
			{
			result=(fourth*1000)+(third*100)+(second*10)+first;
			std::cout<<result<<"\n";
			break;
			}
		 else
			{
			fifth=test4%4;
			test5=test4/4;
			result=(fifth*10000)+(fourth*1000)+(third*100)+(second*10)+first;
			std::cout<<result<<"\n";
			break;
			}
		 }
	   }
	 }
  }
case 2:
   {
	int x, first, second, third, fourth, fifth, test1, test2, test3, test4, test5;
	int result;

	std::cout<<"Imput integer: ";
	std::cin>>x;
	
	first=x%2;
	test1=x/2;
	 if(test1<=0)
		 {
		 result=first;
		 std::cout<<result<<"\n";
		 break;
		 }
	 else
	 {
	  second=test1%2;
	  test2=test1/2;
	   if(test2<=0)
		   {
		   result=(second*10)+first;
		   std::cout<<result<<"\n";
		   break;
		   }
	   else
	   {
		third=test2%2;
		test3=test2/2;
		if(test3<=0)
			{
			result=(third*100)+(second*10)+first;
			std::cout<<result<<"\n";
			break;
			}
		else
		{
		 fourth=test3%2;
		 test4=test3/2;
		 if(test4<=0)
			{
			result=(fourth*1000)+(third*100)+(second*10)+first;
			std::cout<<result<<"\n";
			break;
			}
		 else
			{
			fifth=test4%2;
			test5=test4/2;
			result=(fifth*10000)+(fourth*1000)+(third*100)+(second*10)+first;
			std::cout<<result<<"\n";
			break;
			}
		 }
	   }
	 }
   }
case 8 :
 {
	int x, first, second, third, fourth, fifth, test1, test2, test3, test4, test5;
	int result;

	std::cout<<"Imput integer: ";
	std::cin>>x;
	
	first=x%8;
	test1=x/8;
	 if(test1<=0)
		 {
		 result=first;
		 std::cout<<result<<"\n";
		 break;
		 }
	 else
	 {
	  second=test1%8;
	  test2=test1/8;
	   if(test2<=0)
		   {
		   result=(second*10)+first;
		   std::cout<<result<<"\n";
		   break;
		   }
	   else
	   {
		third=test2%8;
		test3=test2/8;
		if(test3<=0)
			{
			result=(third*100)+(second*10)+first;
			std::cout<<result<<"\n";
			break;
			}
		else
		{
		 fourth=test3%8;
		 test4=test3/8;
		 if(test4<=0)
			{
					 result=(fourth*1000)+(third*100)+(second*10)+first;
			std::cout<<result<<"\n";
			break;
			}
		 else
			{
			fifth=test4%8;
			test5=test4/8;
{
result=(fifth*10000)+(fourth*1000)+(third*100)+(second*10)+first;
			std::cout<<result<<"\n";
			break;
			}
		 }
	   }
	 }
   }
default :
	std::cout<<"Invalid entry";
}	 
   return (0);
   }




I have a feeling however that this isnt the right way to approach this...
Im asking because ive been working on this for around 6 hours straight now and just cant seem to crack it. I know it involves using a While loop, or a For loop but i just cant seem to have that "ahah" moment to know how to use the proper loops necesarry. Help would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Base Conversion Program Help Please

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 109
  • View blog
  • Posts: 1,530
  • Joined: 25-January 07

Re: Base Conversion Program Help Please

Posted 20 October 2008 - 02:10 AM

are you trying to convert number from any base to decimal?

you must be trying something similar to this...
i=-1;
result = 0;
digit = 0;
while(number > 0)
{
  i++;
  digit = number%10; // getting last digit of number.
  result += digit*pow(base,i); // conversion in decimal.
  number /=10;  //reduce the number.
}



I wrote this on the fly. So you might need small modifications in this, please check for them.

I hope this will help you. :)
Was This Post Helpful? 0
  • +
  • -

#3 Raven-Mage  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-October 08

Re: Base Conversion Program Help Please

Posted 20 October 2008 - 02:26 AM

Oh sorry, trying to convert from base 10 to either base 2, 4, 8, 16. Obviously i havent done the 16 yet because i dont think im doing it write. I am using Microsoft Visual C++
Was This Post Helpful? 0
  • +
  • -

#4 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 109
  • View blog
  • Posts: 1,530
  • Joined: 25-January 07

Re: Base Conversion Program Help Please

Posted 20 October 2008 - 02:46 AM

alright, in that case you can use recursion techniques

something like this [this won't work for base > 10]
void baseconvert(int num, base)
{
  if(num == 0 ) 
    return;
  baseconvert((num/base), base);
  cout << (num%base);
}


I wrote this on the fly as well. So you might need small modifications in this again, please check for them.

I hope this will help you. :)
Was This Post Helpful? 0
  • +
  • -

#5 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Base Conversion Program Help Please

Posted 20 October 2008 - 05:00 AM

//You'll have to excuse me if the syntax is not 100% correct.
//Anyways, first define the char-array below, it will work for all bases up to Hex.

static char table[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F};

//Also define an array where you save the values of the new base (of course it doesn't have to be static)
static int value[100];

void DecimalToOtherBase(int decimal, int newBase) {
int i = 0;
int j;

  value[i] = (decimal % newBase);
  i++;
  while ((decimal / newBase) > 0) {
	decimal = decimal / newBase;
	value[i] = (decimal % newBase);
	i++;
  }
  for (j = 0; j < i; j++)
	printf("%c", table[value[i - (j + 1)]]);
//The for-loop of course is to reverse the order of the numbers.
}



The code can probably be optimized but I must say I'm rather happy with it.
The reason the char-array can be reused is that the printed value will always be fetched from within the range of the base.
Ex: Base 8 will always fetch values in range [0..7] (the first 8 values of the array.)
Ex: Base 5 will always fetch values in range [0..4] (the first 5 values of the array.)
It's good to know that although most are rarely used, you can use any integer as base.

This post has been edited by Gloin: 20 October 2008 - 05:13 AM

Was This Post Helpful? 0
  • +
  • -

#6 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 109
  • View blog
  • Posts: 1,530
  • Joined: 25-January 07

Re: Base Conversion Program Help Please

Posted 20 October 2008 - 05:06 AM

Yes, that's one of the good ways Gloin. But I think he wants to try it without using arrays.
Was This Post Helpful? 0
  • +
  • -

#7 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Base Conversion Program Help Please

Posted 20 October 2008 - 05:15 AM

Ok, so he'll just have to rewrite it recursively then. Not too difficult.
I suppose you can even do it iteratively with loops although it will have a terrible complexity after recalculating stuff over and over.

This post has been edited by Gloin: 20 October 2008 - 05:21 AM

Was This Post Helpful? 0
  • +
  • -

#8 Raven-Mage  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-October 08

Re: Base Conversion Program Help Please

Posted 20 October 2008 - 12:16 PM

Alright, ive made SOME headway. Heres my code now :
#include <iostream>

int main(void)
{
int x, base, power, rem;

cout<<Enter number;
cin>>x;
cout<<Enter base;
cin>>base;

power=0;
do
{
rem = x%base;
total += rem *( 10^power);
power++;
x/base;
}while(x>0);

cout<<"The number "<<x<<" Converted to base " <<base<<" equals: "<<total;

return 0;
}



Keep in mind I want to convert the inputted number from a base to a base. So far this code will produce the first part right? As in get the "from base". The thing i dont know how to do is get it to go up to base 16, the way it is now should work for bases 2,4,8. Once again, help would be appreciated.

This post has been edited by Raven-Mage: 20 October 2008 - 12:18 PM

Was This Post Helpful? 0
  • +
  • -

#9 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Base Conversion Program Help Please

Posted 20 October 2008 - 12:36 PM

The code isn't syntactically correct so it won't produce anything.

Some errors: (At least so I assume)

total += rem *( 10^power); //10^power means (10 XOR power) and I don't see how that could be relevant
x/base; // x/base what? this is not a condition nor an assignment, it does nothing and I doubt it will even compile.

Besides, it would still print the values in reversed order

This post has been edited by Gloin: 20 October 2008 - 12:38 PM

Was This Post Helpful? 0
  • +
  • -

#10 Raven-Mage  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-October 08

Re: Base Conversion Program Help Please

Posted 20 October 2008 - 12:42 PM

View PostGloin, on 20 Oct, 2008 - 12:36 PM, said:

The code isn't syntactically correct so it won't produce anything.

Some errors: (At least so I assume)

total += rem *( 10^power); //10^power means (10 XOR power) and I don't see how that could be relevant
x/base; // x/base what? this is not a condition nor an assignment, it does nothing and I doubt it will even compile.

Besides, it would still print the values in reversed order



Well, my goal for the 10^power, was to raise 10 to that power. Is there another way of accomplishing this?
x/base i thought would divide x by base, how are you suppose to write this if not like that?
I apologize for nagging so much i realize im a beginner at this and just would like some clarification
Was This Post Helpful? 0
  • +
  • -

#11 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Base Conversion Program Help Please

Posted 20 October 2008 - 12:49 PM

No problem, but when you make that division you must save the result somewhere.

x = x/base; <-- Correct syntax, saves he value back in to x.

About power, I can't remember for sure but I think the function name is Pow.

total += rem * Pow(10, power); would be correct then.
Was This Post Helpful? 1
  • +
  • -

#12 Raven-Mage  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-October 08

Re: Base Conversion Program Help Please

Posted 20 October 2008 - 12:57 PM

Thanks a bunch.

Not going to push anymore but if anyone has any tips on how to convert into base 16, id be grateful.
Was This Post Helpful? 0
  • +
  • -

#13 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Base Conversion Program Help Please

Posted 20 October 2008 - 01:05 PM

Unfortunately, I think that will be very difficult. I must say I think you came up with a rather beautiful solution so far though.
Was This Post Helpful? 0
  • +
  • -

#14 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Base Conversion Program Help Please

Posted 20 October 2008 - 01:12 PM

Are you allowed to use string-builders?
Was This Post Helpful? 0
  • +
  • -

#15 Raven-Mage  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 20-October 08

Re: Base Conversion Program Help Please

Posted 20 October 2008 - 02:03 PM

View PostGloin, on 20 Oct, 2008 - 01:12 PM, said:

Are you allowed to use string-builders?



The only things not allowed are arrays and manipulators
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2