hi guys.
Another problem facing in my profiler program is with branches.
I need to change this: - x = 5;
-
y = x + 2x + 7;
-
if (x > y)
-
printf(“x is %d\n”, x);
-
else
-
printf(“y is %d\n”, y);
-
to this: - x = 5;
-
y = x + 2x + 7;
-
if (x > y) { branch[0]++;
-
printf(“x is %d\n”, x);
-
}
-
else { branch[1]++;
-
printf(“y is %d\n”, y);
-
}
-
The problem is when i read an "if" line, i have to check include thebraces since i'm adding another statement that must be inside the if. Now i know the braces won't be there for the above one, but what if i have a : - if (....)
-
if (.....)
-
{
-
....
-
}
-
How would i know when to close the matching braces that i opened??
Can anyone think of a way?
Thank you.
10 1388
Sebouh-
Could you please clarify your qestion? It's a good idea to put all loop and conditional statements in brackets, just in case you want to go back later and add something, and if statements, if you don't have the brackets, will execute only the statement on the next line (however, you can do chains of if - if - if statements, but it's not good practice). I would suggest including everything you want to be done when an 'if' statement is executed inside brackets, but again, I'm not sure I understand your question...
Sebouh-
Could you please clarify your qestion? It's a good idea to put all loop and conditional statements in brackets, just in case you want to go back later and add something, and if statements, if you don't have the brackets, will execute only the statement on the next line (however, you can do chains of if - if - if statements, but it's not good practice). I would suggest including everything you want to be done when an 'if' statement is executed inside brackets, but again, I'm not sure I understand your question...
OKay, let me explain better.
I want to count how many times the branch is taken, that's why i add a branch[i]++. I can always open a braket, put the branch thing there with the other statements and then close the braket. But i don't know when to close the braket. When do the statements (or chain of ifs) end? I need to know when they end so that i can let's say close the first if's braket.
OKay, let me explain better.
I want to count how many times the branch is taken, that's why i add a branch[i]++. I can always open a braket, put the branch thing there with the other statements and then close the braket. But i don't know when to close the braket. When do the statements (or chain of ifs) end? I need to know when they end so that i can let's say close the first if's braket.
In an if statement, it's either all done, or not. It ends where you want it to, after everything has been done that you want to happen if the selected branch has been taken: -
if (conditional)
-
// only this statement is done
-
// this one is not, even though it is indented
-
-
if (conditional2) {
-
// this statement is done
-
// and this one is too
-
// and all the ones up until the last bracket
-
}
-
// and this one is done, because the conditional is over, and it's the next after the if
-
-
if (false_condition)
-
// this statement isn't done, because it's false - it never enters the 'if'
-
-
if (false_condition2) {
-
// this isn't done
-
// and neither are any of these
-
// until after the bracket is closed again
-
}
-
// but this one is, because it's not in the if
-
Your branch[i]++ will work, but I'm not sure what you're saying about 'ending' if statements, you want to end them, when you're done doing things that relate specifically to that branch.
Does that answer your question?
... -
if (conditional)
-
// only this statement is done
-
// this one is not, even though it is indented
-
...
-
...
This should be -
if (conditional)
-
// only this statement is done
-
// this one is always done, its outside the scope
-
This should be -
if (conditional)
-
// only this statement is done
-
// this one is always done, its outside the scope
-
Ah, good catch! Thanks stroken - I did mean that it was not done as part of the if statement - it was outside it (which would mean it is always done). Thanks again.
Maybe i'm being unclear.
Ok, what my program must do is modify the C code it get's as input. An example of that modification is to take this code:
and produce this code: -
if (true)
-
{
-
branch[i]++;
-
<statement>;
-
}
But if i wan't to modify this: -
if (true)
-
if (true)
-
{
-
<stmt1>;
-
<stmt2>;
-
}
I have to add a { to the first if, and add a } at it's end, to make the branch[i]++ part of this if.
The problem i'm facing is: When do i know where to insert the }? It' not the case that the first is has 1 line under it.
I think i'm starting to come up with a solution. Please, tell me if this would work.
If the first if has no braces, i would add one, and update a brace-stack. Everytime i get a {, i'd push one, and pop it if i get a matching } and at the end when the stack is left with a {, i would insert the } in the code.
Well after some analysis, i've come to the conclusion that the stack solution doesn't work either. There is the question on when to check for 1 left "{". with chained ifs where the last if contains a large ( > 2 ) body, during the procssing of the body, i wiil en dup checking the stack and inserting a "}" there, leading to: -
if (...)
-
{
-
if (....)
-
{
-
} // checked for single { and found true, so inseted a }
-
<stmt>
-
<stmt>
-
}
And after more analysis and brainstorming, i've come up with a possible solution that includes recursion. I'll let you know if it worked... ;)
Do you know if the code will be properly formatted? You could do it based on indentation, though that would not work with anything that isn't properly indented...
@OP: did you read my last reply in your other thread ?
kind regards,
Jos
Well many of the stuff i'm doing work only with proper formatting, not indentation wise but proper location of braces,proper statements (not 10 on 1 line)...
JosAH: I did read your last post, but i'm not familiar with the concept. So if i have to read about them and learn, i'm not sure it will be a shortcut. I'm due friday on this assignment and i'm not half way there yet. Besides, i don't think the instructor expects me to work with those, so i'm playing it safe.
However thanks for that reply, and i will be reading about them as soon as i get the chance.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Albert |
last post by:
Why doesn't:
#include <stdio.h>
void reverse(char, int);
main()
{
char s;
|
by: Robin Becker |
last post by:
When young I was warned repeatedly by more knowledgeable folk that self
modifying code was dangerous.
Is the following idiom dangerous or unpythonic?
def func(a):
global func, data
data =...
|
by: GaryB |
last post by:
Hi Guys,
I've been battling with this one for hours - I hope that you can help
me!
My code modifies the <aon a page, from a standard document link into
a link with a tailored onclick event.
...
|
by: reycri |
last post by:
While the following is allowed:
if (a == b)
SomeFunction();
else
OtherFunction();
The following is not:
try
|
by: davidz |
last post by:
My office has adopted Team Foundation Server to track and manage the
development and deployment process at our site. I have been asked to
modify one of the standard work item reports, "Related...
|
by: _mario.lat |
last post by:
for struct:
struct in6_addr {
uint8_t s6_addr;
};
is provided a costant:
#define IN6ADDR_LOOPBACK_INIT {{{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}}}
what does means {{{, and }}}?
|
by: allpervasive |
last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems
in reading and modifying the contents of a file,, hope you can help to
solve this problem. Here i attach the file to be...
|
by: lasing |
last post by:
Hi all,
recently encounter following bugs:
if (a b); //<- bug in the careless ";" here
return 0;
Due to the extra ";" in the if statement line, it always return 0.
Anyone know g++...
|
by: dspfun |
last post by:
Hi!
The words "expression" and "statement" are often used in C99 and C-
textbooks, however, I am not sure of the clear defintion of these
words with respect to C.
Can somebody provide a sharp...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |