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.
void show_binary(unsignedint 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 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 :)
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
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <iostream>
usingnamespace std;
void show_binary(unsignedint 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.