Solved

Binary to decimal program question...

Posted on 2003-10-25
345 Views
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();
}

0
Question by:gothic130
• 9
• 6

LVL 16

Expert Comment

ID: 9621219
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

LVL 16

Expert Comment

ID: 9621223
Oops, I made a mistake. Good exercise : can you find it ? :)
0

Author Comment

ID: 9621226
I don't understand the binary(10,res) line, what does it do? :) it's an array or a function?
0

LVL 16

Expert Comment

ID: 9621231
It's a function call. It is supposed to take a number (here 10) and output its binary representation in the res array.
0

Author Comment

ID: 9621240
isn't suppose to be i>=2?
0

LVL 16

Expert Comment

ID: 9621246
Nope. Actually the for loop should be :

for(i=7; i>=0; i--)
0

Author Comment

ID: 9621253
jejeje i've change the program but it prints a lot of crazy numbers!!!!
0

LVL 16

Expert Comment

ID: 9621260
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

Author Comment

ID: 9621293
Why the i have to be 7 and not 8?
0

LVL 16

Accepted Solution

_nn_ earned 500 total points
ID: 9621296
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

LVL 16

Expert Comment

ID: 9621303
>> Why the i have to be 7 and not 8?

Because an array of 8 elements has indexes from 0 to 7.
0

Author Comment

ID: 9621319
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

LVL 16

Expert Comment

ID: 9621330
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

LVL 16

Expert Comment

ID: 9621332
#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

Author Comment

ID: 9621338
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

Expert Comment

ID: 9622749
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

Featured Post

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: Iâ€™ve never triâ€¦
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were smallâ€¦
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.