• C

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();
}


gothic130Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_nn_Commented:
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 ?
0
_nn_Commented:
Oops, I made a mistake. Good exercise : can you find it ? :)
0
gothic130Author Commented:
I don't understand the binary(10,res) line, what does it do? :) it's an array or a function?
0
Managing Security Policy in a Changing Environment

The enterprise network environment is evolving rapidly as companies extend their physical data centers to embrace cloud computing and software-defined networking. This new reality means that the challenge of managing the security policy is much more dynamic and complex.

_nn_Commented:
It's a function call. It is supposed to take a number (here 10) and output its binary representation in the res array.
0
gothic130Author Commented:
isn't suppose to be i>=2?
0
_nn_Commented:
Nope. Actually the for loop should be :

for(i=7; i>=0; i--)
0
gothic130Author Commented:
jejeje i've change the program but it prints a lot of crazy numbers!!!!
0
_nn_Commented:
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.
0
gothic130Author Commented:
Why the i have to be 7 and not 8?
0
_nn_Commented:
I'll give you some hints.

Let's take a decimal number : 1243. It's representation actually means :

1 x 10^3 + 2 x 10^2 + 4 x 10^1 + 3 x 10^0

So, if we would consider powers of ten as "digits", that would give :
digit 3 : 1
digit 2 : 2
digit 1 : 4
digit 0 : 3

The formula to retrieve this "mapping" is simple and looks like the alogrythm in your binary() function

1243 / 10 = 124     --> use this for next loop
1243 % 10 = 3     --> "bit" 0 is 3

124 / 10 = 12     --> use this for next loop
124 % 10 = 4     --> "bit" 1 is 4

12 / 10 = 1     --> use this for next loop
12 % 10 = 4     --> "bit" 2 is 2

1 / 10 = 0     --> that's the last loop
1 % 10 = 1     --> "bit" 3 is 1

The same algorythm can be applied for any base, 2 for instance.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
_nn_Commented:
>> Why the i have to be 7 and not 8?

Because an array of 8 elements has indexes from 0 to 7.
0
gothic130Author Commented:
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]?
0
_nn_Commented:
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.
0
_nn_Commented:
#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;
}

0
gothic130Author Commented:
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.
0
kkpavankumarCommented:
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);
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.