GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Nov-2005, 06:50
harry.net harry.net is offline
New Member
 
Join Date: Oct 2005
Posts: 5
harry.net is on a distinguished road

convert array of decimal to array of binary


I have written a program to convert an array of decimal Numbers to its binary.

input is an array of size 8 , input:input[3]
cout put is an array of size 32, means each integer number in input should be represented as 4-bit in output. output: output[12]

for example this program run as below:
enter a string of NO
1
2
3
1000010011000000

this is wrong because it is showing the integer in reverse order, instead of showing 1 as 0001 it has showed 1000.

would you please check my program and tell me what's wrong with it?


CPP / C++ / C Code:
#include<iostream.h>

main()
{
	int key[3],x,j,b[13],n,m=1,d;

cout<<"enter a string of NO";

for(int i=0;i<3;i++)
		 cin>>key[i];


for ( int k=0;k<4;k++)
{
	
	x=key[k];
	 
	n=m*4;
	j=4;
	
	
	for (j=4;j>=1;j--)
	{

		d=x%2;
		b[n]=d;
		x=x/2;
		cout<<b[n];
		n--;
		
		
		
	}
	

	
	 m=m+1;
}

     return 0;

}
Last edited by LuciWiz : 01-Nov-2005 at 06:53. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 01-Nov-2005, 12:25
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: convert array of decimal to array of binary


Quote:
Originally Posted by harry.net
I have written a program to convert an array of decimal Numbers to its binary.

input is an array of size 8 , input:input[3]
cout put is an array of size 32, means each integer number in input should be represented as 4-bit in output. output: output[12]

for example this program run as below:
enter a string of NO
1
2
3
1000010011000000

this is wrong because it is showing the integer in reverse order, instead of showing 1 as 0001 it has showed 1000.

would you please check my program and tell me what's wrong with it?


CPP / C++ / C Code:
#include<iostream.h>

main()
{
    int key[3], x, j, b[13], n, m=1, d;
    
    cout<<"enter a string of NO";

    for(int i = 0; i < 3; i++)
            cin >> key[i];

    for ( int k=0; k < 4; k++)
    {
        x = key[k];
        n = m * 4;
        j = 4;

        for (j = 4; j >= 1; j--)
        {
            d = x % 2;
            b[n] = d;
            x = x / 2;
            cout << b[n];
            n--;
        }

        m = m + 1;
    }
    return 0;
}
First is your formatting. I made it more consistant. Use SPACEs insead of TABs to control your format easier. And use more SPACEs in each line.

Why is your first loop inputting 0 < 3 and your second 0 < 4? This will cause a problem.

The reason you are outputting the values in reverse order is you generate each value and put it in the array in reverse order. But you output the value immediately. You want to do your ourput after you totaly load the 'b' array.

Also note you never load b[0] since n counts from 4 to 1, not 3 to 0.

Oh, and read the Guidelines please.
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
  #3  
Old 02-Nov-2005, 00:16
harry.net harry.net is offline
New Member
 
Join Date: Oct 2005
Posts: 5
harry.net is on a distinguished road

Re: convert array of decimal to array of binary


Thank you so much for your help.

I changed the things which you have mentioned, and now the program is working properly.

here is the modified program.



Code:
CPP / C++ / C Code:
#include<iostream.h>

int b[11];  //modified from local to global

main()
{
	int key[3], x, j, n, m=1, d;

	cout<<"enter a string of NO";

	for( int i = 0; i < 3; i++ )
		 cin>>key[i];


	for ( int k = 0; k < 3; k++ )   //modified from 4 to 3
	{
	
		x = key[k];
		n = m * 4;
		j = 3;
		n--;              // modified to start from position 3, 7, 11
	
	
		for ( j = 4; j >= 1; j-- )    
		{

			d = x % 2;
			b[n] = d;
			x = x / 2;
		    n--;
		}
	
	 m = m + 1;

}

   cout << "dec to bin ";      //modified to print the output not in reverse order
   for ( int q1 = 0; q1 <= 11 ; q1++ )
   cout << b[q1];
   cout << endl;

   return 0;

}
 

Recent GIDBlogNARMY by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
convert char array into CString? nasaiya MS Visual C++ / MFC Forum 11 22-Aug-2005 03:13
template comiling problems - need expert debugger! crq CPP / C++ Forum 1 01-Feb-2005 21:26
Hex Result giving strange answers. Rosdahale C Programming Language 6 07-Dec-2004 20:28
Creating N string gwk C Programming Language 3 20-Jul-2004 23:27

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:23.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.