Is the output of the following REPEAT UNTIL code complier-dependent? - loops

Is the output of the following code compiler-dependent?
x<-10;
REPEAT UNTIL (x<10)
{
x<-x+10;
}
DISPLAY(x);
One source says the REPEAT UNTIL part will not be executed so output will be 10, but some said the loop will be executed infinitely.

Related

while loop has terminating sign still works well

I was implementing Newton Raphson method in C. The code work well. There is no error in the code.
#include<stdio.h>
#include<math.h>
#define f(x)(x * sin(x)+cos(x))
#define df(x)(x*cos(x))
int main()
{
float x,h,e;
e=0.0001;
printf("Enter the initial value of x:\n");
scanf("%f",&x);
do
{
h=-f(x)/df(x);
x=x+h;
}
while(fabs(h)>e);
printf("The value of the root is=%f",x);
return(0);
}
/*
Output:
Enter the initial value of x: 3
The value of the root is = 2.798386
However, I was surprised I mean how did this code work? As per c rule while statement does not have any terminating semicolon. However, in my code while(fabs(h)>e); has a semicolon yet it run well.
Can anyone tells me how does it work?
What you mean is putting
while(...);
{
//some code
}
that will be interpreted as
while(...){
//looping without any instruction (probably an infinite loop)
}
{
//some code that will be executed once if the loop exits
}
do-while loop has the code executed before the condition (so at least once differently from simple while loop). The correct syntax has a semicolumn:
do{
//your code to be executed at least once
}while(...);
So the answer to your question is that :
do
{
h=-f(x)/df(x);
x=x+h;
}
while(fabs(h)>e);
is not a while statement, it's a do-while statement.

C language syntax: if { ... } while(0);

I am looking at https://github.com/iputils/iputils/blob/s20161105/ping.c and I see from lines 608-713:
if (source.sin_addr.s_addr == 0) {
... // Omitted
} while(0);
Two questions:
How it compiles
What is the value of adding while (0) after if statement
It's two statements in succession. Entirely equivalent to this
if(/* ... */) {
// Body
}
while(0) {
// empty
}
while(0); is just a loop with a single empty statement for a body.
There is no value in adding it after the if. I suspect it's leftover from a previous refactoring, but the git history in that repository does not go that far.
It compiles because it is two statements, first
if (...) { ... }
followed by another statement
while (0);
i.e. a loop with empty body that is never run.
There is no value. while (0); as a separate statement is utterly useless. do { ... } while (0) is not useless but it is a completely different thing.
Probably the code was refactored from a form that initially used do { ... } while (0) and using breaks to exit the flow early (from the linked question above). Currently the source file does not have any instances of do statement, there are only 3 times that the verb do exists in some strings.

Problems with long input/output in C

I have to make a program in C that gets a non-defined amount of double values and prints them all increased by a certain percentage based on the number. The program should stop when the user enters a negative value. It all works well when I use a small quantity of numbers, but when the input consists of a larger amount of numbers the program prints just the last ones.
Here's my code:
#include <stdio.h>
int main()
{
double ins=0; //the input
while(ins>=0){
scanf("%lf",&ins);
if(ins<0){break;}
else{
if(ins<500){printf("%.2lf ",ins*1.15);}
else if(ins<=1000){printf("%.2lf ",ins*1.10);}
else {printf("%.2lf ",ins*1.05);}
}
}
return 0;
}
Additional information: Using GCC compiler.
Example of output the program should give for a specific input.
Input:
4003.31 1212.35 3414.31
4257.1 1394.37 1217.28
3602.85 4218.58 4994.8
1133.82 1086.48 2117.43
2253.86 3827.71 2170.16
1161.27 3069.77 1338.08
2791.99 3709.33 180.43
4555.77 318.58 1912.24
158.68 2106.49 4439.56
1247.34 -0.79
Output I should get:
4203.48 1272.97 3585.03 4469.96
1464.09 1278.14 3782.99 4429.51
5244.54 1190.51 1140.80 2223.30
2366.55 4019.10 2278.67 1219.33
3223.26 1404.98 2931.59 3894.80
207.49 4783.56 366.37 2007.85 182.48
2211.81 4661.54 1309.71
What could I do to make the program work correctly not just with small amounts but also with quantities like the above ?
Edit: the output I'm getting with the above input is "1309.71",which is just the last number of the full output I should receive.
Note: You have a simple logic error. Because you are checking ins<500 before ins<=100 you can never hit the ins<=100 case.
Your code and sample data works for me. Can you give a more detailed description of the problem?
Well, I found out the solution. As you guys said that the code was working properly for you I tested it using another command line interface. And now the output is correct, so it seems like the one I was using before (Windows CMD) was deleting outputted information when that information reached a certain limit, maybe a small buffer size or something like that. Thank you for the help guys !!

C programming. Why does 'this' code work but not 'that' code?

Hello I am studying for a test for an intro to C programming class and yesterday I was trying to write this program to print out the even prime numbers between 2 and whatever number the user enters and I spent about 2 hours trying to write it properly and eventually I did it. I have 2 pictures I uploaded below. One of which displays the correct code and the correct output. The other shows one of my first attempts at the problem which didn't work correctly, I went back and made it as similar to the working code as I could without directly copying and pasting everything.
unfortunately new users aren't allowed to post pictures hopefully these links below will work.
This fails, it doesn't print all numbers in range with natural square root:
for (i = 2; i <= x; i++)
{
//non relevant line
a = sqrt(i);
aa = a * a;
if (aa == i);
printf("%d ",i);
}
source: http://i.imgur.com/WGG6n.jpg
While this succeeds, and prints even numbers with natural sqaure root
for (i = 2; i <= x; i++)
{
a = sqrt(i);
aa = a * a;
if (aa == i && ((i/2) *2) == i)
printf("%d ", i);
}
source: http://i.imgur.com/Kpvpq.jpg
Hopefully you can see and read the screen shots I have here. I know that the 'incorrect code' picture does not have the (i/2)*2 == i part but I figured that it would still print just the odd and even numbers, it also has the code to calculate "sqrd" but that shouldn't affect the output. Please correct me if I'm wrong on that last part though.
And Yes I am using Dev-C++ which I've read is kinda crappy of a program but I initally did this on code::blocks and it did the same thing...
Please I would very much appreciate any advice or suggestions as to what I did wrong 2 hours prior to actually getting the darn code to work for me.
Thank you,
Adam
your code in 'that' includes:
if (aa == i);
// ^
printf(...);
[note the ; at the end of the if condition]
Thus, if aa == i - an empty statement happens, and the print always occures, because it is out of the scope of the if statement.
To avoid this issue in the future, you might want to use explicit scoping1 [using {, } after control flow statements] - at least during your first steps of programming the language.
1: spartan programmers will probably hate this statement
Such errors are common. I use "step Over", "Step Into", "Break Points" and "watch window" to debug my program. Using these options, you can execute your program line by line and keep track of the variables used in each line. This way, u'll know which line is not getting executed in the desired way.

I have a function with a lot of return points. Is there any way that I can make gdb show me which one is returning?

I have a function with an absurd number of return points, and I don't want to caveman each one, and I don't want to next through the function. Is there any way I can do something like finish, except have it stop on the return statement?
You can try reverse debugging to find out where function actually returns. Finish executing current frame, do reverse-step and then you should stop at just returned statement.
(gdb) fin
(gdb) reverse-step
There is already similar question
I think you're stuck setting breakpoints. I'd write a script to generate the list of breakpoint commands to run and paste them into gdb.
Sample script (in Python):
lines = open(filename, 'r').readlines()
break_lines = [line_num for line_num, line in enumerate(lines) if 'return' in line and
line_num > first and line_num <= last]
break_cmds = ['b %s:%d' % (filename, line_num) for line_num in break_lines]
print '\n'.join(break_cmds)
Set filename to the name of the file with the absurd function, first to the first line of the function (this is a quick script, not a C parser) and last to the number of the last line of the function. The output ought to be suitable for pasting into gdb.
Kind of a stretch, but the catch command can stop on many kinds of things (like forking, exiting, receiving a signal). You may be able to use catch catch (which breaks for exceptions) to do what you want in C++ if you wrap the function in try/finally. For that matter, if you break on a line inside the finally you can probably single-step through the return after that (although how much that will tell you about where it came from is highly dependent on optimization: common return cases are often folded by gcc).
How about taking this opportunity to break up what seems to be clearly a too-large function?
This question's come up before on SO. My answer from there:
Obviously you ought to refactor this function, but in C++ you can use this simple expedient to deal with this in five minutes:
class ReturnMarker
{
public:
ReturnMarker() {};
~ReturnMarker()
{
dummy += 1; //<-- put your breakpoint here
}
static int dummy;
}
int ReturnMarker::dummy = 0;
and then instance a single ReturnMarker at the top of your function. When it returns, that instance will go out of scope, and you'll hit the destructor.
void LongFunction()
{
ReturnMarker foo;
// ...
}

Resources