Pointers, C++...code not working - c++

This is the question:
Write the definition of a function minMax that has five parameters. The first three parameters are integers. The last two are set by the function to the largest and smallest of the values of the first three parameters. The function does not return a value.
The function can be used as follows:
int a = 31, b = 5, c = 19, big, small;
minMax(a, b, c, &big, &small); /* big is now 31; small is now 5 */
This is my code:
void minMax(int x, int y, int z, int* big, int* small)
{
if (x < y && x < z)
*small = x;
else if (y < x && y < z)
*small = y;
else if (z < x && z < y)
*small = z;
if (x > y && x > z)
*big = x;
else if (y > x && y > z)
*big = y;
else if (z > x && z > y)
*big = z;
}
This is the error I'm getting:
Your function did not change the value of small. Make sure you are dereferencing it in your function.
Not sure what's wrong?
Thanks.

I see one immediate problem.
What do you think will happen when you pass the numbers 1, 1 and 7?
Perhaps you may want to consider the use of <= and >= rather than just < and >.
Since that error message looks nothing like any compiler error I've seen before (and the code is valid syntactically), I'd suggest the message is coming from a test harness which probably:
sets the big/small values to numbers other than those being passed in (eg, -9999).
calls the function with test data (eg, 1,1,7).
checks the output varibales to ensure they've been changed to the correct values.
In addition, it's not the most readable code in the world (no offence intended). If you can structure your code in such a way that its intent is clear from a glance (including comments where appropriate), you'll have hordes of future programmers singing your praises and worshiping your name :-)
Something like this shows the intent a little more clearly (IMNSHO) than lots of those else if constructs:
// Populate big/small based on max/min of x, y and z.
void minMax (int x, int y, int z, int *big, int *small) {
// Set by default to x, only change if others are bigger.
*big = x;
if (y > *big) *big = y;
if (z > *big) *big = z;
// Same for small but with reversed comparisons.
*small = x;
if (y < *small) *small = y;
if (z < *small) *small = z;
}

I'm not sure what isn't working. It seems like that would basically work but could be better structured.
Maybe something like this:
void minMax(int x, int y, int z, int* big, int* small)
{
*big = *small = x;
if (y > *big)
*big = y;
if (y < *small)
*small = y;
if (z > *big)
*big = z;
if (z < *small)
*small = z;
}

The error message
Your function did not change the value of small. Make sure you are dereferencing it in your function.
… appears to come from a test harness provided to you by your teacher.
Anyway, it's correct: there are values that you can choose where your function will not assign anything to *small.
For example, with a, b and c the same value, your function will do nothing at all.
Anyway,
for future questions, please provide a complete example program that demonstrates the issue.
So that people will not have to guess and use unreliable telepathy.
Also, the assignment calls for you to implement a function with an ungood signature.
It teaches a Bad Way™ to design functions.
Here is a possible ordinary C++ function signature:
void getMinAndMax( int& smallest, int& largest, int a, int b, int c )
Here is an even better signature with modern C++ technology:
std::pair<int, int> minAndMax( int a, int b, int c )
The absence of a get prefix for the latter function's name is because it is an expression-oriented function, like sin and cos (you wouldn't write getSin or getCos, would you?), while the presence of that prefix for the first function is merely to make the name imperative, to reflect that it's not an expression-oriented function but instead an action-oriended function.
Of course, with C++11 one would let the function accept any number of arguments. Except that as I'm writing this, Visual C++ does not yet support that properly. For example, here is the signature of std::min from the C++11 standard library:
template<class T, class Compare>
T min(initializer_list<T> t, Compare comp);
With C++03 one can do that to some degree by accepting a single container argument, of templated type.

Related

Finding all the ordered pairs of integers lying on a line ax+by=c in better than O(n^2) time complexity [duplicate]

This question already has an answer here:
What's algorithm used to solve Linear Diophantine equation: ax + by = c
(1 answer)
Closed 5 years ago.
I am trying to write a code which can input 3 long int variables, a, b, c.
The code should find all integer (x,y) so that ax+by = c, but the input values can be up to 2*10^9. I'm not sure how to do this efficiently. My algorithm is O(n^2), which is really bad for such large inputs. How can I do it better? Here's my code-
typedef long int lint;
struct point
{
lint x, y;
};
int main()
{
lint a, b, c;
vector <point> points;
cin >> c >> a >> b;
for(lint x = 0; x < c; x++)
for(lint y = 0; y < c; y++)
{
point candidate;
if(a*x + b*y == c)
{
candidate.x = x;
candidate.y = y;
points.push_back(candidate);
break;
}
}
}
Seems like you can apply a tiny bit of really trivial math to solve for y for any given value of x. Starting from ax + by = c:
ax + by = c
by = c - ax
Assuming non-zero b1, we then get:
y = (c - ax) / b
With that in hand, we can generate our values of x in the loop, plug it into the equation above, and compute the matching value of y and check whether it's an integer. If so, add that (x, y) pair, and go on to the next value of x.
You could, of course, make the next step and figure out which values of x would result in the required y being an integer, but even without doing that we've moved from O(N2) to O(N), which is likely to be plenty to get the task done in a much more reasonable time frame.
Of course, if b is 0, then the by term is zero, so we have ax = c, which we can then turn into x = c/a, so we then just need to check that x is an integer, and if so all pairs of that x with any candidate value of y will yield the correct c.

Determine the largest number among three numbers using C++

How can I determine the largest number among three numbers using C++?
I need to simplify this
w=(z>((x>y)?x:y)?z:((x>y)?x:y));
Conditionals do not simplify this.
Starting from C++11, you can do
w = std::max({ x, y, z });
w = std::max(std::max(x, y), z);
is one way.
big = a > b ? (a > c ? a : c) : (b > c ? b : c) ;
Use the simple if condition
int w = x;
if(y > w)
w = y;
if(z > w)
w = z;
Where w is the max among three.
A variant on oisyn's answer (use an initializer list) and Bathesheba's answer (invoke no copies) is to use std::ref to create an initializer list of references, and then use std::max normally:
using std::ref;
w = std::max({ref(x), ref(y), ref(z)});
This is only advantageous if creating a reference is cheaper than creating a copy (and it isn't for primitives like int)
Demo

C++ Is this a form of micro optimization

Is this micro-optimization, or is it optimization at all?
void Renderer::SetCamera(FLOAT x, FLOAT y, FLOAT z) {
// Checking for zero before doing addition?
if (x != 0) camX += x;
if (y != 0) camY += y;
if (z != 0) camZ += z;
// Checking if any of the three variables are not zero, and performing the code below.
if (x != 0 | y != 0 | z != 0) {
D3DXMatrixTranslation(&w, camX, camY, camZ);
}
}
Would running a for.. loop with the condition having vector.size() force the application to recount the elements in the vector on each loop?
std::vector<UINT> vect;
INT vectorSize = vect.size();
for (INT Index = 0; Index < vectorSize; Index++) {
// Do vector processing
}
// versus:
std::vector<UINT> vect;
for (INT Index = 0; Index < vect.size(); Index++) {
// Do vector processing
}
I'm using Visual Studio and as for the second question, it seems like something a compiler could optimize, but I'm just not sure on that.
Depending on the implementation of vector, the compiler may or may not understand that size is not changed. After all, you call different vector functions inside the loop, any of which might change size.
Since vector is a template, then the compiler knows everything about it, so if it works really hard, it could understand that size doesn't change, but that's probably too much work.
Often, you would want to write like this:
for (size_t i = 0, size = vect.size(); i < size; ++i)
...
While we're at it, a similar approach is used with iterators:
for (list<int>::iterator i = lst.begin(), end = lst.end(); i != end; ++i)
...
Edit: I missed the first part:
Is this optimization?
if (x != 0) camX += x;
if (y != 0) camY += y;
if (z != 0) camZ += z;
No. First of all, even if they were int, it wouldn't be optimization since checking and branching when the values are probably most of the times not zero is more work.
Second and more importantly, they are float. This means that besides the fact that you shouldn't directly compare them to 0, they are basically almost never exactly equal to 0. So the ifs are 99.9999% true.
Same thing applies to this:
if (x != 0 | y != 0 | z != 0)
In this case however, since matrix translation could be costly, you could do:
#define EPS 1e-6 /* epsilon */
if (x > EPS || x < -EPS || y > EPS || y < -EPS || z > EPS || z < -EPS)
and now yes, comparing to a matrix multiplication, this is probably an optimization.
Note also that I used || which gets short-circuited if for example right from the beginning x > EPS is true (it won't calculate the rest), but with | that won't happen.
I suspect that on many architectures the first three lines are an anti-optimization because they may introduce a floating point compare and then branch which can be slower than just always doing the addition (even if it's floating point).
On the other hand making sure that at least one component is non-zero before doing the transformation seems sound.
For your second case, size has to be constant time and will almost certainly be inlined out to a direct access to the vector's size. It's most likely fully optimizable. That said, sometimes it can make the code/loop easier to read by saving the size off because that clearly shows you are asserting the size won't change during the loop.
Firstly, Regarding the vector.size(), see
this SO question. On a side note, I haven't seen an implementation where std::vector::size() isn't O(1).
if (x != 0) camX += x; this cmp and consequent jne however is going to be slower than simply adding the variable x no matter what. Edit: Unless you expect well over 50 % cache misses on camX
The first one is probably a pessimisation, the check for 0 is probably slower than the addition. On top of that, in the check before the call to D3DXMatrixTranslation, you use | instead of the short-circuiting logical or ||. Since the check before the function call is probably a time-saver (or even semantically necessary), wrap the entire code in that check,
void Renderer::SetCamera(FLOAT x, FLOAT y, FLOAT z) {
if (x != 0 || y != 0 || z != 0) {
camX += x;
camY += y;
camZ += z;
D3DXMatrixTranslation(&w, camX, camY, camZ);
}
}
if all of x, y and z are zero, nothing need be done, otherwise, do all.
For the second, the compiler can hoist the vector.size() outside the loop if it can determine that the size doesn't change while the loop runs. If the compiler cannot determine that, it must not hoist the size() computation outside the loop.
Doing that yourself when you know that the size doesn't change is good practice.

how does getdepth function work in MarchingCube algorithm?

I am trying to understand Marching Cube Algorithm, so for I think I have understood how triangles are formed and how normals are calculated in each grid. I can see there is a linked list kind of structure that links each grid to another. But when I come across GetDepth(t[m]) which passes each triangles (those triangles of each grid) (t[0],..,..)individually, it returns depth of the node.
The function,
float GetDepth(TRIANGLE t) {
float z;
z = t.p[0].z;
z = t.p[1].z > z? t.p[1].z: z;
z = t.p[2].z > z? t.p[2].z: z;
return z;
}
It looks like its trying to find max z(is it true).
I can see that it compares " > " and then I lost it.
Can any one please explain what is happening here.
It would seem that you are unfamiliar with ? as a ternary operator.
The code you posted is equivalent to the following:
float GetDepth(TRIANGLE t) {
float z;
z = t.p[0].z;
if (t.p[1].z > z) {z = t.p[1].z;} else {z = z;}
if (t.p[2].z > z) {z = t.p[2].z;} else {z = z;}
return z;
}
And yes, this does find the maximum z in the p array.

Algorithm for finding a rectangle constrained to its parent

Basically what I want to do is illustrated here:
I start with A and B, then B is conformed to A to create C.
The idea is, given TLBR rectangles A, B, make C
I also need to know if it produces an empty rectangle (B outside of A case).
I tried this but it just isn't doing what I want:
if(clipRect.getLeft() > rect.getLeft())
L = clipRect.getLeft();
else
L = rect.getLeft();
if(clipRect.getRight() < rect.getRight())
R = clipRect.getRight();
else
R = rect.getRight();
if(clipRect.getBottom() > rect.getBottom())
B = clipRect.getBottom();
else
B = rect.getBottom();
if(clipRect.getTop() < rect.getTop())
T = clipRect.getTop();
else
T = rect.getTop();
if(L < R && B < T)
{
clipRect = AguiRectangle(0,0,0,0);
}
else
{
clipRect = AguiRectangle::fromTLBR(T,L,B,R);
}
Thanks
You seem to have a mistake in the final condition checking whether or not the intersection rectangle is empty.
You check L < R && B < T, but it seems like the condition for an empty rectangle should be:
L > R || B < T.
By the way, you can make your code a little simpler and easier to read by using Min and Max functions. You have a lot of this pattern:
if (x < y)
a = x;
else
a = y;
Which can be written simply as
a = Min(x, y);
Edit
Another mistake is that you take the maximum bottom and the minimum top. You should be taking the minimum bottom and the maximum top. (Assuming the rectangles correspond to screen coordinates, where the top actuallly has lower y values.
Logically, these are two different problems. I would first write an is_intersected() function returning an appropriate boolean value.
If the rects do intersect, I would then perform a clip operation that resembled the following pseudocode:
C.left.x = max(A.left.x, B.left.x);
C.right.x = min(A.right.x, B.right.x);
C.left.y = max(A.left.y, B.left.y);
C.right.y = min(A.right.y, B.right.y);