0

Please help me my teacher gave me a program and I don't know how to do it. So this the program the she gave me.
"Conversion from any Base to Base 10". Please help me :(

4
Contributors
5
Replies
28
Views
3 Years
Discussion Span
Last Post by Lucaci Andrew
0

Do you know how to do this on paper? Do you understand what is meant by a number being in a base other than base 10?

0

Yeah I know this on paper. But making a program with it I can't. And my teacher said it should be in array.

0

learning to do it in code is what you're taking those classes for. So go ahead and try...
If you just wait for others to do it for you and hand in what they have done as your own you've learned nothing except how to be a dishonest, cheating, lazy, profiteering, liar.

0

Heres's my program and I don't if this right or wrong. And plus I need this to be in array.

#include<iostream.h>
#include<conio.h>
int convert(int a,int base)
{
int sum=a%10;
for(int i=base;(a/=10)!=0;i*=base)
sum+=a*i;
return sum;
}

int main()
{
    int a,base,ans;

    cout << "Enter a number: ";
    cin >> a;

    cout << "Enter a base: ";
    cin >> base;

    ans=convert(a,base);

    cout << "the converted base 10 is"<<ans;
    getch();
    return 0;
}   

Edited by jr.sayre3

0

Here, this should pretty much clear everything uncertain about converting any number to base 10: Click Here

Also, your function is wrong, don't rush through steps, it will get you confused:

#include <cmath>
int ToBase10(int number, int base){
    int ret = 0;                        //return number;
    for (int i = 0; i < base; i++){     //loop for computing the base power
        //add to your number the last digit raised to base^i
        ret += number % 10 * std::pow((double)base, (double)i);
        number /= 10;                   //slice the last digit
    }
    return ret;
}

Edited by Lucaci Andrew

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.