program not doing what it should
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: program not doing what it should

  1. #1
    Join Date
    Dec 2013
    Posts
    4

    Unhappy 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;
    }

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,553

    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 03:21 PM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Dec 2013
    Posts
    4

    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

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,553

    Re: program not doing what it should

    Quote Originally Posted by jennahello View Post
    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,553

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Dec 2013
    Posts
    4

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center