How do you write this in C language?

mOeeOm

Platinum Member
Dec 27, 2004
2,588
0
0
Say I have 12 in base 3 and I want to convert it to base 10, so its 1 x 3^1 + 2 x 3^0 = 5 in base 10.

How would you write this in C language and make it work for any number you enter and an infinite amount of digits?

Look at the code I attached, I need to convert the operands into a different base before the arithmetic operation.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
For base-16 there is a well-defined set of digits above 9 (A-F) but that isn't the case for other bases.

_if_ your assignment allows you to set a maximum base of 36 then you can just use 0-9 + A-Z.

if not, about all that you can do is write out your base-X numbers as
digit-digit-digit or digit,digit,digit or digit(space)digit(space)digit where digit is a base-10 number.

for example, for base 16 the number 255 could be written as: 15,15
 

mOeeOm

Platinum Member
Dec 27, 2004
2,588
0
0
Originally posted by: DaveSimmons
For base-16 there is a well-defined set of digits above 9 (A-F) but that isn't the case for other bases.

_if_ your assignment allows you to set a maximum base of 36 then you can just use 0-9 + A-Z.

if not, about all that you can do is write out your base-X numbers as
digit-digit-digit or digit,digit,digit or digit(space)digit(space)digit where digit is a base-10 number.

for example, for base 16 the number 255 could be written as: 15,15

Ya, but writing digit-digit-digit wont convert it to base 10 from base 6...my assignment is to take any base 6 number and convert it to base 10, perform an arithmetic operation, and convert it back to base 10.

Now the arithmetic operation will be, say for multiplaction, operand1 * operand2, say its 25 * 25, how do I make operand1 and operand2 become base 10?

Thats what I need to know :*(
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
base 6 just uses 0-5 so that should be really easy.

for N digits left to right, just like you do it for base-10 or base-2:
digit1
* 6 + digit2
* 6 + digit3
...
* 6 + digit N

in other words either
- a loop 1... N times to add 1 new digit, first multiplying the sum by (base) if loop>1
or
- an initial assignment then a loop from 2..N
or
- while loop that stops when there are no more digits

Use char string functions (or other string-slicing you learned in class) to get the digits one-by-one
 

mOeeOm

Platinum Member
Dec 27, 2004
2,588
0
0
Originally posted by: DaveSimmons
base 6 just uses 0-5 so that should be really easy.

for N digits left to right, just like you do it for base-10 or base-2:
digit1
* 6 + digit2
* 6 + digit3
...
* 6 + digit N

in other words either
- a loop 1... N times to add 1 new digit, first multiplying the sum by (base) if loop>1
or
- an initial assignment then a loop from 2..N
or
- while loop that stops when there are no more digits

Use char string functions (or other string-slicing you learned in class) to get the digits one-by-one

Heh, I'm new to this so try to dumb it down a shade :p

What I'm trying to do it as

operand1 += digit * 6^n;
operand2 += digit * 6^n;

But how do I define n to be the position of the digit? Or is this completely wrong?

What is the code to put in?

This is my first class with programming and its my last, its mandatory for my engineering program and I just wanna do well on the 4 labs and go through it. So sorry if I'm asking obvious questions...
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
the way I wrote it you don't need to know n you figure it out as you go from left to right:
( ( (digit * 6) + digit 2)* 6 + digit 3 ) * 6 + diigit 4


for your way you would get the string for operand 1, use strlen(string) to count the number of digits, then start with
n = strlen()-1 as the initial power going from left to right
or
0 as the power going from right to left

given "5142" base 6, strlen = 4 so the n to start with is 4-1 = 3

so loop i from 0-3,
string[i = 0] = 5, sum += 5 * 6^3
string[i = 1] = 1, sum += 1 * 6^2

if you set n = strlen()-1 outside your loop you can loop i from 0....n and the power for digit i will be n-i
 

mOeeOm

Platinum Member
Dec 27, 2004
2,588
0
0
Originally posted by: DaveSimmons
the way I wrote it you don't need to know n you figure it out as you go from left to right:
( ( (digit * 6) + digit 2)* 6 + digit 3 ) * 6 + diigit 4


for your way you would get the string for operand 1, use strlen(string) to count the number of digits, then start with
n = strlen()-1 as the initial power going from left to right
or
0 as the power going from right to left

given "5142" base 6, strlen = 4 so the n to start with is 4-1 = 3

so loop i from 0-3,
string[i = 0] = 5, sum += 5 * 6^3
string[i = 1] = 1, sum += 1 * 6^2

if you set n = strlen()-1 outside your loop you can loop i from 0....n and the power for digit i will be n-i

We didn't learn string, so it would look suspicious if I used it :p

So far we learn if-else statments, do and loop stuff..so I guess I'll use the first method:

( ( (digit * 6) + digit 2)* 6 + digit 3 ) * 6 + diigit 4

But how do you make it for an infinite amount of digits?
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
The phrase string is being used as a character stirng array; Not a C++ object.

char string[80];

He is using indexing into the character array.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
if you can't use
char operand1[128] ;

then you will have to read input one character at a time, treat the first '0' - '9' as digit 1 then stop building operand1 when you read a non- '0' - '9' character.
 

Cooler

Diamond Member
Mar 31, 2005
3,835
0
0
[ATTACH CODE]
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <math.h>
using namespace std;

int main(int argc, char *argv[])
{


string first;
string last;
string myChar;
double baseten =0.0;
int baseten2 ;
cin>>first>>myChar>>last;

for (int i =0; i< first.length(); i++){

char aa = first;
istringstream a(&aa);
int number;
a>>number; // change the 2 to the base needed
baseten += number * pow( 2.0,first.length()-1 *1.0);
}
cout<<" "<<baseten ;
system("PAUSE");
return EXIT_SUCCESS;
}
[/ATTACH CODE]