This forum is in archive mode. You will not be able to post new content.

Author Topic: Base Convertor  (Read 3991 times)

0 Members and 1 Guest are viewing this topic.

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
Base Convertor
« on: March 11, 2013, 10:44:44 AM »
This simple base convertor converts between base 2 and and base 16(including them).
Sorry, I didn't wrote comments. ;)

Code: (C) [Select]

#include <stdio.h>


int      convertedNumber[64];
long int numberToConvert;
int      base;
int      digit = 0;


void getNumberandBase ();


void convertNumber ();


void showConvertedNumber ();


int main() {


getNumberandBase();


convertNumber();


showConvertedNumber();


return 0;
}


void getNumberandBase () {


printf("Enter a number to be converted: ");


scanf("%li", &numberToConvert);


printf("Enter the base: ");


scanf("%i", &base);


if(base < 2 || base > 16){
 printf("You entered a wrong base.\n The base should be between 2 and 16.\n");
 base = 10;
}


}


void convertNumber (){
do {


   convertedNumber[digit] = numberToConvert % base;


   ++digit;


   numberToConvert/= base;
}while(numberToConvert != 0);


}


void showConvertedNumber() {


const char baseDigits[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};


int nextDigit;


printf("Converted number = ");


for(--digit; digit >= 0; --digit) {
 
  nextDigit = convertedNumber[digit];


  printf("%c", baseDigits[nextDigit]);
}


printf("\n");


}
« Last Edit: March 11, 2013, 10:45:12 AM by parad0x »

Offline 0poitr

  • Peasant
  • *
  • Posts: 149
  • Cookies: 64
    • View Profile
Re: Base Convertor
« Reply #1 on: March 11, 2013, 03:33:18 PM »
Just a quick note:

When you're declaring func. prototypes, you're also supposed to declare the parameters and their types.
Since your functions don't take any, this would be

  • void getNumberandBase (void);
  • void convertNumber (void);
  • void showConvertedNumber (void);
This doesn't change the program or anything. But it's a convention and helps the compiler to point out if there are inconsistancies b/w your func prototype and function calls, i.e. if the order/type/number of parameters.don't match.
Imagination is the first step towards Creation.

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
Re: Base Convertor
« Reply #2 on: March 11, 2013, 04:08:39 PM »
Just a quick note:

When you're declaring func. prototypes, you're also supposed to declare the parameters and their types.
Since your functions don't take any, this would be

  • void getNumberandBase (void);
  • void convertNumber (void);
  • void showConvertedNumber (void);
This doesn't change the program or anything. But it's a convention and helps the compiler to point out if there are inconsistancies b/w your func prototype and function calls, i.e. if the order/type/number of parameters.don't match.
Thanks, I'll take care of that.

Offline Super_mario666

  • Knight
  • **
  • Posts: 160
  • Cookies: 7
  • Professional Badass
    • View Profile
Re: Base Convertor
« Reply #3 on: March 11, 2013, 05:23:06 PM »

You should avoid using global variables in your programs, instead declare it in a function and pass it by reference into other functions.

The reason for this is once you start making larger programs, any function anywhere in the problem can change it, so if you or a partner decide to change the value of a global variable somewhere in the program and it creates a bug, you would have to sift though 1000s of line of code to find it.

Global variables should only be declared as a constants to avoid this.
« Last Edit: March 11, 2013, 05:24:45 PM by Super_mario666 »
The Bigger they are...The more likely you'll get your ass kicked

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: Base Convertor
« Reply #4 on: March 25, 2013, 06:53:54 PM »
Just a quick note:

When you're declaring func. prototypes, you're also supposed to declare the parameters and their types.
Since your functions don't take any, this would be

  • void getNumberandBase (void);
  • void convertNumber (void);
  • void showConvertedNumber (void);
This doesn't change the program or anything. But it's a convention and helps the compiler to point out if there are inconsistancies b/w your func prototype and function calls, i.e. if the order/type/number of parameters.don't match.

Fairly well said.  I think it should be added that, as indicated, the type is important though not the variable name.  This means that you can declare a prototype as such:
Code: (c) [Select]
int func(int, char, char*, char*, userType*);

You should avoid using global variables in your programs, instead declare it in a function and pass it by reference into other functions.

The reason for this is once you start making larger programs, any function anywhere in the problem can change it, so if you or a partner decide to change the value of a global variable somewhere in the program and it creates a bug, you would have to sift though 1000s of line of code to find it.

Global variables should only be declared as a constants to avoid this.

Absolutely agreed.  Avoid using global variables unless absolutely necessary.  This is a lesson best learned now rather than later.  Even at work, in a professional environment with learned individuals, I have to suffer through code that was not written with this in mind and thus I am constantly finding small bugs where a global value has been changed without concern for what else needs to use that variable.  This is made far worse by multithreaded code.  Adding mutexes to protect the use of global variables is not a solution and introduces further problems, particularly in performance.  Since I am paid salary, it is a serious waste of my time to run through the 2+ GBs of source that we maintain at work to resolve issues with global variables that never should have appeared in the code to begin with.
« Last Edit: March 27, 2013, 07:22:00 PM by Xires »
-Xires

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
Re: Base Convertor
« Reply #5 on: March 26, 2013, 06:52:35 AM »

Offline rasenove

  • Baron
  • ****
  • Posts: 950
  • Cookies: 53
  • ಠ_ಠ
    • View Profile
Re: Base Convertor
« Reply #6 on: March 26, 2013, 08:05:23 AM »
Hes going modify that post and write something  when he gets time. Mean while, that post will hold the place for him to write.
My secrets have secrets...

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.