Solved

Solved
Last edited on
Try to firstly describe or write down the mathematical process of converting a number from base 10 to base 2. Compare it with your code.
You could consider this function to help you.

1
2
3
4
5
6
7
8
9
10
11
void Convert_To_Base_2(int num)
{
	int Array[100];
	int i = 0;
	while(num != 0)
	{
		Array[i] = (num % 2);
		num /= 2;
		i++;
	}
}
Last edited on
I didn't because If you know c++/c/c# you have to know what that means and it is.
Here is the full program for that:

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
#include <iostream>

using namespace std;

void Convert_From_Base_10_To_Base_2(int num);

int main()
{
	int number;

	cin >> number;

	Convert_From_Base_10_To_Base_2(number);

	system("pause");
	return 0;
}

void Convert_From_Base_10_To_Base_2(int num)
{
	int Array[100];
	int i = 0;
	while(num != 0)
	{
		Array[i] = (num % 2);
		num /= 2;
		i++;
	}
	for(int j = i-1; j >= 0; j--)
	{
		cout << Array[j];
	}
	cout << endl;
}
I compared it and it's fine.
P.S. I'm novice.
I know it's fine. I wrote it right now. So, I guess you an close the topic. Unless you want more explanations.

I hope I was helpful,
~ Raul ~
Well, if you need further help, you can always PM me. :)
I didn't asked for another code. I need help how to solve mine.
Last edited on
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
#include <iostream>
#include <conio.h>
using namespace std;
int main(){
	int base10, base2=0, aux, inv=0;
	cout<<"\n\n\n             This program transfers a number from base 10 to base 2.";
	cout<<"\n\n   Type the number, from base 10: ";
	cin>>base10;
	aux=base10;
	cout<<"\n   Graphical scheme of the calculation...";

	// write this in your code
	int array[100], i = 0;  // ADD ----------------------------------

	while(aux>0){
		cout<<"\n "<<aux<<""; 
       	cout<<" ";
		if(aux%2==0) { cout<<0; array[i] = 0; } // ADD ----------------------------------
		else if(aux%2==1) { cout<<1; array[i] = 1; } // ADD ----------------------------------
       	base2=base2*10+aux%2;
	    aux=aux/2;
		i++; // ADD ----------------------------------
	}


	cout<<"\n 0";
    
	/* ---------------------------------- delete 
	while(base2>=0){             //He's from tale to head.
        inv=inv+base2%10;
        base2=base2/10;
    }
	*/

	cout<< endl << "The number "<<base10<<" from base 10 to bese 2 is "; // <<inv<<"."; <-- Not good; ----------------------------------

	 // ADD ----------------------------------
		for (int j = i-1; j >= 0; j--)
			cout << array[j];
		cout << ".";
	// ---------------------------------------

	cout<<"\n\n All rights reserved.\n Isengardium Company...\n Version 1.0\n Press any key to continue...";
	  getch();
	}
That's what you need to modify in order to get your code working. I would recommend using the once I gave you, but if you really need this one, okay.
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
#include <iomanip> // ADD ----------------------------------
#include <iostream>
#include <conio.h>
using namespace std;
int main(){
	int base10, base2=0, aux, inv=0;
	cout<<"\n\n\n             This program transfers a number from base 10 to base 2.";
	cout<<"\n\n   Type the number, from base 10: ";
	cin>>base10;
	aux=base10;
	cout<<"\n   Graphical scheme of the calculation...";

	// write this in your code
	int array[100], i = 0;  // ADD ----------------------------------

	while(aux>0){
		cout<<"\n " <<  setw(6) << aux<<""; // MODIFY THIS ----------------------------------
       	cout<<" ";
		if(aux%2==0) { cout<<0; array[i] = 0; } // ADD ----------------------------------
		else if(aux%2==1) { cout<<1; array[i] = 1; } // ADD ----------------------------------
       	base2=base2*10+aux%2;
	    aux=aux/2;
		i++; // ADD ----------------------------------
	}


	cout<<"\n 0";
    
	/* ---------------------------------- delete 
	while(base2>=0){             //He's from tale to head.
        inv=inv+base2%10;
        base2=base2/10;
    }
	*/

	cout<< endl << "The number "<<base10<<" from base 10 to bese 2 is "; // <<inv<<"."; <-- Not good; ----------------------------------

	 // ADD ----------------------------------
		for (int j = i-1; j >= 0; j--)
			cout << array[j];
		cout << ".";
	// ---------------------------------------

	cout<<"\n\n All rights reserved.\n Isengardium Company...\n Version 1.0\n Press any key to continue...";
	  getch();
	}


That's it.

Including <iomanip> and using the setw(n) function allows you to set the width of the space where the letters are printed, which alligns them as you wished.

~ Raul ~
Last edited on
Topic archived. No new replies allowed.