I need help writing a program that will calculate the sum of any power of the numbers 1 through 10. So for example: 1^2 + 2^2 + 3^2.... + 10^2. Except that the power does not have to be 2, it can be any number that I enter.
Your code formatting is not intuitive.
Function "Power" computes a sum.
Statement on line 12 does not end with a semi-colon.
What is the purpose of loop on line 11?
Function Power returns the first time the execution reaches line 13, i.e. on the first iteration of the loops.
Line 12 has an equality comparison rather than accumulation.
i is the base, and p is the exponent. The first loop gives the numbers 1-10 for i. The second (and im not too sure I did it right) is supposed to give any number as p (the exponent).
When is 1 <= 10 false? When is p <=i || p > i false? I get that p is the power, but isn't your power exp? If you're given the exponent, you don't need to provide your own.
sum = pow(i, p) assigns i^p to sum, it doesn't add it.
short is a short, or 2-byte, integer. It can be useful for decreasing space needed by the program. Although, a char is 1 byte, so @codekiddy could have chosen a char to reduce space even more.
What your code does is set sum to every exponent of 1-10 (which is infinite), so it's not giving you no output, it's running infinitely.
Sorry, that 1 is supposed to be an i. I'll fix that...
I get that p is the power, but isn't your power exp?
If I replace "exp" with "p" I get an error message saying, "declaration of int p shadows a parameter".
sum = pow(i, p) assigns i^p to sum, it doesn't add it.
Will fix that.
What your code does is set sum to every exponent of 1-10 (which is infinite), so it's not giving you no output, it's running infinitely.
So how do I fix this?
My intention is the following: Make the first loop give the numbers 1-10. The second loop should give me the powers (exponents). This can be ANY number, hence the condition p <=i || p > i.
That I use pow(i, p) and add it to sum which is intiialized to 0.
What am I doing wrong?
Ok, had some syntax errors, here is the newly revised code:
@Gi Pa
Thank you for the solution but for future reference, we try not to give answers here. It's easier for people to learn if you help them find the problem and figure out their own solution. If you just give them the solution, they won't know what was wrong in the first place, so they can't fix it in the future.
@Arslan7041
The question asks for you to give the sum of all numbers 1-10 to the exponent n. n can be any number, so you know it's input by the user or some other function (that's why your function call has exp).
Your most recent code still has 1 <= 10.
The line for(p = 0; p <=i || p > i; p++) causes an infinite loop (p <= i || p > i is never false; and is unnecessary. You receive your exponent in the function (exp), so you just use that (remove p altogether).
You were setting sum, not adding to it, so sum would always equal the most recent power, not the sum of all powers. Fix it by sum += pow(i, exp); or sum = sum + pow(i, exp). As has been pointed out, pow returns a double, so you should have a double for sum (you can find workarounds, but there's no good reason for you to do that (the function returns a double, and exp is a double).
@GRex2595
Thank you so much. It worked! As you stated, the p variable was unnecessary. This whole time I thought I needed a nested loops, when all I needed was a single loop.
You should have double for sum because if you accept a double for the exponent, then your pow will return a double. If you only accept integers, then it doesn't matter.