C help

ISW

Member
mata me code eke error 1 kiyanawada ? meka liwwe decimal number 1k dunnama eke binary number 1 output karanna.. singlish walin comment dala athi.. loku udawwak...... :yes: :yes:


Code:
#include<stdio.h>
#include<conio.h>

void D2B (int num)
{
int binary[16];
int x;
for(x=0;x<16;x++) /*array eka 1,0 ewain koitharam durakata fill unada balanna*/
binary[x]=' ';
x=0;
while(num>=1) /*deken bedagena yanna*/
{
binary[x]=num%2;/* number eka deken bedala itiriya ganna*/
num/=2;/*number eka deken bedala  ayeth deken bedanna while eke mulata yawanna.*/
/*hithapan 5 gaththa kiyala 5ta 2 ewa 2i itiri 1i ne. 1 array ekata dagena apahu 2 2n bedanawa*/ 
x++; /*array eka count karanna*/
}
while(binary[x]!=' ')*/ koi welawaka hari mul while eka return wenakota array eka koi tharam fill welada balanna*/
x++;
for(;x<=0;x--)/* array eka reverce  order ekata print karanna*/
printf("%d",binary[x]);
}
void main()
{
int num;
clrscr();
printf("Enter your number :- ");
scanf("%d",&num);
D2B(num);
getch();
}
 
Last edited:

kasuncs

Well-known member
  • May 21, 2007
    3,590
    271
    83
    Machan balannam. meta passe danakota code kiyana tag eka atule meawa psot karanna.
     

    kasuncs

    Well-known member
  • May 21, 2007
    3,590
    271
    83
    Fixed code

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    void D2B (int num)
    {
    	int binary[16];
    	int x,i;
    	for(x=0;x<16;x++) /*array eka 1,0 ewain koitharam durakata fill unada balanna*/
    		binary[x]=0;
    	x=0;
    	while(num>=1) /*deken bedagena yanna*/
    	{
    		binary[x]=num%2;/* number eka deken bedala itiriya ganna*/
    		num/=2;/*number eka deken bedala ayeth deken bedanna while eke mulata yawanna.*/
    		/*hithapan 5 gaththa kiyala 5ta 2 ewa 2i itiri 1i ne. 1 array ekata dagena apahu 2 2n bedanawa*/
    		x++; /*array eka count karanna*/
    	}
    	//while(binary[x]!=' ')/* koi welawaka hari mul while eka return wenakota array eka koi tharam fill welada balanna*/
    	//	x++;
    	for(i=x;i>0;i--)/* array eka reverce order ekata print karanna*/
    		printf("%d",binary[i-1]);
    	printf("\n");
    }
    void main()
    {
    	int num;
    	printf("Enter your number :- ");
    	scanf("%d",&num);
    	D2B(num);
    	system("pause");
    }
     
    Last edited:

    kasuncs

    Well-known member
  • May 21, 2007
    3,590
    271
    83
    Use ANSI C and try to use debugger. I used Visual Studio 2008 Express to solve this.
     

    kasuncs

    Well-known member
  • May 21, 2007
    3,590
    271
    83
    Just test it. Hitapan screen shot ekak danna. Turbo C++ 3.0 kiyanne stone age compiler ekak. Program works.
     

    OptiplexFx

    Member
    May 29, 2009
    2,568
    44
    0
    cyberspace
    Here is a much shorter way of doing the same thing with recursion:

    Code:
    void D2B(int i)
    {
       int j = 0;
       if (i != 0)
       {
          j = i;
          D2B(i >> 1);
          printf("%d", j&0x01);
        }
    } 
    
    void main()
    {
    	int num;
    	printf("Enter your number :- ");
    	scanf("%d",&num);
    	D2B(num);
    	system("pause");
    }
     

    ISW

    Member
    kasuncs said:
    Fixed code

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    void D2B (int num)
    {
    	int binary[16];
    	int x,i;
    	for(x=0;x<16;x++) /*array eka 1,0 ewain koitharam durakata fill unada balanna*/
    		binary[x]=0;
    	x=0;
    	while(num>=1) /*deken bedagena yanna*/
    	{
    		binary[x]=num%2;/* number eka deken bedala itiriya ganna*/
    		num/=2;/*number eka deken bedala ayeth deken bedanna while eke mulata yawanna.*/
    		/*hithapan 5 gaththa kiyala 5ta 2 ewa 2i itiri 1i ne. 1 array ekata dagena apahu 2 2n bedanawa*/
    		x++; /*array eka count karanna*/
    	}
    	//while(binary[x]!=' ')/* koi welawaka hari mul while eka return wenakota array eka koi tharam fill welada balanna*/
    	//	x++;
    	for(i=x;i>0;i--)/* array eka reverce order ekata print karanna*/
    		printf("%d",binary[i-1]);
    	printf("\n");
    }
    void main()
    {
    	int num;
    	printf("Enter your number :- ");
    	scanf("%d",&num);
    	D2B(num);
    	system("pause");
    }

    machan methanadi array eka 0 fill karala ba.. because binary waladi use wenne 1,0 kiyana values ne.. mama try 1k dennam
     

    kasuncs

    Well-known member
  • May 21, 2007
    3,590
    271
    83
    Ccode.png
     

    ISW

    Member
    OptiplexFx said:
    Here is a much shorter way of doing the same thing with recursion:

    Code:
    void D2B(int i)
    {
       int j = 0;
       if (i != 0)
       {
          j = i;
          D2B(i >> 1);
          printf("%d", j&0x01);
        }
    } 
    
    void main()
    {
    	int num;
    	printf("Enter your number :- ");
    	scanf("%d",&num);
    	D2B(num);
    	system("pause");
    }

    thanks machan.. puluwannam printf("%d", j&0x01); methana wena de explain karanna
     

    OptiplexFx

    Member
    May 29, 2009
    2,568
    44
    0
    cyberspace
    & is a Bit wise AND operator, if you take j&0x01 that means whatever the value of j is compared with the value 0x01(hex) which is 00000001, and only the bits equal (in this case the right most one is returned as the value).
    In the above example take value 12
    Which translates in binary to 1100
    The recursive procedure does a bit shift to right each time until all the bits are shifted. That would look like this :
    Code:
    1100
    0110
    0011
    0001
    0000
    And if you take the bit wise AND with that value and print in the recursive (reverse) order you’ll get something like this :
    Code:
    0001 & 00000001= 1
    0011 & 00000001= 1
    0110 & 00000001= 0
    1100 & 00000001= 0

    Since you are printing from left to right your string will look like 1100 which is the binary representation of 12.
     

    ISW

    Member
    OptiplexFx said:
    & is a Bit wise AND operator, if you take j&0x01 that means whatever the value of j is compared with the value 0x01(hex) which is 00000001, and only the bits equal (in this case the right most one is returned as the value).
    In the above example take value 12
    Which translates in binary to 1100
    The recursive procedure does a bit shift to right each time until all the bits are shifted. That would look like this :
    Code:
    1100
    0110
    0011
    0001
    0000
    And if you take the bit wise AND with that value and print in the recursive (reverse) order you’ll get something like this :
    Code:
    0001 & 00000001= 1
    0011 & 00000001= 1
    0110 & 00000001= 0
    1100 & 00000001= 0

    Since you are printing from left to right your string will look like 1100 which is the binary representation of 12.

    Thanks Bro :cool: :cool:
     

    dissan

    Member
    Nov 13, 2007
    30
    0
    0
    Colombo
    ISW said:
    mata me code eke error 1 kiyanawada ? meka liwwe decimal number 1k dunnama eke binary number 1 output karanna.. singlish walin comment dala athi.. loku udawwak...... :yes: :yes:


    Code:
    #include<stdio.h>
    #include<conio.h>
    
    void D2B (int num)
    {
    int binary[16];
    int x;
    for(x=0;x<16;x++) /*array eka 1,0 ewain koitharam durakata fill unada balanna*/
    binary[x]=' ';
    x=0;
    while(num>=1) /*deken bedagena yanna*/
    {
    binary[x]=num%2;/* number eka deken bedala itiriya ganna*/
    num/=2;/*number eka deken bedala  ayeth deken bedanna while eke mulata yawanna.*/
    /*hithapan 5 gaththa kiyala 5ta 2 ewa 2i itiri 1i ne. 1 array ekata dagena apahu 2 2n bedanawa*/ 
    x++; /*array eka count karanna*/
    }
    while(binary[x]!=' ')*/ koi welawaka hari mul while eka return wenakota array eka koi tharam fill welada balanna*/
    x++;
    for(;x<=0;x--)/* array eka reverce  order ekata print karanna*/
    printf("%d",binary[x]);
    }
    void main()
    {
    int num;
    clrscr();
    printf("Enter your number :- ");
    scanf("%d",&num);
    D2B(num);
    getch();
    }


    machan..i complied this ... and the error is it prints an extra '32' with the required result....this is because u initialise the array with ' ' .the ascii value of ' ' is also 32.
    jus change ur code to after the the end of the "while (num>=1) " loop to:\


    x=0;
    while(binary[x]!=' ') /* koi welawaka hari mul while eka return wenakota array eka koi tharam fill welada balanna*/
    {

    x++;
    }
    for(j=x-1;j>=0;j--) /* array eka reverce order ekata print karanna*/
    printf("%d ",binary[j]);

    j is a newly declared integer...

    hope this helps