Hi there Daniweb,
right now im in an engineering program and we are being put through a crash course in C. Ive never dont any type of programming before so it is quite an adventure (to say the least).
Currently we are doing an assignment in which we have to convert a binary number (base 2) to a decimal number (base 10). I think I have the beginnings of the program, but Im not sure how to proceed. Im not sure what variables i should be assigning and how to implement them.
we were given pseudo code to implement into the program, but i think im missing a lot.
Pseudo-code algorithm: (start by convincing yourself that this algorithm works)
1. Prompt the user to enter a binary number, followed by the Enter key
2. Assign variable decimalValue 0
3. Get the first binary digit and Assign variable nextDigit the value of the digit
4. While (there are still digits to be processed)
4.1 Assign variable decimalValue the value of (decimalValue * 2) + nextDigit
4.2 Get the next binary digit and Assign nextDigit the value of the digit
5. Output the value of variable decimalValue
so far i have this:
#include <stdio.h>
#include <stdlib.h>
int decimalValue;
int binnumber;
int main()
{
printf("Please enter a binary number: \n");
decimalValue = 0;
scanf("%i", &binnumber);
system("PAUSE");
return 0;
}
should my int binnumber actually be nextDigit (following the pseudo code)?
could someone please help guide me to the next step? i THINK i should be using an 'if' or "while" statement. i know that the program should terminate when the value returned = 0. i also know that in the calculations i need to have something along the lines of "x %10 = a" followed by " a/10 = b" (something along those lines, i used completely random variable letters there). my friend in the class said something about converting an int into a char, but i wasnt sure where he was going with that.lol
with regards to the binary numbers, how many letters should i assign? couldnt someone be difficult and enter a 10 digit binary? in that case, should i have 10 letters (one for each integer) for the binary to decimal function?
i know this must sound absolutely stupid, but this is a very foreign language to me (C, not english.lol) and we have only been taking the course for a few weeks. i cant stand how in the textbook all their examples are so simple! why cant they give harder examples!?lol
i really appreciate any help you can give me. im quite concerned about this as i feel like im only taking in small chunks of what i should be.
ill update the code portion of my post as i proceed. hopefully i dont make a complete arse of myself!
thanks in advance
DFE