5 Replies - 5640 Views - Last Post: 10 May 2009 - 06:13 AM Rate Topic: -----

#1 unrulycomplexity  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 09-May 09

Fast Exponentiation Algorithm

Post icon  Posted 10 May 2009 - 02:49 AM

Hi everyone, i am a noob in c++ and i have an assignment which requires me to do a program about fast exponentiation algorithm.
#include <cstdlib>
#include <iostream>
#include <sstream>

using namespace std;

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

	string power;
	string mod;
	int ans;
	int a;
	string d;
	//int p;
	int b;
	int i=23;
	char c[23];
   
	cout<<"Enter any Number to the power of any Number mod any Number in Decimal\nExample:123 ^ 456 mod 78\n";
	cin>>a>>power>>i>>mod>>b;
	d = itoa(i,c,2);
	std::istringstream stm;
	int p;
	stm.str(d);
	stm >> p;

  for(int m = 0; m<p.length(); m++)
	{
			
			if (p = 0)
			{
				  a=a*a;
			}
		   else if (p = 1)
			{
				  a=(a*a)*a;
			}
	}
	
	ans = a % b;
	
	cout<<"The answer is" << ans <<endl;
  

	
	system("PAUSE");
	return 0;
}


this is what i have done but it seems that there an error on the p.length. could someone help me with this? thxs in advance!!

Is This A Good Question/Topic? 0
  • +

Replies To: Fast Exponentiation Algorithm

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Fast Exponentiation Algorithm

Posted 10 May 2009 - 02:52 AM

Welcome to DIC!


Please give us some more details of your problem.
( a ) Does your code compile?
( b ) Any errors or warnings?
( c ) Is the program producing any output?
( d ) How is the actual output different to what you want / expect? Give details.

-------

You have declared "p" as an int.
You can't use length() on an int.
What were you trying to do there?

---

Also have a look at these
            if (p = 0)
...
           else if (p = 1)



Remember "=" and "==" are not the same thing.
Which one did you mean here?

---

This post has been edited by janotte: 10 May 2009 - 03:01 AM

Was This Post Helpful? 0
  • +
  • -

#3 unrulycomplexity  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 09-May 09

Re: Fast Exponentiation Algorithm

Posted 10 May 2009 - 03:03 AM

View Postjanotte, on 10 May, 2009 - 01:52 AM, said:

Welcome to DIC!


Please give us some more details of your problem.
( a ) Does your code compile?
( b ) Any errors or warnings?
( c ) Is the program producing any output?
( d ) How is the actual output different to what you want / expect? Give details.


thxs janotte for the welcome!! =)
after compiling my code it showed me this errors
28 `length' has not been declared
28 request for member of non-aggregate type before '(' token
Was This Post Helpful? 0
  • +
  • -

#4 unrulycomplexity  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 09-May 09

Re: Fast Exponentiation Algorithm

Posted 10 May 2009 - 03:42 AM

actually i was trying to convert a number to binary and to do some calculations on it.
an example would be: 15 ^17, i will need to take 17 to convert to binary which would be 10001 and i need to skip the first number and take the second number to calculate. so if the second number is 0 i would need to take 15*15 and if it was a 1 would need to take 15*15*15 therefore i would need to read every number starting from the 2nd till the end.

so if .length() cant be used is there any other way to read the binary number by number?
Was This Post Helpful? 0
  • +
  • -

#5 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Fast Exponentiation Algorithm

Posted 10 May 2009 - 04:04 AM

I am having trouble guessing at what your plan or design is.

Talk us through, line by line, what each line here is doing and why you want it done.
    cout<<"Enter any Number to the power of any Number mod any Number in Decimal\nExample:123 ^ 456 mod 78\n";
    cin>>a>>power>>i>>mod>>b;
    d = itoa(i,c,2);
    std::istringstream stm;
    int p;
    stm.str(d);
    stm >> p;


Was This Post Helpful? 0
  • +
  • -

#6 unrulycomplexity  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 09-May 09

Re: Fast Exponentiation Algorithm

Posted 10 May 2009 - 06:13 AM

oh its ok janotte i have already figured it out myself. thxs for all the help!! =D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1