I know C++, JavaScript, PHP, and a few other languages (Inform 7, TI-BASIC, Java). MSN me ([email protected]) if you want.
Also, C++ is a lot easier to use than C (according to my dad, who has used both).
Anyway, I recently went through to a problem involving loops in C
The code below does one thing, give Base (Base), give Exponent (Power) then the program will multiply the base by itself (Resulting to the Total) by looping itself, until x = Power. The idea is, x = 0 and every time the program multiplies the base by itself, +1 is added to x. And once x is equal to Power, then the program knows that it can finally stop, thus the break;.
But I noticed one thing. If the base was 2 and the exponent was 2, it won't go out as 4, instead it'll stay as 2. Probably it never actually did this function: [Total = Total * Base;] and the last thing to modify Total is the function: [Total = Total + Base;]
So what am I doing wrong? I'm not so familiar with loops, and that was a copy paste, instead of writing the loop code from scratch. So I don't have the complete picture of what loops are in C yet.
Your loop is sort of correct - it loops the right amount of times, but what you forgot is that the last time it loops, your if-statement becomes true and the system will output your data and it will NOT multiply the base to the total again, although it should be.
One solution would be to just put the multiplication into the then-section of the if-statement.
But I'd like to suggest another way of writing the function:
{floatBase,Total;intPower,x;printf("Input the base: ");scanf("%f",&Base);Total=Total+Base;//we don't actually need the "total +" part, since total is 0 at this point// and I'd advice you to remove it completely. Some programming lang-//uages might give you an error, because total isn't initialized yet.printf("\nInput the exponent: ");scanf("%d",&Power);for(x=0;x<Power;x++){Total=Total*Base;}printf("%f",Base);printf("^%d",Power);printf(" is equal to %f",Total);}
Readability is the most important aspect, especially when you programm larger systems or if you want others to be able to easily read your code. If you add unneeded if-statements, breaks and weird constructs you're making it harder for yourself to find errors.
The loop in the code above is clear (and also a little bit faster) and much easier to debug.
(I've not actually tested that code, so I can't be 100% sure there's no error in it. But I'd be ashamed of myself if there was.)
PS: I just noticed the last sentence of yours. To elaborate on loops a little more..
There are three kinds of loops. The for-loop, the do-while-loop and the while-do-loop.
What you are using (and this is best in this situation) is the for-loop.
The head (first line) of the loop specifies how often it is supposed to run all actions within it. It basically says:
"I have my variable X, which I set to 0. I want to loop as long as X is smaller than Power. And evertime I finish one loop, I'll add +1 to X."
Because if you input 2, it will run 3 times, thus having an exponent of 3.
But setting it as 1, it will run 2 times, thus having the expected exponent of 2.
Thanks, I don't know much about how to create loops, but I have a good understanding of the concept, that's why I just copy paste looped examples and edit it from there.
Can you provide a link or teach me how to create loops?
Edit:
Oh and you make sense into remove the Total +, let me remove that, thanks.
One last thing, if you aren't bothered to helping me even further, I have one more c script consisting of loops which I have a feeling, can be more compact and simple than what it is right now.
Because if you input 2, it will run 3 times, thus having an exponent of 3.
But setting it as 1, it will run 2 times, thus having the expected exponent of 2.
We set x=0; Then we start the loop:
Is x(0) < Power(2) ? Yes. Run the loop, set x=x+1; <- - First iteration
Is x(1) < Power(2) ? Yes. Run the loop, set x=x+1; <- - Second iteration
Is x(2) < Power(2) ? No. Stop the loop.
Oh, ah^.^ The reason why the number was wrong isn't actually that it looped once too often, but that Total was set to Base in the beginning. That basically does the same as looping once. This way, It'd always return x^(y+1).
Good example: Enter 0 as Power and 5 as Base. It should return 1, but it returns 5. Even though the loop wouldn't run at all.
first variables in C and C don't start on their identity (0), they start with the data originally in the static memory or stack (depends if they are locals or globals), so it can be anything. However, some C compilers will reserve the memory as 0.
second, the proposed algorithm will get you the power plus one, but it won't get you Base^Power.
Here, I have fixed up your algorithm and I have added some propositional logic to help you out note some things.
intmain(){/** Calculates the natural power of any base. * * @pre Power >= 0; * @post (%product i | 0 <= i < Power : Base) == Total */floatBase,Total=1.0;// if a variable is not initialized in C, it may notintPower,x;// contain 0 but instead the value which was originally// in memory (means other value used before by other program).// try this:// printf("%d", Base); // it may print a random value if your compiler does not set reserved memory as 0.printf("Input the base: ");scanf("%f",&Base);printf("\nInput the exponent: ");scanf("%d",&Power);/// @assert 0 <= Power/// @invariant 0 <= x < Power &&/// Total == (%product i | 0 <= i < x : Base)/// @variant Power - xfor(x=0;x!=Power;x++)Total=Total*Base;// it's more eficient to use only one printf call.printf("%.2f^%d is equal to %.2f\n",Base,Power,Total);system("PAUSE");return0;}
And here's the code for any Power:
intmain(){/** Calculates the natural power of any base. * * @pre ((Base == 0) ==> Power > 0) * @post ((Power >= 0)==>(%product i | 0 <= i < Power : Base) == Total) && * ((Power < 0)==>(%product i | 0 <= i < -Power : 1/Base) == Total) */floatBase,Total=1.0;// if a variable is not initialized in C, it may notintPower,x;// contain 0 but instead the value which was originally// in memory (means other value used before by other program).// try this:// printf("%d", Base); // it may print a random value if your compiler does not set reserved memory as 0.printf("Input the base: ");scanf("%f",&Base);printf("\nInput the exponent: ");scanf("%d",&Power);if(Power<0){Base=1.0/Base;Power=-Power;}/// @invariant 0 <= x < Power &&/// Total == (%product i | 0 <= i < x : Base)/// @variant Power - xfor(x=0;x!=Power;x++)Total=Total*Base;///printf("%.2f^%d is equal to %.6f\n",Base,Power,Total);system("PAUSE");return0;}
@s3rius Thanks for the link. I think I'll be able to learn loops tomorrow. I'll fix the other script myself once I've learned loops.
@BlinkHawk Although you lost me at a lot of parts, I am thankful I've learned a few things using your proposed script.
For one, you can call multiple variables in printf as long as it's in an ordered pattern. (That's the best way I can describe it with a sleepy mind right now)
Second, you taught me how to shorten the decimals. It's pretty annoying seeing 6 zeros. I hope I can apply this to other functions like scanf.
Third, the variables getting their values from a past script. I guess I'll be putting in = 0 to all the variables next time.
Fourth, I don't think I have taken negative powers yet :P but thanks, this will impress my teacher.
Thank you, I hope I can ask you guys more questions on this thread later on.
Rollback Post to RevisionRollBack
Member since 2010. Made the -The Thing- [Revival] game. Nostalgic of the WC3 days.
Anyone?
Member since 2010. Made the -The Thing- [Revival] game. Nostalgic of the WC3 days.
Yes, you can find their post here, or eventually someone will reply here :) .
can someone answer my C questions?
Member since 2010. Made the -The Thing- [Revival] game. Nostalgic of the WC3 days.
Well, you could go ahead and ask them, no?
I don't know so much about C (more confident in C+ +), but asking usually doesn't cost anything ;p
Thank God I kept this thread.
Anyway, I recently went through to a problem involving loops in C
The code below does one thing, give Base (Base), give Exponent (Power) then the program will multiply the base by itself (Resulting to the Total) by looping itself, until x = Power. The idea is, x = 0 and every time the program multiplies the base by itself, +1 is added to x. And once x is equal to Power, then the program knows that it can finally stop, thus the break;.
But I noticed one thing. If the base was 2 and the exponent was 2, it won't go out as 4, instead it'll stay as 2. Probably it never actually did this function: [Total = Total * Base;] and the last thing to modify Total is the function: [Total = Total + Base;]
So what am I doing wrong? I'm not so familiar with loops, and that was a copy paste, instead of writing the loop code from scratch. So I don't have the complete picture of what loops are in C yet.
Member since 2010. Made the -The Thing- [Revival] game. Nostalgic of the WC3 days.
Your loop is sort of correct - it loops the right amount of times, but what you forgot is that the last time it loops, your if-statement becomes true and the system will output your data and it will NOT multiply the base to the total again, although it should be.
One solution would be to just put the multiplication into the then-section of the if-statement.
But I'd like to suggest another way of writing the function:
Readability is the most important aspect, especially when you programm larger systems or if you want others to be able to easily read your code. If you add unneeded if-statements, breaks and weird constructs you're making it harder for yourself to find errors.
The loop in the code above is clear (and also a little bit faster) and much easier to debug.
(I've not actually tested that code, so I can't be 100% sure there's no error in it. But I'd be ashamed of myself if there was.)
PS: I just noticed the last sentence of yours. To elaborate on loops a little more..
There are three kinds of loops. The for-loop, the do-while-loop and the while-do-loop.
What you are using (and this is best in this situation) is the for-loop.
The head (first line) of the loop specifies how often it is supposed to run all actions within it. It basically says:
"I have my variable X, which I set to 0. I want to loop as long as X is smaller than Power. And evertime I finish one loop, I'll add +1 to X."
x = 1 not x = 0.
Because if you input 2, it will run 3 times, thus having an exponent of 3.
But setting it as 1, it will run 2 times, thus having the expected exponent of 2.
Thanks, I don't know much about how to create loops, but I have a good understanding of the concept, that's why I just copy paste looped examples and edit it from there.
Can you provide a link or teach me how to create loops?
Edit:
Oh and you make sense into remove the Total +, let me remove that, thanks.
One last thing, if you aren't bothered to helping me even further, I have one more c script consisting of loops which I have a feeling, can be more compact and simple than what it is right now.
Can you?
Member since 2010. Made the -The Thing- [Revival] game. Nostalgic of the WC3 days.
We set x=0; Then we start the loop:
Is x(0) < Power(2) ? Yes. Run the loop, set x=x+1; <- - First iteration
Is x(1) < Power(2) ? Yes. Run the loop, set x=x+1; <- - Second iteration
Is x(2) < Power(2) ? No. Stop the loop.
Oh, ah^.^ The reason why the number was wrong isn't actually that it looped once too often, but that Total was set to Base in the beginning. That basically does the same as looping once. This way, It'd always return x^(y+1).
Good example: Enter 0 as Power and 5 as Base. It should return 1, but it returns 5. Even though the loop wouldn't run at all.
Anyway, there's a tutorial on loops http://www.cprogramming.com/tutorial/c/lesson3.html and also on a lot of other C / C+ + related stuff.
About your script - well, post it and if it doesn't mean too much work (being lazy, eatin' ice) I'll check it out.
Let me correct you all.
first variables in C and C don't start on their identity (0), they start with the data originally in the static memory or stack (depends if they are locals or globals), so it can be anything. However, some C compilers will reserve the memory as 0.
second, the proposed algorithm will get you the power plus one, but it won't get you Base^Power.
Here, I have fixed up your algorithm and I have added some propositional logic to help you out note some things.
And here's the code for any Power:
@s3rius Thanks for the link. I think I'll be able to learn loops tomorrow. I'll fix the other script myself once I've learned loops.
@BlinkHawk Although you lost me at a lot of parts, I am thankful I've learned a few things using your proposed script.
For one, you can call multiple variables in printf as long as it's in an ordered pattern. (That's the best way I can describe it with a sleepy mind right now)
Second, you taught me how to shorten the decimals. It's pretty annoying seeing 6 zeros. I hope I can apply this to other functions like scanf.
Third, the variables getting their values from a past script. I guess I'll be putting in = 0 to all the variables next time.
Fourth, I don't think I have taken negative powers yet :P but thanks, this will impress my teacher.
Thank you, I hope I can ask you guys more questions on this thread later on.
Member since 2010. Made the -The Thing- [Revival] game. Nostalgic of the WC3 days.
The link I gave you also contains a tutorial covering this ;)