How to I write an equation to raise a variable to another variable using a for loop?

I can't use Math.Pow() here, it has to be a loop. If there's an easier loop to use here, I would definitely like some help, lmao
92 Replies
Jimmacle
Jimmacle2y ago
a for loop works for this you just have to put all the other stuff in the right place think about what you need to put inside the loop and how many times the loop needs to run
CallMeSpriggy
CallMeSpriggyOP2y ago
It needs to run 5 times And I know the equation has to go in there (number^number) but I don't know how to write it in a way that actually works
Jimmacle
Jimmacle2y ago
what does it mean to raise a number to the power of another number?
CallMeSpriggy
CallMeSpriggyOP2y ago
multiply it by itself however many times the exponent says
Jimmacle
Jimmacle2y ago
sounds like something a loop might be good at
CallMeSpriggy
CallMeSpriggyOP2y ago
but I don't know how to write it like that what, like root * root?
Jimmacle
Jimmacle2y ago
yep (i'm assuming you aren't expected to handle negative exponents)
CallMeSpriggy
CallMeSpriggyOP2y ago
no
CallMeSpriggy
CallMeSpriggyOP2y ago
this is wrong, isn't it
Jimmacle
Jimmacle2y ago
it's closer, but you're not including the result of any previous multiplications you'll also have to change the first line to loop the correct number of times
CallMeSpriggy
CallMeSpriggyOP2y ago
so result++?
Jimmacle
Jimmacle2y ago
not really, break down the steps of something short like 2^3
CallMeSpriggy
CallMeSpriggyOP2y ago
? 2 * 2 * 2
Jimmacle
Jimmacle2y ago
right, then what steps are left to solve it
CallMeSpriggy
CallMeSpriggyOP2y ago
uhhh
Jimmacle
Jimmacle2y ago
just order of operations you start with a 2, multiply that by 2, then multiply the result of that (4) by 2
VitalJeevanjot
root = root*root ?
Jimmacle
Jimmacle2y ago
don't give answers please
VitalJeevanjot
pardon ? aren't you asking for one ?
Jimmacle
Jimmacle2y ago
no i'm helping someone learn to solve a problem giving away the answer undermines that
VitalJeevanjot
maybe but if you know how to teach then not let me try if you don't mind which is
Jimmacle
Jimmacle2y ago
excuse me?
VitalJeevanjot
lol, you do nvm then
CallMeSpriggy
CallMeSpriggyOP2y ago
but how would root = root*root work
VitalJeevanjot
good question
Jimmacle
Jimmacle2y ago
it doesn't which is why people should only butt in if there's a good reason to
VitalJeevanjot
please don't use 3rd grade language
MODiX
MODiX2y ago
jimmacle#0000
you start with a 2, multiply that by 2, then multiply the result of that (4) by 2
React with ❌ to remove this embed.
Jimmacle
Jimmacle2y ago
see if you can translate this to code not necessarily in a loop, but just write out that process
CallMeSpriggy
CallMeSpriggyOP2y ago
2*2=4*2
Jimmacle
Jimmacle2y ago
i mean code that will run in C#
CallMeSpriggy
CallMeSpriggyOP2y ago
I'm,,,, very new to this sorry
Jimmacle
Jimmacle2y ago
that's why i'm breaking it down so if you want to start with a 2, you could consider doing result = root
VitalJeevanjot
now you getting it
Jimmacle
Jimmacle2y ago
then how would you make result have the value of for example, root^3 dude, i'm not the one with the problem. please don't interrupt unless you have something to contribute
VitalJeevanjot
i said root = root*root which was call to think for it but you was rude
Jimmacle
Jimmacle2y ago
but that's wrong
VitalJeevanjot
so that's what i said
Jimmacle
Jimmacle2y ago
and not helpful to the person trying to learn
VitalJeevanjot
well as long as you can connect with him then sure...
CallMeSpriggy
CallMeSpriggyOP2y ago
the ^ operator doesn't work in c# At least, my version of it
Jimmacle
Jimmacle2y ago
right, which is why i asked you to break down calculating the power to a more basic operation which is multiplication exponents are just repeated multiplication, that's what the assignment is trying to get you to implement
VitalJeevanjot
@CallMeSpriggy8033 let's fix your loop first if that's fine with you
Jimmacle
Jimmacle2y ago
if you keep trying to derail this conversation i'm going to ask a mod to step in
VitalJeevanjot
why you so rude, learning from 2 won't make it lesser, but better also being rude is not going to do good for him
Jimmacle
Jimmacle2y ago
he can't fix the loop without understanding what he's actually trying to accomplish
VitalJeevanjot
don't call me, if you don't want to be my audience. @CallMeSpriggy8033 Remember you wrote i=root at first step ? in the loop
CallMeSpriggy
CallMeSpriggyOP2y ago
so the for line is okay?
VitalJeevanjot
yup
Jimmacle
Jimmacle2y ago
...what?
VitalJeevanjot
but not the parts in it
Jimmacle
Jimmacle2y ago
no, it's totally wrong
VitalJeevanjot
the syntax was ok
Jimmacle
Jimmacle2y ago
which is what i was building to until you decided to be rude and try to take over the conversation
VitalJeevanjot
now read carefully for(initialization, up-until, do-operation) {} these are the 3 parts of the loop initialization means, start from (From where the loop starts) you are starting it from the root value but that is not right is it ? you want to go up-until exponent, but that is also not written good so let me break down what's wrong first initialization, From where the loop starts. i here should start from 0 (not 1, just remember that for now because of the language we use it's where the practices applies to) also it is based on other factors, but just remember that for now until you grown mature in it up-until the second part. Here you want your loop to go to certain number. Like the exponent. For example if you want to have exponent 5 then this will be initialziation = 0 up-until = 4 so it will run as 0,1,2,3,4 (5 times starting from 0) now 3rd part operation is correct how you handled. Which is increment by one or (++) any questions until now @CallMeSpriggy8033 ? let me know or we can move forward So for example
for (int i = 0; i < 5; i++) {
// this will run 5 times 0 to 4
}
for (int i = 0; i < 5; i++) {
// this will run 5 times 0 to 4
}
or
for (int i = 0; i <= 4; i++) {
// this will run 5 times 0 to 4
}
for (int i = 0; i <= 4; i++) {
// this will run 5 times 0 to 4
}
consider the 5 or 4 in above loop as your exponent makes sense ?
Scratch
Scratch2y ago
@vitaljeevanjot I appreciate your attempt to help, but Jimmacle has this under control. When you jump in like you did while someone's trying to teach something, it gets to be really confusing for new devs.
VitalJeevanjot
Thank you, for the clarity however it thought it as public discussion so i was just filling the blanks but the reply from Jimmacle was a bit hurtful
Scratch
Scratch2y ago
From Jimmacle's perspective, he was trying to explain/teach a lesson and then you jumped into the conversation and took it over leading to much more confusion for the OP
VitalJeevanjot
I see, Yes Jimmacle did cleared this to me in the DM he just did ok i discotinue from here thanks
CallMeSpriggy
CallMeSpriggyOP2y ago
I just want to figure out the equation for this loop head in hands or whatever it is I need to write to ge this right
Jimmacle
Jimmacle2y ago
okay, so back to where we left off exponents are just repeated multiplication like you said at the beginning consider the case root ^ 1, it's just root since multiplying 0 by anything is 0, it's actually better to assume the exponent is at least 1 and put this case outside the loop so we have something to multiply with that isn't 0 like int result = root; then in the loop we only have to think about the repeated multiplications does that make sense?
CallMeSpriggy
CallMeSpriggyOP2y ago
? so is the loop itself right or wrong
Jimmacle
Jimmacle2y ago
as it is now it's wrong, i'm trying to help you work out what the loop needs to do so we've put the value of root in result, now if we actually wanted the 2nd power we could add another line like result = result * root right? and if you wanted the 3rd power you could just copy and paste that line again, and so on until you get to the power you want
CallMeSpriggy
CallMeSpriggyOP2y ago
but I don't know the power
Jimmacle
Jimmacle2y ago
right, that's where the loop will come in instead of copying and pasting the line you can use the loop to make it run a specific number of times
CallMeSpriggy
CallMeSpriggyOP2y ago
so not like this?
Jimmacle
Jimmacle2y ago
it's close, but you don't want to reset result to root every time you loop you want to keep building up the answer from an initial value in this case you'd want to change int result = 0 to int result = root and remove var result = root; from inside the loop
CallMeSpriggy
CallMeSpriggyOP2y ago
but now it just returns the root number
Jimmacle
Jimmacle2y ago
which is expected, because it's not looping the correct number of times - we still have to fix this line
Jimmacle
Jimmacle2y ago
like vital was explaining, we basically want the for loop to count up to however much the exponent is the first section initializes i to a starting value, the second part checks if the loop should stop, and the third part does something to i each loop so if you wanted to count from 0 up to exponent what would that look like? (this isn't exactly correct but it will be closer)
CallMeSpriggy
CallMeSpriggyOP2y ago
+1?
Jimmacle
Jimmacle2y ago
right, so that's what the third part is doing - every time the loop runs it will increment i by 1 but do we want to start from root or something else?
CallMeSpriggy
CallMeSpriggyOP2y ago
...something else?
Jimmacle
Jimmacle2y ago
right, so what do you think the int i = root part should be changed to
CallMeSpriggy
CallMeSpriggyOP2y ago
I... don't know 1?
Jimmacle
Jimmacle2y ago
that's right (nearly), but do you understand why?
CallMeSpriggy
CallMeSpriggyOP2y ago
no
Jimmacle
Jimmacle2y ago
so when the code starts running the for loop, it will set i to 1 and then check your condition i <= exponent to see if it should run at all if that condition is true, it will run the code inside the loop, then run i++ to add 1 to i then it checks the condition again and so on until i <= exponent isn't true anymore then the loop ends does that make sense?
CallMeSpriggy
CallMeSpriggyOP2y ago
yea
Jimmacle
Jimmacle2y ago
so the loop is basically counting up by 1 until i is more than exponent
CallMeSpriggy
CallMeSpriggyOP2y ago
but now it gives me this
Jimmacle
Jimmacle2y ago
yeah, we have to account for the fact we set result = root at the beginning for example, to calculate root ^ 2 we actually only multiply once so the loop needs to run 1 less than the exponent
CallMeSpriggy
CallMeSpriggyOP2y ago
< instead of <=
Jimmacle
Jimmacle2y ago
that's one way to do it, give it a try
CallMeSpriggy
CallMeSpriggyOP2y ago
yes! It wokrs!
Jimmacle
Jimmacle2y ago
now this will depend on what your teacher expects, but if you try to calculate root ^ 0 do you get the right answer? i don't know if you're expected to handle that so it might not matter
CallMeSpriggy
CallMeSpriggyOP2y ago
I don't think it does
Jimmacle
Jimmacle2y ago
might be a fun exercise to try and solve anyway 😛 but if not then it looks like you're all set
CallMeSpriggy
CallMeSpriggyOP2y ago
thanks! would,,,,, you like to help me with a couple more? there's one dealing with characters and strings that I'm not sure about
Jimmacle
Jimmacle2y ago
unfortunately i'm already behind on going to bed, but there are others who should be able to jump in and help if you make a new post
CallMeSpriggy
CallMeSpriggyOP2y ago
aight! Night :)
Jimmacle
Jimmacle2y ago
o7
Want results from more Discord servers?
Add your server