While loop looking for unexpected input for no reason? - loops

I am trying to run a while loop program that has nothing to do with input. It is merely supposed to tell me what the final value of the calculation will be. However when I run the program it does nothing. It doesn't end either. I am confused at what is happening?
int x = 90;
while (x < 100)
{
x += 5;
if (x > 95)
x -= 25;
}
System.out.println( "final value for x is " + x);

The loop never terminates because x never reaches 100. If you want to see for yourself what's happening to x, add a line to your loop so that the code looks like this:
int x = 90;
while (x < 100) {
System.out.println("x = " + x); // More useful output here...
x += 5;
if (x > 95)
x -= 25;
}
System.out.println("final value for x is " + x);

What happen is that your while loop is never stopping, so it never prints something, try changing your code inside the loop.
How can you realize about that?
Put some prints out and inside the while loop:
int x = 90;
System.out.println("Before the while");
while (x < 100) {
System.out.println("Inside the while");
x += 5;
if (x > 95)
x -= 25;
}
System.out.println("final value for x is " + x);
Iteration 1:
x = 95
Iteration 2:
x = 100
if condition is true, so x = 75
... So whenever x reaches 100 the condition will make it 75. Therefore the while never ends.

Related

I intend to create an infinity for loop but it came out with a result

int x , sum = 0;
for (x = 10; x > 0; x++) {
sum += x;
}
I am expecting the loop never end because x will never hit 0.
System.out.println(sum);
It surprises me when it outputs this value -1073741869 , where my x is incrementing instead of decrementing.

write C program to compute the numeric root

The numeric root of x is computed as follows:
a) Compute the sum, y, of all of x’s (decimal) digits;
b) If y is greater than 10, then set x to y and go to step a). Otherwise, y is x’s numerical root.
Thus, the numeric root of 10, 202 and 875 are 1, 4 and 2, respectively.
Here is my code:
#include <stdio.h>
int main(void)
{
int x, y, index, found;
found = 0;
scanf("%d", &x);
if (x < 0)
{
printf("The input number must be nonnegative.\n");
}
else
{
y = 0;
index = x % 10;
while (found != 1)
{
while (x > 10)
{
y = y + index;
x = (x - index) / 10;
index = x % 10;
}
y = y + index;
if (y < 10)
{
printf("%d\n", y);
found = 1;
}
else
{
x = y;
}
}
}
return 0;
}
My output are always numbers like "-2147483623" etc.
Any help will be appreciated.
Your solution can be greatly simplified with the use of a simple function.
See the following solution using the Ada language:
with Ada.Integer_Text_IO; use Ada.Integer_Text_Io;
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
Inpt_Value : Natural;
Root : Natural := 0;
function Find_Root(X : Natural) return Natural is
Value : Natural := X;
Root : Natural := 0;
begin
while Value > 0 loop
Root := Root + (Value mod 10);
Value := Value / 10;
end loop;
return Root;
end Find_Root;
begin
Put("Enter a non-negative integer: ");
Get(Item => Inpt_Value);
Root := Find_Root(Inpt_Value);
while Root > 10 loop
Root := Find_Root(Root);
end loop;
Put_Line(Root'Image);
end Main;
I think that your problem is here :
if(y < 10){
printf("%d\n",y);
found = 1;
}
else{
x = y;
}
in your else statement you assign the value of x to y, but you don't change the value of y.
Maybe you should do something like this :
if(y < 10){
printf("%d\n",y);
found = 1;
}
else{
x = y;
y = 0;
index = x % 10;
}
In your entire program you don't seem to decrease or reset the value of y, that's why once the else block entered, there is no way to enter the if block, because y will increase and always be greater than 10 until an overflow happens, that's why y is negative at the end.
PS: Always use a debugger or printf statements it will help you detect such problems.

Calculate the number of all possible execution paths in a C function

I am desperately looking for a way to easily calculate the number of all possible execution paths in a C function.
For example, for the following function I would expect to get a result of 3 (if there is a chance based on the values that 'i' gets to enter any of the 'if' statements)
void test(void)
{
if (i>0)
x = x + 1;
else if (i>10)
x = x + 2;
else
x = x + 3;
}
Use comma operator as
int test(void)
{
int ways = 0;
if (++ways, i>0)
x = x + 1;
else if (++ways, i>10)
x = x + 2;
else
{
x = x + 3;
++ways;
}
return ways;
}

Control Instructions in C

I am not understanding for loop statement and expression following it. Please do help me understand.
#include<stdio.h>
int main()
{
int x = 1;
int y = 1;
for( ; y ; printf("%d %d\n",x,y))
y = x++ <= 5;
return 0;
}
And the output I got
2 1
3 1
4 1
5 1
6 1
7 0
y = x++ <= 5; ==> y = (x++ <= 5); ==> first compare x with 5 to check whether x is small then or equals to 5 or not. Result of (x++ <= 5) is either 1, 0 assigned to y,
As x becomes > 5, (x++ <= 5) becomes 0 so y = 0 and condition false and loop break,
Basically the for syntax is:
for(StartCondition; Test; PostLoopOperation) DoWhileTestPasses;
In this case:
StartCondition == None
Test == (y != 0)
PostLoopOperation == do some printing
DoWhileTestPasses == set y to zero if x > 5 otherwise to non-zero THEN increment x.
Which is all rather bad practice because it is confusing.
Would be better written as:
int x=0;
int y=0;
for(y=0; y = (x <= 6); x++)
{
printff("%d %d\n",x,y);
}
return(0);
In y = x++ <= 5;, y stores the value that is output by the condition x++ <= 5 (here x++ is post increment). If the condition is true then y = 1 else y = 0.
for( ; y ; printf("%d %d\n",x,y))
In the for loop you are printing the values of x and y after executing the for loop body.
Initialize your variables:
int x = 1; int y = 1;
There are 3 statements for the for loop: -1. Initialize, 2. Condition, 3. Iteration:increment/decrement
In your case, you did not provide the initialize condition, however, you have the part of condition and incrementation. I do not think your for loop is used in the correct way.
You should swap the part of incrementation with your body like this:
for(; y; y = x++ <= 5;)
printf("%d %d\n", x, y)
First, you check whether the condition is true or not, y is true or not. Then, you print x and y out. Then, the part of incrementation is executed, x++ <= 5 or not. The result is assigned to y. It does so until your condition is false, y == false.
NOTE: For the good programming, you should enclose your body with a curly braces.
similar to this
int x = 1;
for( int y = 1; y!=0 ; )
{
if (x++ <= 5)
{
y = 1;
}
else
{
y = 0;
}
printf("%d %d\n",x,y);
}
Perhaps this slightly transformed (but functionally equal) code will help:
int x = 1;
int y = 1;
while (y) {
y = (x <= 5);
x = x + 1;
printf("%d %d\n", x, y)
}

Trying to get this for loop to work?

I just started to learn C so the answer is probably incredibly obvious but when I run this code the number 0 just keeps repeating in an infinite loop. I'm trying to print x from 0 to 1 in increments of .05.
#include <stdio.h>
int main()
{
double x;
for( x = 0; x <= 1; x+.05 )
{
printf("%d\n", x );
}
}
for( x = 0; x <= 1; x += .05 )
seems like your not writing the changed x value to x..... If you know what I mean :D
x++ is the same as x+=1
x+.05 doesn't modify x's value, thus x will always be 0 and result in a infinite loop...
I think that's what you're looking for:
for( x = 0; x <= 1; x+=0.05 )
{
printf("%f\n", x );
}
You'll want to change to the += sign and change the d to an f.
d is for decimal integers
f is for floating point numbers
You want the addition and assignment compound operator, which is +=, not just +.
for( x = 0; x <= 1; x+=.05 )
Currently the result of your expression is x + 5, and its result is not used, resulting in your loop's condition never being false.
Change the line in your for loop to
for( x = 0; x <= 1; x += .05 )
Note that
x += .05
Is equivalent to typing
x = x + .05
which is what you really want since the goal is to update the value of x.

Resources