473,386 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

the power function

Hi, I usualy try to solve class problems on my own but I am hitting a wall.
I am supposed to write a power function and call it in main but having problems like
Expand|Select|Wrap|Line Numbers
  1. void power(int base,int exponent)
  2. {
  3.    for( i=0; i<exponent: i++ )
  4.    {
  5.    //This is my trouble area please help. Thanks
  6.  
  7.    }             
  8. }
Apr 5 '07 #1
40 22591
sicarie
4,677 Expert Mod 4TB
Ok, sure. Please use code tags to frame code next time - it helps with readability.

The trick there is to understand what the power function does. So, what does an exponent do, mathematically?
Apr 5 '07 #2
check this out

code removed per FAQ
Apr 5 '07 #3
ok My apologies. the power function raises the base by the exponent
like x^y
the x is raised to the y power or
x^5 = x*x*x*x*x
Apr 5 '07 #4
sicarie
4,677 Expert Mod 4TB
ok My apologies. the power function raises te base by the exponent like x^y
the x is raised to the y power or x^5 = x*x*x*x*x
It's not a big deal, it just helps with readability.

Right, so look at your for loop (specifically, 'exponent') variable, and think about how you could get that functionality.
Apr 5 '07 #5
sicarie
4,677 Expert Mod 4TB
Also, how many times does it loop? What do you have to do each single time it loops to get the answer?
Apr 5 '07 #6
It's not a big deal, it just helps with readability.

Right, so look at your for loop (specifically, 'exponent') variable, and think about how you could get that functionality.
i"m stuck..... Can you throw me a hint
Apr 5 '07 #7
sicarie
4,677 Expert Mod 4TB
i"m stuck..... Can you throw me a hint
I amended my last post with a reply - see if that helps.
Apr 5 '07 #8
Also, how many times does it loop? What do you have to do each single time it loops to get the answer, and how many times specifically does it loop?
the loop ends when the counter becomes less then the exponent. I was thinking that base=base*base but I knew that wouldnt work so I need a third variable I think to store the original base in and then multiply it by the base untill the loop ends
Apr 5 '07 #9
faceboy, do not post code again, I have warned you twice, and PM'd you about this.
Apr 5 '07 #10
sicarie
4,677 Expert Mod 4TB
the loop ends when the counter becomes less then the exponent. I was thinking that base=base+base but I knew that wouldnt work so I need a third variable I think to store the original base in and then multiply it by the base untill the loop ends
Well, look at your explanation of

x^5 = x*x*x*x*x

This needs to multiply x by x five times, correct?

In c/c++ you can do a nifty little shortcut of

variable += 1 as a shorthand of variable = variable +1

Now let me ask you - if you took x^2 * x^3, what do you get?
Apr 5 '07 #11
Well, look at your explanation of

x^5 = x*x*x*x*x

This needs to multiply x by x five times, correct?

In c/c++ you can do a nifty little shortcut of

variable += 1 as a shorthand of variable = variable +1

Now let me ask you - if you took x^2 * x^3, what do you get?

x^5 and using that method "variable += 1"confuses the crap out of me I rather just use the straight out one.
Apr 5 '07 #12
sicarie
4,677 Expert Mod 4TB
x^5 and using that method "variable += 1"confuses the crap out of me I rather just use the straight out one.
That's fine - whatever helps you keep it straight.

Hmm, ok, let's break down the for loop

for (a counter; while the counter is less than 'exponent'; increment the counter)
do something here to get the resulting number

This is the same as
do something here to get the resulting number
do something here to get the resulting number
do something here....
.
.
.
'exponent' number of times.

Is that starting to look something like a possible specific example of x^5 = x*x*x*x*x ?
Apr 5 '07 #13
That's fine - whatever helps you keep it straight.

Hmm, ok, let's break down the for loop

for (a counter; while the counter is less than 'exponent'; increment the counter)
do something here to get the resulting number

This is the same as
do something here to get the resulting number
do something here to get the resulting number
do something here....
.
.
.
'exponent' number of times.

Is that starting to look something like a possible specific example of x^5 = x*x*x*x*x ?

ok...i'm on the edge I know it =] just not quit there
Apr 5 '07 #14
sicarie
4,677 Expert Mod 4TB
ok...i'm on the edge I know it =] just not quit there
So you have a loop that will iterate exponent number of times, what action do you want to do one time for each iteration to get the final number?
Apr 5 '07 #15
So you have a loop that will iterate exponent number of times, what action do you want to do one time for each iteration to get the final number?

well you want the base * base so would sum = base * base work I dont think so because then I think the sum with then need to be multiplyed by the base? Am I making things more complex?
Apr 5 '07 #16
sicarie
4,677 Expert Mod 4TB
well you want the base * base so would sum = base * base work I dont think so because then I think the sum with then need to be multiplyed by the base? Am I making things more complex?
You are, but you're close. You're right about the base*base. Now think about the fact that you can multiply x^2 by x^3 and get x^5.

Another way of looking at it, x*x = x^2, right? Now let's call that product. What's product * x?

::Edit - I guess product would be more apt than sum, so I updated it.
Apr 5 '07 #17
You are, but you're close. You're right about the base*base. Now think about the fact that you can multiply x^2 by x^3 and get x^5.

Another way of looking at it, x*x = x^2, right? Now let's call that sum. What's sum * x?
x^3 because x^2 * x = x^3
Apr 5 '07 #18
Ganon11
3,652 Expert 2GB
You're so close. You're right in thinking that base = base * base; will not work, because with x = 5, your first iteration results in 25, then 625, then...etc.

If you were adding numbers together, you'd use a sum variable. Now, since we are adding, sum should start at 0, because 0 + x = x (This is the Addition Identity Rule). But you are multiplying. If you started sum as 0, then your result would be 0, because 0 * x = 0. What you need is a number n such that n * x = x. Can you think of the number that will fulfill this requirement? This is what your sum variable will start out as - then do sum = sum * base.
Apr 5 '07 #19
You're so close. You're right in thinking that base = base * base; will not work, because with x = 5, your first iteration results in 25, then 625, then...etc.

If you were adding numbers together, you'd use a sum variable. Now, since we are adding, sum should start at 0, because 0 + x = x (This is the Addition Identity Rule). But you are multiplying. If you started sum as 0, then your result would be 0, because 0 * x = 0. What you need is a number n such that n * x = x. Can you think of the number that will fulfill this requirement? This is what your sum variable will start out as - then do sum = sum * base.

well in your example n should start out with sum =1 but doesnt the sum = 1 restart the sum = sum * base when it loops?
Apr 5 '07 #20
sicarie
4,677 Expert Mod 4TB
The answer is what you start out with. What are you passed initially?
Apr 5 '07 #21
The answer is what you start out with. What are you passed initially?

I passed in base first and being base
Apr 5 '07 #22
Ganon11
3,652 Expert 2GB
well in your example n should start out with sum =1 but doesnt the sum = 1 restart the sum = sum * base when it loops?
OK, think through this example.

I've called the function, passing 5 and 3 (i.e. I want to find 5^3).

Now, sum starts as 1 before the loop.

First execution of the loop: sum = sum * base;
sum = 1 * 5;
sum = 5;

Second execution of the loop: sum = sum * base;
sum = 5 * 5;
sum = 25;

etc...

So, no, using sum = 1 will not reset the value, but setting sum = 0 will.
Apr 5 '07 #23
sicarie
4,677 Expert Mod 4TB
I passed in base first and being base
So you start out with base. Can you represent that as an exponent?
Apr 5 '07 #24
So you start out with base. Can you represent that as an exponent?

I dont quite get what you are trying to ask me
Apr 5 '07 #25
sicarie
4,677 Expert Mod 4TB
I dont quite get what you are trying to ask me
Look at what Ganon posted, and think about it - you're right there.
Apr 5 '07 #26
OK, think through this example.

I've called the function, passing 5 and 3 (i.e. I want to find 5^3).

Now, sum starts as 1 before the loop.

First execution of the loop: sum = sum * base;
sum = 1 * 5;
sum = 5;

Second execution of the loop: sum = sum * base;
sum = 5 * 5;
sum = 25;

etc...

So, no, using sum = 1 will not reset the value, but setting sum = 0 will.

ok I am seeing the working but I still dont get how you will declare the sum = 1
And in the loop the sum will have a new value but when the loop, loops, I beleive the sum will become 1 again.
Apr 5 '07 #27
Ganon11
3,652 Expert 2GB
It would if you declare sum within the loop. But you should declare it right before the loop enters.
Apr 5 '07 #28
hmmm like

for( i=0; i<exponent; i++; sum=1 )
{
sum= base* sum
}

then base which is the base will be multiplyed by the sum which os one but when the loop comes back around wont the sum be 1 again? This doesnt make sense to me.
Apr 5 '07 #29
Ganon11
3,652 Expert 2GB
No, since you are assigning the new value (sum * base) to sum, it will change values every time within the loop. Try it for yourself to see that it changes.
Apr 5 '07 #30
sicarie
4,677 Expert Mod 4TB
Try declaring sum right before your for loop.
Apr 5 '07 #31
Try declaring sum right before your for loop.
I get one error when I try to run it says parse error and points to
for (int i = 0; i < exp; i++; )
Apr 5 '07 #32
sicarie
4,677 Expert Mod 4TB
I get one error when I try to run it says parse error and points to
for (int i = 0; i < exp; i++;);
Just a slight syntax error - try removing the semi-colon from the right side of the for loop.
Apr 5 '07 #33
Just a slight syntax error - try removing the semi-colon from the right side of the for loop.
ahh ok I dont know why but that works so here is the thing
I input 5
then input 2
it says that the answer is 5
cout<<sum
Apr 5 '07 #34
sicarie
4,677 Expert Mod 4TB
ahh ok I dont know why but that works so here is the thing
I input 5
then input 2
it says that the answer is 5
cout<<sum
And your main follows pseudocode of:

declare vars
get input
call function
save function return value
print function return value

?
Apr 5 '07 #35
can I send in my code so you can see it
Apr 5 '07 #36
Expand|Select|Wrap|Line Numbers
  1.   for (int i = 0; i < exp; i++);
  2.   {
  3.     sum= base*sum;
  4.      return sum;
  5.    }
  6.  
Apr 5 '07 #37
sicarie
4,677 Expert Mod 4TB
Your return statement - you have it inside your for loop. This means you return on the first execution of the loop. You want to return when the loop is completed.
Apr 5 '07 #38
Expand|Select|Wrap|Line Numbers
  1.   for (int i = 0; i < exp; i++);
  2. {
  3.     sum= base*sum;
  4. }
  5.       return sum;
  6.  
  7. }
ok I should have seen that but in this one All I get in return is the origional value
Apr 5 '07 #39
sicarie
4,677 Expert Mod 4TB
Expand|Select|Wrap|Line Numbers
  1.   for (int i = 0; i < exp; i++);
  2.  
You still have a ';' after your for loop, remove that. For statements are a little weird in that they are not terminated by a semicolon, but the stuff inside them still is.
Expand|Select|Wrap|Line Numbers
  1. doStuff();
  2. for (part1; part1; part3) {             // no ';' here
  3.     initialize.someVariable();        // ended with a semicolon
  4. }                                              // no ';' here either
  5. doStuff(with.someVariable());     // ended with a semicolon
  6.  
Apr 5 '07 #40
Before I begin... To answer the question...

Expand|Select|Wrap|Line Numbers
  1. Code removed - please read posting guidelines.
  2.  
Oct 12 '07 #41

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: utab | last post by:
Hi there, Is there a way to convert a double value to a string. I know that there is fcvt() but I think this function is not a part of the standard library. I want sth from the standard if...
11
by: Russ | last post by:
I have a couple of questions for the number crunchers out there: Does "pow(x,2)" simply square x, or does it first compute logarithms (as would be necessary if the exponent were not an integer)?...
32
by: chris.fairles | last post by:
Just want an opinion. I have an algorithm that needs to run as fast as possible, in fact. Its already too slow. I've done as much algorithmic changes as I can to reduce the amount of code, so now...
11
by: anibio06 | last post by:
hi guys, i want to find 2^n (pow(2,n)). I am doing java.lang.Math and using power function. But error occured. Because my variable is integer type. but the function arguments are double. ...
5
by: Gus007 | last post by:
Hi all, Need the community great support once more. :) I need to know how to calculate the power of some numbers in C, the problem is that the number is too big , and the compiler gives a...
5
by: elsa | last post by:
hi i need help in writing a function integerPower(base,exponent) that returns the value base^exponent with assuming that the exponent is a positive, nonzero integer and that the base is an integer....
11
by: Chris Forone | last post by:
hello group, is there some reference implementation of a fast power function? how fast is the power function in <cmath>? thanks & hand, chris
9
by: suppamax | last post by:
Hi everybody! I'm writing a C program for a PIC18F microcontroller. I need to calculate a power function, in which both base and exponent are fixed point numbers (ex: 3.15^1.13). Using...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.