9 Replies - 1499 Views - Last Post: 07 January 2009 - 11:31 PM Rate Topic: -----

#1 frosha   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-January 09

Convert an integer in base b1 to an integer in base b2

Posted 07 January 2009 - 02:28 AM

Hi,

Am frosha and please I need a help with my project
its in microsoft visual C++


1. Theme: Implementing a course related C++ program.


2. Objectives:
a. Have the students to practice programming in C++.
b. Have the students to practice documenting their programs.
c. Have the students to practice numbering conversion.
d. Have the students to practice analyzing the program time complexity.


3. Topic: Implement in C++ a program to solve the following problem:
Covert an integer in base b1 to an integer in base b2, where b1 and b2 are integers and 2≤b1≤10 and 2≤b2≤10.



This is my code

#include <iostream.h>
#include <cmath>
int main()
{int FBase , SBase ,number, counter,n[10],q;
cout << "select the base from 2 to 10 :" ;
cin  >> FBase ;
cout << "write the Number which you want to convert : " ;
cin >> number ;
q= 0 ;
counter = 0 ;
while ( number != 0 ) 
{
q += (number %10 * pow(FBase,counter) );
number  = number/10;
counter++;
}
cout << "convert to decimal = " << q  << endl;
cout << "select the base to convert " ;
cin >> SBase ;
counter = 0 ; 
while ( q != 0 ) 
{
n[counter] =q % SBase ;
q = q / SBase ;
counter = counter + 1 ;
} 
for (counter = counter -1  ; counter >= 0 ; counter--)
cout << n[counter] ;
cout << endl;
system("pause");
return 0 ;
}




I have one ERROR
I realy dont know how to handle it

please also I want to know if its right with the problem or not !!


I realy need to solve this code right
please :(


Waiting..[u]
:unsure:

** Edit ** :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Convert an integer in base b1 to an integer in base b2

#2 AmitTheInfinity   User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: Convert an integer in base b1 to an integer in base b2

Posted 07 January 2009 - 02:44 AM

I haven't tried to run your code, but I found this one
q += ((number %10) * (int)pow(FBase,counter) ); function pow returns a double data type result. So result of total expression becomes double, whereas your RHS is integer [variable q]. So either make q as a double or typecase the result of pow the way I did.

I hope this will help you. :)
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6642
  • View blog
  • Posts: 30,931
  • Joined: 10-May 07

Re: Convert an integer in base b1 to an integer in base b2

Posted 07 January 2009 - 04:04 AM

** Renamed title to be descriptive of problem **
Was This Post Helpful? 0
  • +
  • -

#4 frosha   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-January 09

Re: Convert an integer in base b1 to an integer in base b2

Posted 07 January 2009 - 05:13 AM

View PostAmitTheInfinity, on 7 Jan, 2009 - 01:44 AM, said:

I haven't tried to run your code, but I found this one
q += ((number %10) * (int)pow(FBase,counter) ); function pow returns a double data type result. So result of total expression becomes double, whereas your RHS is integer [variable q]. So either make q as a double or typecase the result of pow the way I did.

I hope this will help you. :)



I Understund, but the problem is that I dont know how to solve the ERROR :(
Was This Post Helpful? 0
  • +
  • -

#5 AmitTheInfinity   User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: Convert an integer in base b1 to an integer in base b2

Posted 07 January 2009 - 05:18 AM

Replace your statement with the one I posted.
Was This Post Helpful? 0
  • +
  • -

#6 frosha   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-January 09

Re: Convert an integer in base b1 to an integer in base b2

Posted 07 January 2009 - 06:26 AM

View PostAmitTheInfinity, on 7 Jan, 2009 - 04:18 AM, said:

Replace your statement with the one I posted.



!!!
you are right
We forget some thing here !
Thanx so much :D


but now, the code is right with the main problem?
Was This Post Helpful? 0
  • +
  • -

#7 AmitTheInfinity   User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: Convert an integer in base b1 to an integer in base b2

Posted 07 January 2009 - 06:35 AM

I would suggest you to validate the number [to be converted] for given base before converting it to decimal.

for example if I input First base as 2 but I entered number to be converted as 3, and program will still continue! even though there is no 3 in binary base!

I haven't checked whether it works or not, but I don't find any logical mistakes in it so It should work for you.

This post has been edited by AmitTheInfinity: 07 January 2009 - 06:36 AM

Was This Post Helpful? 0
  • +
  • -

#8 frosha   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-January 09

Re: Convert an integer in base b1 to an integer in base b2

Posted 07 January 2009 - 09:37 AM

View PostAmitTheInfinity, on 7 Jan, 2009 - 05:35 AM, said:

I would suggest you to validate the number [to be converted] for given base before converting it to decimal.

for example if I input First base as 2 but I entered number to be converted as 3, and program will still continue! even though there is no 3 in binary base!

I haven't checked whether it works or not, but I don't find any logical mistakes in it so It should work for you.



OK I will see and tell you about it

THANX you ..
:wub:
Was This Post Helpful? 0
  • +
  • -

#9 frosha   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-January 09

Re: Convert an integer in base b1 to an integer in base b2

Posted 07 January 2009 - 11:27 PM

View PostAmitTheInfinity, on 7 Jan, 2009 - 01:44 AM, said:

I haven't tried to run your code, but I found this one
q += ((number %10) * (int)pow(FBase,counter) ); function pow returns a double data type result. So result of total expression becomes double, whereas your RHS is integer [variable q]. So either make q as a double or typecase the result of pow the way I did.

I hope this will help you. :)




Still there is one ERROR :(


error C2065: 'system' : undeclared identifier

??
Was This Post Helpful? 0
  • +
  • -

#10 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6642
  • View blog
  • Posts: 30,931
  • Joined: 10-May 07

Re: Convert an integer in base b1 to an integer in base b2

Posted 07 January 2009 - 11:31 PM

View Postfrosha, on 8 Jan, 2009 - 12:27 AM, said:

error C2065: 'system' : undeclared identifier


Add the following includes to your code.

#include <stdio.h>
#include <stdlib.h>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1