x+y+z=10

a problem in the c++algorithm

I typed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

using namespace std;
bool ok(int a,int b, int c,int n)
{
	if (a+b+c==n)
		return true;
	else
		return false;
}
void printnum(int x,int y,int z,int k)
{
	for (x=1;x<=8;x++)
		for(y=1;y<=8;y++)
			for(z=1;z<=8;z++)
			{
				{
					{
						if(ok(x,y,z,k))
							cout<<"("<<x<<","<<y<<","<<z<<")"<<endl;
					}
				}
			}
}

int main(int argc, char*argv[])
{    int X,Y,Z;
int N=10;
	printnum(X,Y,Z,N);
	system("pause");
	return 0;
}
to calculate the sovle of the integer function x+y+z=10
such as 1+1+8=10
2+3+5=10


and I got the error:
1
2
3
4
5
6
7
8
1
1>  main.cpp
1>c:\users\danny\documents\visual studio 2010\projects\testfor gl\testfor gl\main.cpp(29): warning C4700: uninitialized local variable 'Z' used
1>c:\users\danny\documents\visual studio 2010\projects\testfor gl\testfor gl\main.cpp(29): warning C4700: uninitialized local variable 'Y' used
1>c:\users\danny\documents\visual studio 2010\projects\testfor gl\testfor gl\main.cpp(29): warning C4700: uninitialized local variable 'X' used
1>main.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/INCREMENTAL:NO' specification
1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
1>C:\Users\danny\Documents\Visual Studio 2010\Projects\testfor gl\Debug\testfor gl.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
why the variable X,Y,Z is wrong?
At line 28 X, Y and Z are defined, but not initialised with any specific value. Then these variables are used in the function call at line 29.

Note, those are just warnings, not errors. The program should still execute.

But - the other error is more important: unresolved external symbol _WinMain

That looks to me as though the wrong project type has been set up, this should be a console application but it is trying to create a Windows application instead.
yup, so I need to write #define N 10 ?
but it still didn't work.
I wrote it in VS2010 and chose the console application.
The book doesn't gave me the answer so I just tried the void printnum(int x,int y,int z,int k)
maybe it was a wrong way to build the function?
If you read the compiler warning carefully, it tells you exactly what is wrong! Don't freak out, read it slowly.

uninitialized local variable 'Z' used
uninitialized local variable 'Y' used
uninitialized local variable 'X' used

What this means is you have not initialized these variables with a value before you decided to use them.

Note N = 10, but what are the values X,Y,Z suppose to have??

Question
Why do you pass in values for x, y ,z to function printnum and then in the loop each is set be 1? What is the point??

It seems the only value you care for is the sum N=10

Also what is with the crazy braces inside the 3rd nested loop??? Seriously understand what braces do =)

Try the code below, when you can give the variable some meaningful names, make code self documenting when you can!

Also what kind of function name is ok? why not give it a better name, like Sum3 or ValidateSum

I changed the loop to go from 0 to sum, since 0+0+sum = sum, got it? If you don't care about zero, start the loop from 1, but then this algo is partially correct.

use pre-increment when you can, like ++x, it's faster since a copy of the return value does not need to take place!

int a = ++x; <== is not the same as ==> int a = x++;

The 2nd requires a copy of x to be made that is then assigned to a, and then x is incremented!

In a function always use const when you don't want the passed in value to be modified.

FYI: Notice how function ok has been simplified, this is a good trick to learn. You actually don't need this function as the check can be done inside the loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool ok( const int a, const int b, const int c, const int sum )
{
   return ( (a + b + c) == sum );
}

void printnum( const int sum )
{
   for( int x = 0; x <= sum; ++x )
      for( int y = 0; y <= sum; ++y )
         for( int z = 0; z <= sum; ++z )
            if( ok( x, y, z, sum ) ) cout << "(" << x << "," << y << "," << z << ")" << endl;
}

int main( int argc, char*argv[] )
{
   printnum( 10 );
   system("pause");
   return 0;
}


---
Rajinder Yadav <[email protected]>

DevMentor Labs
http://labs.devmentor.org
Creating Amazing Possibilities
Last edited on
I thought the main problem was I misunderstood the loop
for(i=0:i<4;i++)
{
for(i=0;i<4;i++)
{
cout<<i<<endl;
}
}

and
for(i=0:i<4;i++)
for(i=0;i<4;i++)
cout<<i<<endl;
/////////////////////
I didtn't know the difference between them.My classmate told me they are the same,maybe they are wrong.
In your first post, you have 3 braces nested inside the last loop! This is not the same as your previous exampe.

1
2
3
4
5
6
7
8
for(z=1;z<=8;z++)
{
   {
      {
         if(ok(x,y,z,k))  cout<<"("<<x<<","<<y<<","<<z<<")"<<endl;
      }
   }
}


When posting code use the code block, its hard for others to read code that's not formatted properly.
Last edited on
I notice that.but I still can't find the difference of for {} and for...
Topic archived. No new replies allowed.