This is the first "useful" program that I wrote in high school. It is designed to convert a decimal number to it's binary representation. There are many flaws in this code, but let's dig deeper.

#include <iostream>
using namespace std;
int sol[20];
int bin[20];
int number = 0;
int answer = 0;

I am confused by my choice to pass variables to each function as well as to use global variables. It is obvious that I did not understand the intricacies of variable scope. Since this was such a small program global variables did not cause a problem. I would not recommend that global variables be used for anything other than settings in most programs.

int input() {
  cout << "Enter number to convert to binary \t:"; cin >> number;
  return number;

This function works, but it should not be a separate function. I was excited that I could create multiple functions and still compile the code.

int* pwrsoftwo(int) {
  int loopcount = 1;
  while (loopcount <20) {
    sol[loopcount] = sol[loopcount-1]*2;
    loopcount = loopcount +1;
  return sol;

I chose to use an array of integers to represent a binary number. Looking back, a string would make much more sense to use here.

int* conversion(int,int*) {
  int x = 19;
  do {
  if (number- sol[x] - answer < 0)  bin[x] = 0;
  else {
    bin [x] = 1;
    answer = sol[x] + answer;
  } while(x > -1);
  return bin;

This function illustrates the importance of focusing on readability and comments while coding. It takes a few minutes of disection to understand what is going on, and more importantly it is not close to the most efficient solution.

void display(int*) {
  int y = 19;
  do {
   cout<< bin[y];
   y --;
  } while (y >-1);

int main() {

Overall the code works, but could be vastly improved. Using a vector to represent the number, or at least an array of bits, would make the code more efficient and readable. Bitshift would be a more elegant method of calculation and system("pause") should replace the last two lines of main.

I hope that this exercise has been useful to you. I know that it has helped me realize how far I have come in the last 10 years. If you are interested to see my web based version of this code visit my projects page.