This is a split board - You can return to the Split List for other boards.

Can someone help me with a program in C?

#1Glockass1Posted 3/27/2014 9:46:46 AM
Using Dev-C++

Here's my code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int Number(int, int, int);

int main(int argc, char *argv[]) {
int x, y, z, N, M, result, sum ;
sum=0;
for (x = -99; x <= 99; x++){
for (y = x; y <= 99; y++){
for (z = y; z <= 99; z++){
result = Number(x, y, z);
printf("%d %d %d, %d\n",x,y,z,result) ;
if (result == 1){
printf("%d %d %d\n", x, y, z);
sum++;
}
}
}
}
printf("%d total combinations\n", sum);

return 0;
}

int Number(int x, int y , int z){
int a,b,c,d,e;
double d1;
d = a*a + b*b+ c*c;
d1= pow(d,1.0/3);
e=(int)d1;
if ((d1-e)== 0) {
printf("%7.5g \t", d1-e);
return 1;
}

else {
printf("%7.5g\n", d1-e );
return 0;
}


}

For some reason it doesn't work. Here is an example of the result:

http://i.imgur.com/IZURYvW.jpg

Why does it return 0 all the time?

Thanks for the help.

Check this image for better viewing:

http://i.imgur.com/UrQrriY.jpg
---
Steam ID: Glockass1
http://i.imgur.com/qsRb7.png http://i.imgur.com/PgA0Rlc.gif
#2neroAngeloPosted 3/27/2014 10:12:23 AM
Ask one of the thousands of C programming boards on the internet. Not a PC gaming board.
---
PSWii360U - This is how I triforce http://i.imgur.com/9qF9vGa.jpg
i7 920 @3.3GHz, 6GB DDR3, 7850 2GB
#3Ch3wyPosted 3/27/2014 10:17:21 AM
It's always returning 0 because d1-e does not equal 0.
---
How the hell can a octopus live outside of water anyways? This is so stupid. -Fade2black001
#4Glockass1(Topic Creator)Posted 3/27/2014 10:28:51 AM
Ch3wy posted...
It's always returning 0 because d1-e does not equal 0.


I don't think that's it. Even for x=-2, y=0, z=2 it should be 1 but it's not.
---
Steam ID: Glockass1
http://i.imgur.com/qsRb7.png http://i.imgur.com/PgA0Rlc.gif
#5Ch3wyPosted 3/27/2014 10:41:20 AM
Have you tried using cbrt(d) instead of pow(d,1.0/3)?
---
How the hell can a octopus live outside of water anyways? This is so stupid. -Fade2black001
#6Ch3wyPosted 3/27/2014 10:45:02 AM
Wait a second you never even set the values for a, b, and c.
---
How the hell can a octopus live outside of water anyways? This is so stupid. -Fade2black001
#7popping4itPosted 3/27/2014 11:04:18 AM
Ch3wy posted...
Wait a second you never even set the values for a, b, and c.


ya that looks like the problem should be.

in line 31 d = x*x + y*y + z*z

or prior ...
a = x
b = y
c = z
---
whens mahvel?
#8Glockass1(Topic Creator)Posted 3/27/2014 11:19:24 AM
popping4it posted...
Ch3wy posted...
Wait a second you never even set the values for a, b, and c.


ya that looks like the problem should be.

in line 31 d = x*x + y*y + z*z

or prior ...
a = x
b = y
c = z


lol that was the problem I think. I got confused because I thought they get values automatically. Thanks for the help. Also, is there a way to make the program faster? It takes a while to go through all the combinations.
---
Steam ID: Glockass1
http://i.imgur.com/qsRb7.png http://i.imgur.com/PgA0Rlc.gif
#9popping4itPosted 3/27/2014 11:23:16 AM
if the goal is to output all the combinations it has to run through x^3 no matter what. its likely the pow function is what ends up taking the most time.
---
whens mahvel?
#10Ch3wyPosted 3/27/2014 11:28:06 AM
There might be a chance that cbrt is faster than throwing a fraction into pow, but I'm not sure. Worth a shot.
---
How the hell can a octopus live outside of water anyways? This is so stupid. -Fade2black001