Here's the function:
Output:
//takes a number and a base to work to. It converts (just prints) this number to said base//base has to be greater then 1void printInBase(int number, int base) {assert(base > 1);//find out how many units is needed to make the number//int numberOfUnits = 1;int units = base;while (units <= number) {//numberOfUnits ++;units = units * base;}//make an array which will be printed out in the end//int numberArray [numberOfUnits];//work out which how many of each unit//int i = 0;int result = number;while (units > 1) {units = units / base;printf("%d", result / units);result = result % units;//i++;}//prints out array//for (int c = 0; c <= numberOfUnits ; c++) {// printf("%d", numberArray[c]);//}}
The function fails at counting base 1, but it hurts my brain too much to think about it! Also, to count in a base larger then 10, it's best to put a comma between digits. e.g:Type a starting number: 1Type an end number: 30Type a base to count in: 71 = 12 = 23 = 34 = 45 = 56 = 67 = 108 = 119 = 1210 = 1311 = 1412 = 1513 = 1614 = 2015 = 2116 = 2217 = 2318 = 2419 = 2520 = 2621 = 3022 = 3123 = 3224 = 3325 = 3426 = 3527 = 3628 = 4029 = 4130 = 42
This way you can distinguish between the 10's and 20's etc. Might be worth putting this in the function. But also, I could make it convert to hexadecimal? Might require more brain hurt.Type a starting number: 1Type an end number: 30Type a base to count in: 151 = 1,2 = 2,3 = 3,4 = 4,5 = 5,6 = 6,7 = 7,8 = 8,9 = 9,10 = 10,11 = 11,12 = 12,13 = 13,14 = 14,15 = 1,0,16 = 1,1,17 = 1,2,18 = 1,3,19 = 1,4,20 = 1,5,21 = 1,6,22 = 1,7,23 = 1,8,24 = 1,9,25 = 1,10,26 = 1,11,27 = 1,12,28 = 1,13,29 = 1,14,30 = 2,0,
No comments:
Post a Comment