also m is not initialized
so first (and as result each next) time you multiply garbage on something, getting garbage as a result
also m is not initialized
so first (and as result each next) time you multiply garbage on something, getting garbage as a result
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Thanks razorlight, you are right about that. Now the code doesn't give any errors, but the result still isn't correct. Also, we need a code that works for negative numbers as well. Any help would be appreciated.
Edit: Sorry vart and sloppy, I didn't see your replies. Checking now!
Edit 2: How should m be initialized?
Code:#include <stdio.h> int power,base; int result=1; int count=0; int main() { printf("base....:D"); scanf("%d",&base); printf("\npower......:D"); scanf("%d",&power); for(count=0;count<power;count++) { base*count=result; } printf("%d to the power of %d = %d ",base,power,result); return 0; }
Last edited by Nathalie; 11-11-2007 at 06:23 AM.
count should = 1 to start the for loop, not 0. There is no zero column of digits in a number base.
If you want to work with negative int's as well, just make your datatypes unsigned int's.
the base may be < or > 0, no problem.
if power is < 0 you should change sign, something like
exp = - power
and after the loop (where you use exp instead of power)
result = 1/ result
Now the program works for anything but negative power numbers. To change them, as you said, you probably need an if-structure or so? Would it be possible to write it down? I'm not really sure where to start.
Code:#include <stdio.h> unsigned int power,base; int result=1; int count=1; int main() { printf("base....:D"); scanf("%d",&base); printf("\n power......:D"); scanf("%d",&power); for(count=0;count<power;count++) { result *= base; } printf("%d to the power of %d = %d ",base,power,result); return 0; }
read comments:
The variables above have to go inside main as told before.Code:unsigned int power,base; // delete "unsigned" here. int result=1; int count=1; // it is completely useless to put =1 here // because count is initialized in for loop
it does change. An unsigned int cannot be negative. Maybe to simplify your problem you can leave power unsigned and base signed.
the problem is (assuming "power" is an integer) that result=base^(-power) is not an integer. Example:
10^(-2)= 1/(10^2)=0.01 that is not an integer.
To store non-integer values in a variable you must declare it as double (or float).
But doing that requires a little change to printf (because %d is for integers, not for double)
As you see there is a bit to write sooo:
It 'd be really nice if you tried to write something before... I am as lazy as you :-DIf you could go into that briefly, it'd be nice.
As many have noted before, it's a good practice to properly indent your code. It looks all sloppy and messy. After each brace, indent with a few spaces or a tab. Code will look so much cleaner and easier to read.
Well i should have read the previous posts of this thread, but been lazy today. From last few post i can understand u are trying to find the power of a number. Why not use pow function from math header?
ssharish
From first post:
As to why... no clue. Negative powers aren't really the easiest to deal with...- A program that calculates powers (ex. 3^4 when 3 and 4 are given by the user) without using pow(a,b).
I was thinking of writing something like A*((B-1)* "*A"), which would probably work in VB but how do you write this correctly in C?
Okay, we've been trying to make it work with negative powers for awhile now but still no success. However maybe it is something in the right direction and you can give a hint after looking at the code.
For the if <0, count<power is not correct I think, because the count will never be a negative number at all. But when I make it -power, it gives a floating point exception and when we tried some other things, it even crashed. Any ideas?
Code:#include <stdio.h> int main() { int power,base; float result=1; int count=1; printf("base....:D"); scanf("%d",&base); printf("\npower......:D"); scanf("%d",&power); if (power>0) { for(count=0;count<power;count++) { result *= base; } } if (power<0) { for(count=0;count<power;count++) { result *= -base; } } printf("%d to the power of %d = %.2f ",base,power,result); return 0; }