Write a program converting Base 10 numbers to any base from 2 - 16

Can someone give me some advice/hints in regards to writing a program that takes a decimal number from the command line and converts this number in REVERSE order into the base specified by the last parameter??

I've been trying to study C++ for several weeks now but I'm truely uncertain of how to start this! Help if possible, thank you very much.

Whenever you try to write a program, don't just start writing it straightaway. First think how you will do this on a paper by writing steps and than convert these steps to a C++ code.

So do you have some steps or C++ code with you? Post that.
Sorry.. I'm still looking through my textbook to see what coding to use, it's difficult as I don't really understand the project itself.. :(

(Example1: YourProgName 12 b2
which should return the number '12' displayed as a binary number
in REVERSE order i.e as 0011 )
I struggle to comprehend how to produce this program. It feels like a new language, argggh!


Ok I will help but will not write any code for you.

read about the left shift operator working in C. Everytime you left shift, its a divide by 2.
now do the shift untill the number becomes a 1.
after every shift, see if the number is even and in that case you have to print a 0 otherwise a 1.

so here are the steps:
12 -> 0
shift once and it becomes a 6.
6 -> 0
shift once and it becomes a 3.
3 -> 1
shift once and it becomes a 1
1 -> 1
this will end your program.


simple!! isn't it. :)


try to put this into a program and show it and I will help you to correct it.
#include <iostream>


using namespace std;

void show_binary(unsigned int u) ;

int main(int argc, char *argv[])

{
int i=1, t;

//left shift
for (t=0; t < 12 ; t++) {
show_binary(i) ;
i = i << 1 ;
}

return 0;
}




It doesnt compile!! :(
Last edited on

always use code tags. My mistake i wrote left shift by error. It should be right shift. sorry for that.
try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void show_binary(unsigned int u)
{

	for (t=0; t < 12 ; t++)		//run this loop till u is equal to 1, start from its original value
	{		
		i = i << 1 ; //it should be right shift. sorry for that.
					//so here you will check if the number is even or odd using % operator
					//accordingly print the value
					//than right shift, and the loop will start again, similarly it will go on and keep printing.
	}

}


int main(int argc, char *argv[])
{

	int number = 12;
	show_binary(number);	//just call the function once and it will print the binary for you

	return 0;
}

Hey I tried fixing the coding but I get error messages such as below, when pressing run/compile:

[Linker Error] undefined reference to 'show_binary(unsigned int)'
Id returned 1 exit status
[Build Error] [Output/MingW/right] Error 1


What's happening here? :(
Hey I think I'll just fail this project and try pass the next one, I don't think I could possibly finish it by tonight, very sorry for bothering you man. Your help was much appreciated. Thanks again buddy :)
its easy and you will clear this overnight.. just try it and dont lose hope. :)
send the final code. lets fix it.
I tried typing exactly what the 'right shift' in the textbook says and yet my C++ program wont compile it, so I'm assuming its missing files or somewhat. And if I can't get the rift shift working, the rest of the program, error checking and such... will not work either! sigh

double first;

first= atof(argv[1]);


if
(argv[2][0] == 'b1') // 3rd parameter is base 1
{
cout << first << endl; // I'm not sure how to convert it to binary code ..
;
return 0;
}

Am I sort of on the right track? I could repeat that code for b1 till b16, as that is the requirement?

ok. here it goes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <string.h>
#include <malloc.h>

#include <iostream>


using namespace std;

void show_binary(unsigned int u)
{
	while(u > 1)
	{
		if((u % 2) == 0)
		{
			std::cout << "0";
		}
		else
		{
			std::cout << "1";
		}

		u = u >> 1;
	}

	std::cout << "1" << std::endl;	//the last one. This must be a 1 everytime


	//for (t=0; t < 12 ; t++)		//run this loop till u is equal to 1, start from its original value
	//{		
	//	i = i << 1 ; //it should be right shift. sorry for that.
	//				//so here you will check if the number is even or odd using % operator
	//				//accordingly print the value
	//				//than right shift, and the loop will start again, similarly it will go on and keep printing.
	//}

}


int main(int argc, char *argv[])
{

	int number = 132;
	show_binary(number);	//just call the function once and it will print the binary for you

	return 0;
}


Now you have to modify this to make it work for all the bases. it will just work for base 2 at this moment.

Also, I am doing this but don't expect that you will get the solution every time.

enjoy.. :)
Last edited on
You're so kind.. I will try to modify this right away, thank you so much!
I've made one tiny tiny modification and that is:

double number ;
number = atof(argv[1]);
show_binary(number);

Instead of :

int number = 132;
show_binary(number);
-------- works-------------------------




ALSO

I tried to put :

If (argv [2] [0] == 'b2') //if parameter 3 is base 2, which is what you've coded
{
while(u > 1)
{
if((u % 2) == 0)
{
std::cout << "0";
}
else
{
std::cout << "1";
}

u = u >> 1;
}

------------does not work-----------
I don't think below code is doing the correct comparison assume argv is char *[]

 
If (argv [2] [0] == 'b2') //if parameter 3 is base 2, which is what you've coded 


Topic archived. No new replies allowed.