"printf" did not work

i have a problem that "printf" didint show what i want

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  void obj_function()
{
	for (i = 0; i<no_of_facloc; i++)
	{
		for ( k = 0; k < no_of_facloc; k++ )
			{
				for ( j = 0; j < no_of_facloc; j++ )
					{
						for (q = 0; q < no_of_facloc; q++)
							{
								objFunction += flow[i][k] * dist[j][q] * x[i][j] * x[k][q];
								
							}
					 } 
			}
	}
	printf ("objFunction = %d \n", objFunction);
	system("pause");
}


its show
objFunction=0

the right one is
objFunction = 30232

what did i do wrong
Last edited on
what is objFunction?
To be more explicit than jonnin, it appears that printf() is working just fine. It is significantly more likely that your calculation is incorrect.

"objFunction" is a very odd name to give an integer type variable, kind of like having a pet dog named "bird". Even a simple name like "objFunctionResult" might be better?

Or, better yet, why not make it a function returning a value:

1
2
3
4
5
6
int obj_function()
{
        int result = 0;
        ...
        return result;
}

I recognize that you are new at this, but even so, try to get rid of all those system("pause"); in your program. If necessary, have just one at the end of main().

I can't imagine any kind of solution requiring a n4 loop like you have set up there. What is the problem you are trying to solve here?
Are you using Visual Studio?

I don't know what your loops are doing, but I'd debug it by separating out everything into 4 variables, then putting a breakpoint on the += line:

1
2
3
4
5
int var1 = flow[i][k];
int var2 = dist[j][q];
int var3 = x[i][j];
int var4 = x[k][q];
objFunction += var1 * var2 * var3 * var4; // Put a breakpoint on this line 


Breakpoint on that line and ensure your other values are being set properly by hovering var1, var2 etc
Last edited on
are you assuming its int because %d? %d will work on a double or anything else too, and print nonsense all day long. A lot of new programmers assume d is for double, without looking the codes up.
Last edited on
@Duthomhas im doing problem on quadratic assignment problem .. actually i got the the result but i cannot display it using printf..
@pSystem i will try using ur suggestion .. tq
so I will ask again. What is the type of the variable you are trying to print?
@jonnin so i need to change %d?
I don't know because you won't answer my question.

%d is for INTEGERS (any).
try %1.10f for doubles (or floats or long doubles etc). 1.10 means 10 decimal places. 20 is more than most systems actually have so between 1.0 and 1.20 as you like it. There are also codes for scientific notation and more.

if its not a double/float/int type, tell me more.

printf uses a dumb pointer approach. whatever you stick in the variables, it takes a pointer to that address and then attempts to use that as if it were what you said it was. This can be useful to exploit, or it can cause bugs and crashes, depending on what you did to it. Mostly, you should stick to following the intentions of the tool until you understand it very well. And mostly, you should be using c++ tools, though I will use printf for bunches of doubles due to the formatting hassle of cout.
Last edited on
@shuthairah
http://sscce.org/
Read this and post something we can compile and test.

> objFunction += flow[i][k] * dist[j][q] * x[i][j] * x[k][q];
At the very least, we need the TYPES of all these variables.

Otherwise, you're just wasting everyone's time.
thank you everyone.. i know what wrong about the coding.. thank you so much for the reply and the information
Registered users can post here. Sign in or register to post.