Debugging in Visual Studio. Step into expression - c++

I am using Visual studio 2010. I want to know is there any option in the VS debugger to step into an expression.
Example:
int a = 5, b = 255, c = 10;
int result;
result = a + b*c + a&b ;
To understand the operator precedence, I want to know which expression in the result = a + b*c + a&b ; is executed first and which expression next.
Is there any option to do that?
Thanks.

try to put open and close parenthesis. it will execute first
result = (a + (b*c)) + a&b ;

Related

right operand of comma operator has no effect -wunused variable

While compiling simple cpp file I got an error. I want to write a function that changes celcius to farenheit.
double przelicznik(double n)
{
n = 1,8 * n + 32;
return n;
}
Also it doesn't give me a correct result.
The code is.
n = 1, (8 * n + 32)
The comma operator is a fairly uncommon mechanism where multiple expressions can be done in sequence.
correct code.
n = 1.8 * n + 32;

Increasing the index by 1 - Converting a code in C to MATLAB [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 4 years ago.
I'm manually converting a code in C to MATLAB.
The code contains assignments like the following,
y[10] = p[32]+(p[31]-p[32])*pow(p_c[0],p[34])/(pow(p_c[0],p[34])+pow(p[33],p[34]));
To use it in MATLAB, I am manually increasing the value of all indexes by 1, since the index in MATLAB starts from 1.
y[10+1] = p[32+1]+(p[31+1]-p[32+1])*pow(p_c[0+1],p[34+1])/(pow(p_c[0+1],p[34+1])+pow(p[33+1],p[34+1]));
Is there an easy way of doing this task?
There are around 30 assignments like the above example and I am trying to avoid doing this manually.
Edit 1:
Would it be possible to use regular expression? I can copy all the lines of code that contain assignment in a text file. Using regular expression, if I can locate [35](any number) replace with [35+1]. I'm not sure how to implement this in a code.
Edit 2:A sample of other assignments in the code.
y[0] = ct[0]-x[12];
y[1] = ct[1]-1*x[10]-x[23];
y[3] = p_c[6]+p_c[5];
y[4] = p_c[2];
y[5] = x_c[23]+x_c[10]+y_c[1];
y[6] = y_c[0]+x_c[12];
p[0] = 30;
p[1] = 12;
p[2] = 2;
p[3] = 0;
p[4] = 90;
p[5] = 45
dx[0] = FunctionForD(p[67], p[64], p[66], p[65], p[23], x_c[0], x_c[3], p[49])*p[23]-FunctionForA(y[28], y[29], p[23], y[16])*p[23]+FunctionForD(y[30], y[31], p[23], y[16])*p[23]-FunctionForA(p[134], p[133], p[132], p[130], p[131], p_c[2], p[23], x_c[21], x_c[0], p[49])*p[23]; //
dx[1] = FunctionFor2(p[169], p[167], p[168], p[166], p[23], x_c[1], x_c[17], p[49])
If you were dealing with just one array, you could come up a way to do this. But here you are dealing with 8 different arrays (y, ct, x, p_c, x_c, y_c, p, dx). And the assignments too are in no particular order. They involve various combinations.
If you are using Linux/Unix, you can use the stream editor (sed) tool to accomplish this.
For windows, Notepad++ (which is free) supports regex search and replace. Have a look at this link.
If it is just 30 assignments better do it manually. You can ensure that each index is correctly incremented by 1 in MATLAB by doing something like this:
#define BMI 1 /* BMI is BASE_MATLAB_INDEX */
y[0 + BMI] = ct[0 + BMI] - x[12 + BMI];
and so on...

Expression evaluator without PEMDASM

I have a question. I have these codes.
int x = 20 , y = 2 , z = 5 , a = 0, b = 2 ;
a = x*y-z/b
cout<<"a is "<<a;
done;
when performing this, I want it to be performed like a = 20 * 2 - 5 /2 and I want the result to be 17.5. c++ normally follows PEMDASM rules. How can I perform the expression without following the PEMDAS rule? thanks. I want it to perform according to the order in which they appear first, * appeared first, then - then / . thank you so much
You have to use parentheses:
a = (x*y - z)/b;

Understanding (and forming) the regular expression of this finite automaton

For the above automaton, the regular expression that has been given in my textbook is the following :
a*(a*ba*ba*ba*)*(a+a*ba*ba*ba*)
I am having trouble deriving this...the following is my attempt at it :
aa* + aa*(ba*ba*ba*)* + ba*ba*ba* + ba*ba*ba*(ba*ba*ba*)*
Either I am wrong or I am not being able to simplify it into the form given in the book. Can someone please guide me here, point out the mistake or explain it to me step-by-step?
I'd really really thankful and appreciate that.
Check this out. It presents three good, algorithmic methods for answering questions like these. Learn one of them, or all three of them if you have the time or inclination. State removal is fairly intuitive, although I like Kleene's transitive closure method.
http://krchowdhary.com/toc/dfa-to-reg-exp.pdf
EDIT: Your RE is equivalent to the one provided. here's the reduction of theirs to yours:
0. a*(a*ba*ba*ba*)*(a+a*ba*ba*ba*)
1. = a*(a*ba*ba*ba*)*a + a*(a*ba*ba*ba*)*a*ba*ba*ba*
2. = a*(ba*ba*ba*)*a + a*(ba*ba*ba*)*ba*ba*ba*
3. = a*a + a*(ba*ba*ba*)*a + a*(ba*ba*ba*)*ba*ba*ba*
4. = aa* + a*(ba*ba*ba*)*ba*ba*ba*a + a*(ba*ba*ba*)*ba*ba*ba*
5. = aa* + a*(ba*ba*ba*)*ba*ba*ba*
6. = aa* + aa*(ba*ba*ba*)*ba*ba*ba* + (ba*ba*ba*)*ba*ba*ba*
7. = aa* + aa*(ba*ba*ba*)* + (ba*ba*ba*)*ba*ba*ba*
8. = aa* + aa*(ba*ba*ba*)* + ba*ba*ba* + (ba*ba*ba*)*ba*ba*ba*
9. = aa* + aa*(ba*ba*ba*)* + ba*ba*ba* + ba*ba*ba*(ba*ba*ba*)*
Step 1 is right since r(s+t) = rs + rt.
Step 2 is right since r*(r*sr*)* = r*(sr*)*.
Step 3 is right since r = r + s if L(s) is a subset of L(r).
Step 4 is right since r*r = rr* and rs + rq*s = rs + rqq*s.
Step 5 is right since rs + r = r.
Step 6 is right since r*s = rr*s + s.
Step 7 is right since rs + rqq*s = rs + rq*s.
Step 8 is right since r = r + s if L(s) is a subset of L(r).
Step 9 is right since r*r = rr*.
Please feel free to ask any questions or point out any mistakes I may have made.
EDIT2: If you are interested in these kinds of questions, show some love for the new Computer Science StackExchange by going to this link and committing!!!
http://area51.stackexchange.com/proposals/35636/computer-science-non-programming?referrer=rpnXA1_2BNYzXN85c5ibxQ2
The textbook seems correct. Taking it step by step:
a*(a*
If this part of the regular expression is true (in other words you do actually read in an 'a'), you will move to state 3. Following the rest of the expression:
ba*
will have you in state 2,
ba*
in state 4 and
ba*
will have you back in state 3.
Now, suppose that you did not read in an 'a' during a*(a*, reading the next b will move you to state 2. You then end up in exactly the same situation as previously, and by following the rest a*ba*ba*) you end up back in state 3.
Since you are now back in state 3, the part (a*ba*ba*ba*)* can execute as many times as it wants, as this will simply be the same as our first scenario (where you read in an 'a' during a*(a*).
The second part simply explains the first scenario again, where you have to read at least one 'a', and then the rest is the same.
Hope this helps, let me know if it still does not make sense. Don't know if my explanation is too clear.

Why does this take so long to compile in VCC 2003?

My team need the "Sobol quasi-random number generator" - a common RNG which is famous for good quality results and speed of operation. I found what looks like a simple C implementation on the web. At home I was able to compile it almost instantaneously using my Linux GCC compiler.
The following day I tried it at work: If I compile in Visual Studio in debug mode it takes about 1 minute. If I were to compile it in release mode it takes about 40 minutes.
Why?
I know that "release" mode triggers some compiler optimization... but how on earth could a file this small take so long to optimize? It's mostly comments and static-data. There's hardly anything worth optimizing.
None of these PCs are particularly slow, and in any case I know that the compile time is consistent across a range of Windows computers. I've also heard that newer versions of Visual Studio have a faster compile time, however for now we are stuck with Visual Studio.Net 2003. Compiling on GCC (the one bundled with Ubuntu 8.04) always takes microseconds.
To be honest, I'm not really sure the codes that good. It's got a nasty smell in it. Namely, this function:
unsigned int i4_xor ( unsigned int i, unsigned int j )
//****************************************************************************80
//
// Purpose:
//
// I4_XOR calculates the exclusive OR of two integers.
//
// Modified:
//
// 16 February 2005
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, unsigned int I, J, two values whose exclusive OR is needed.
//
// Output, unsigned int I4_XOR, the exclusive OR of I and J.
//
{
unsigned int i2;
unsigned int j2;
unsigned int k;
unsigned int l;
k = 0;
l = 1;
while ( i != 0 || j != 0 )
{
i2 = i / 2;
j2 = j / 2;
if (
( ( i == 2 * i2 ) && ( j != 2 * j2 ) ) ||
( ( i != 2 * i2 ) && ( j == 2 * j2 ) ) )
{
k = k + l;
}
i = i2;
j = j2;
l = 2 * l;
}
return k;
}
There's an i8_xor too. And a couple of abs functions.
I think a post to the DailyWTF is in order.
EDIT: For the non-c programmers, here's a quick guide to what the above does:
function xor i:unsigned, j:unsigned
answer = 0
bit_position = 1
while i <> 0 or j <> 0
if least significant bit of i <> least significant bit of j
answer = answer + bit_position
end if
bit_position = bit_position * 2
i = i / 2
j = j / 2
end while
return answer
end function
To determine if the least significant bit is set or cleared, the following is used:
bit set if i <> (i / 2) * 2
bit clear if i == (i / 2) * 2
What makes the code extra WTFy is that C defines an XOR operator, namely '^'. So, instead of:
result = i4_xor (a, b);
you can have:
result = a ^ b; // no function call at all!
The original programmer really should have know about the xor operator. But even if they didn't (and granted, it's another obfuscated C symbol), their implementation of an XOR function is unbelievably poor.
I'm using VC++ 2003 and it compiled instantly in both debug/release modes.
Edit:
Do you have the latest service pack installed on your systems?
I would recommend you download a trial edition of Visual Studio 2008 and try the compile there, just to see if the problem is inherent. Also, if it does happen on a current version, you would be able to report the problem, and Microsoft might fix it.
On the other hand, there is no chance that Microsoft will fix whatever bug is in VS2003.