"printf" did not work

Feb 9, 2019 at 2:22pm
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 Feb 9, 2019 at 2:29pm
Feb 9, 2019 at 5:24pm
what is objFunction?
Feb 10, 2019 at 4:23am
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?
Feb 10, 2019 at 5:15am
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 Feb 10, 2019 at 5:16am
Feb 10, 2019 at 4:56pm
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 Feb 10, 2019 at 4:57pm
Feb 11, 2019 at 2:27pm
@Duthomhas im doing problem on quadratic assignment problem .. actually i got the the result but i cannot display it using printf..
Feb 11, 2019 at 2:28pm
@pSystem i will try using ur suggestion .. tq
Feb 11, 2019 at 2:28pm
so I will ask again. What is the type of the variable you are trying to print?
Feb 11, 2019 at 2:30pm
@jonnin so i need to change %d?
Feb 11, 2019 at 2:30pm
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 Feb 11, 2019 at 2:37pm
Feb 11, 2019 at 2:39pm
@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.
Feb 12, 2019 at 10:25am
thank you everyone.. i know what wrong about the coding.. thank you so much for the reply and the information
Topic archived. No new replies allowed.