-
December 14th, 2013, 03:48 PM
#1
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;
}
-
December 14th, 2013, 04:18 PM
#2
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.
Last edited by 2kaud; December 14th, 2013 at 04:21 PM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 14th, 2013, 04:21 PM
#3
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
-
December 14th, 2013, 04:23 PM
#4
Re: program not doing what it should
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().
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 14th, 2013, 06:15 PM
#5
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;
}
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 15th, 2013, 04:38 AM
#6
Re: program not doing what it should
thanks!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|