program not doing what it should
Hei! So i found my problem with endless printing in my program, but the result is still not right. It should print out 1729 (12 + 1 = 9 + 10), but it doesn't.
Code:
#include <iostream>
using namespace std;
int maks(int num1, int num2);
int main()
{
int atgr,m,n,sk=13;
for (int k=1; k<=sk; k++){
for (int l=1; l<=sk; l++){
for (int i=1; i<=sk; i++){
for (int j=1; j<=sk; j++){
m= k*k*k + l*l*l;
n= j*j*j + i*i*i;
//if(k!=l && k!=i && k!=j && l!=i && l!=j && i!=j){
// cout<<atgr;}
}
}
}
}
atgr=maks(m,n);
cout<<atgr<<endl;
}
int maks(int num1, int num2){
int m;
if (num1==num2) m=num1;
return m;
}
Re: program not doing what it should
Code:
int maks(int num1, int num2){
int m;
if (num1==num2) m=num1;
return m;
}
What value do you think m has if num1 is not equal num2?
PS Don't start a new thread on the same topic. Just add a reply to the existing thread.
Re: program not doing what it should
if m==n (j*j*j + i*i*i==k*k*k + l*l*l), because 1729=12^3 + 1^3 = 9^3 + 10^3. i think that my function is working right, but i cant figure out what is wrong with for loops. Program is printing 4394 out but it shouln't
Re: program not doing what it should
Quote:
Originally Posted by
jennahello
if m==n (j*j*j + i*i*i==k*k*k + l*l*l), because 1729=12^3 + 1^3 = 9^3 + 10^3. i think that my function is working right, but i cant figure out what is wrong with for loops. Program is printing 4394 out but it shouln't
No, your maks() function is not working right. It returns the value of m. If num1 equals num2 then m is set to num1. What value should m be if num1 is not equal to num2?
The variable m in main is not the same variable as m in maks(). These are different and changing m in maks() has no effect on the value of m in main().
Re: program not doing what it should
You can simply this program and remove the need for the maks() function.
Code:
#include <iostream>
using namespace std;
int main()
{
const int sk = 13;
for (int k = 1; k <= sk; k++)
for (int l = 1; l <= sk; l++)
for (int i = 1; i <= sk; i++)
for (int j = 1; j <= sk; j++) {
int m = k * k * k + l * l * l;
int n = j * j * j + i * i * i;
if (k != l && k != i && k != j && l != i && l != j && i != j && m == n) {
cout << m;
return 0;
}
}
return 1;
}
Re: program not doing what it should