Link to home
Start Free TrialLog in
Avatar of gothic130
gothic130

asked on

Binary to decimal program question...

I'm working on  a program to convert decimal to binary but I don't understand so well the part under the main and why an error occurs in the line for(res=7;;)  could anyone explain me this? Please, thanks.

This is the program:

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

binary(int num, int arr[8])
{
 int base=2, temp, i=7;
 num=10;
 while(base>=num)
 {
  temp=num/base;
  digit=num%base;
  num=temp;
  arr[i]=digit;
  i--;
 }
 arr[i]=num;
}
main()
{
 int res[8];
 binary(10,res);
 for(res=7;;);
 printf("%d",res);
 getch();
}


Avatar of _nn_
_nn_

When you write

 res=7

you're trying to assign an integer (7) to an array of integers (res), and the compiler isn't happy...

Actually I think that what you want is rather something like :

main()
{
 int i, res[8];
 binary(10,res);
 for(i=7; i>0; i--)
     printf("%d",res[i]);
 getch();
}

Can you explain the changes I made ?
Oops, I made a mistake. Good exercise : can you find it ? :)
Avatar of gothic130

ASKER

I don't understand the binary(10,res) line, what does it do? :) it's an array or a function?
It's a function call. It is supposed to take a number (here 10) and output its binary representation in the res array.
isn't suppose to be i>=2?
Nope. Actually the for loop should be :

for(i=7; i>=0; i--)
jejeje i've change the program but it prints a lot of crazy numbers!!!!
Yup, because there are a lot of problems in the binary() function itself. I guess, if you ran it, it means that you declared the digit variable, else it wouldn't have compiled. :)

But for now, I'd like you to try a bit to find what are the logical problems in the binary() function. Please try, and if you're still stuck, paste your new code and we'll discuss it.
Why the i have to be 7 and not 8?
ASKER CERTIFIED SOLUTION
Avatar of _nn_
_nn_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> Why the i have to be 7 and not 8?

Because an array of 8 elements has indexes from 0 to 7.
Well to me the function binary is oK because it does the right process, but I don't know where the main function calls the binary function, I think it should be a line with arr[i] in the main fuction isn't it? or why can't I just print the arr[i]?
It's 3am here and I feel, I need to get some sleep. In 2 minutes, I'll give you a "solution" (working code). Please try first to find one yourself before reading this thread again.
#include <stdio.h>
#include <conio.h>

void binary(int num, int arr[8])
{
  int base=2, i;
  for(i=0; i<8; i++)
  {
    arr[i]=num%base;
    num=num/base;
  }
}

int main()
{
  int i, res[8];
  binary(10,res);
  for(i=7; i>=0; i--)
    printf("%d",res[i]);
  getch();
  return 0;
}

Well thank you I promise I won't see your code 'till my head explodes, :) Thank you very much(here is 9 PM) :)

Good bye.
Try this program which uses a structure of 8 bits long to convert a number less than 255 to binary....Going forward you can develop it for 16 bits or an integer number.


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


typedef struct byte
{
      unsigned int fi : 1; //First bit denoted by fi.
      unsigned int se : 1; //Second bit denoted by se.
      unsigned int th : 1; //Third bit denoted by th.
      unsigned int fo : 1; //Fourth bit denoted by fo.
      unsigned int ff : 1; //Fifth bit denoted by ff.
      unsigned int si : 1; //Sixth bit denoted by si.
      unsigned int sv : 1; //Seventh bit denoted by sv.
      unsigned int ei : 1; //Eighth bit denoted by ei.
} byte;
byte test;

void byte_print(byte);
void convert_byte(int);

void main()
{
      int a;
//      struct byte test;
      printf("Enter the number < 255 to be converted to binary");
      scanf("%d", &a);
      convert_byte(a);
      byte_print(test);
      getch();
}

void convert_byte(int x)
{
      int i;
//      extern byte test;
      test.fi = x & 1;
      x >>= 1;
      test.se = x & 1;
      x >>= 1;
      test.th = x & 1;
      x >>= 1;
      test.fo = x & 1;
      x >>= 1;
      test.ff = x & 1;
      x >>= 1;
      test.si = x & 1;
      x >>= 1;
      test.sv = x & 1;
      x >>= 1;
      test.ei = x & 1;
      x >>= 1;
}

void byte_print(byte x)
{
 printf("\n%d",x.ei);
 printf("%d",x.sv);
 printf("%d",x.si);
 printf("%d",x.ff);
 printf("%d",x.fo);
 printf("%d",x.th);
 printf("%d",x.se);
 printf("%d\n",x.fi);
}