Why do we write END IF statement in this program? Without writing it, we can easily get our result. Is there any example through which you can explain me the use of END IF statement?
I have tried this:
INPUT X
IF X>10 THEN PRINT "X IS GREATER THAN 10" ELSE PRINT "X IS NOT GREATER THAN 10"
END
then I am also getting expected result.
The real code is:
INPUT X
IF X>10 THEN
PRINT "X IS GREATER THAN 10"
ELSE
PRINT "X IS NOT GREATER THAN 10"
END IF
END
EXPECTED AND DESIRED
FOR EXAMPLE:
When X=5 Then Output will be "X IS NOT GREATER THAN 10".
END IF is needed to indicate the ending of an IF, ELSE IF, ELSE structure written in multiple lines. If there is no END IF (the absence of which will lead to an error) then the statements under IF, ELSE IF, ELSE structure will be considered as a part of the IF, ELSE IF, ELSE structure until there comes an END IF. If there were no END IF, the use of IF, ELSE IF, ELSE structure would have been limited to be used only at the end of the program
Consider a situation where you would want to run 100 particular lines if the condition is true and 100 other lines if the condition is false and 100 more lines which must always execute after the IF-ELSE structure. Now obviously you cannot write all of these 200 IF ELSE related lines in a single line. And if there were no END IF then there would have been no way to run the next 100 lines.
END IF is invalid for the IF, ELSE IF, ELSE statements written in one line. Any statement in the next line will be considered out of the IF, ELSE IF, ELSE structure. It is not always possible to code all of the required functionality in one line. So, it can only be used when a small functionality that can be written in one line is to be triggered on the basis of some conditions. So this is the short coming of this one-liner approach.
Multiple indented if/then/else statements can be combined into a structure easier than a single line statement, such as:
INPUT X
IF X > 10 THEN
PRINT "X is greater than 10."
ELSE
IF X < 10 THEN
PRINT "X is less than 10."
ELSE
PRINT "X is equal to 10."
END IF
END IF
Which is the same as:
INPUT X
IF X > 10 THEN PRINT "X is greater than 10." ELSE IF X < 10 THEN PRINT "X is less than 10." ELSE PRINT "X is equal to 10."
What would be extremely difficult is testing the value of 3 numbers in a single line if/then, such as:
INPUT X, Y, Z
IF X = 0 AND Y = 0 AND Z = 0 THEN PRINT "All zero." ELSE IF X = 0 AND Y <> 0 AND Z = 0 THEN PRINT "X and Z zero." ELSE IF X <> 0 AND Y = 0 AND Z = 0 THEN PRINT "Y and Z zero." ELSE IF X = 0 AND Y = 0 AND Z <> 0 THEN PRINT "X and Y zero." ELSE IF X <> 0 AND Y <> 0 AND Z = 0 THEN PRINT "X and Y non-zero." ELSE IF X <> 0 AND Y = 0 AND Z <> 0 THEN PRINT "X and Z non-zero." ELSE IF X = 0 AND Y <> 0 AND Z <> 0 THEN PRINT "Y and Z non-zero." ELSE PRINT "All non-zero."
Another example of determining 3 input values:
COLOR 15
DO
PRINT "Enter values(y/n)";: INPUT x$
IF LCASE$(x$) = "n" THEN END
PRINT "Enter X,Y,Z";: INPUT X, Y, Z
GOSUB Calculate
LOOP
END
Calculate:
SELECT CASE X
CASE IS > 0
SELECT CASE Y
CASE IS > 0
SELECT CASE Z
CASE IS > 0
PRINT "X is positive, Y is positive, Z is positive."
CASE IS < 0
PRINT "X is positive, Y is positive, Z is negative."
CASE ELSE
PRINT "X is positive, Y is positive, Z is zero."
END SELECT
CASE IS < 0
SELECT CASE Z
CASE IS > 0
PRINT "X is positive, Y is negative, Z is positive."
CASE IS < 0
PRINT "X is positive, Y is negative, Z is negative."
CASE ELSE
PRINT "X is positive, Y is negative, Z is zero."
END SELECT
CASE ELSE
SELECT CASE Z
CASE IS > 0
PRINT "X is positive, Y is zero, Z is positive."
CASE IS < 0
PRINT "X is positive, Y is zero, Z is negative."
CASE ELSE
PRINT "X is positive, Y is zero, Z is zero."
END SELECT
END SELECT
CASE IS < 0
SELECT CASE Y
CASE IS > 0
SELECT CASE Z
CASE IS > 0
PRINT "X is negative, Y is positive, Z is positive."
CASE IS < 0
PRINT "X is negative, Y is positive, Z is negative."
CASE ELSE
PRINT "X is negative, Y is positive, Z is zero."
END SELECT
CASE IS < 0
SELECT CASE Z
CASE IS > 0
PRINT "X is negative, Y is negative, Z is positive."
CASE IS < 0
PRINT "X is negative, Y is negative, Z is negative."
CASE ELSE
PRINT "X is negative, Y is negative, Z is zero."
END SELECT
CASE ELSE
SELECT CASE Z
CASE IS > 0
PRINT "X is negative, Y is zero, Z is positive."
CASE IS < 0
PRINT "X is negative, Y is zero, Z is negative."
CASE ELSE
PRINT "X is negative, Y is zero, Z is zero."
END SELECT
END SELECT
CASE ELSE
SELECT CASE Y
CASE IS > 0
SELECT CASE Z
CASE IS > 0
PRINT "X is zero, Y is positive, Z is positive."
CASE IS < 0
PRINT "X is zero, Y is positive, Z is negative."
CASE ELSE
PRINT "X is zero, Y is positive, Z is zero."
END SELECT
CASE IS < 0
SELECT CASE Z
CASE IS > 0
PRINT "X is zero, Y is negative, Z is positive."
CASE IS < 0
PRINT "X is zero, Y is negative, Z is negative."
CASE ELSE
PRINT "X is zero, Y is negative, Z is zero."
END SELECT
CASE ELSE
SELECT CASE Z
CASE IS > 0
PRINT "X is zero, Y is zero, Z is positive."
CASE IS < 0
PRINT "X is zero, Y is zero, Z is negative."
CASE ELSE
PRINT "X is zero, Y is zero, Z is zero."
END SELECT
END SELECT
END SELECT
RETURN
An alternative is to use select case:
INPUT X
SELECT CASE X
CASE IS > 10
PRINT "X is greater than 10."
CASE IS < 10
PRINT "X is less than 10."
CASE ELSE
PRINT "X is equal to 10."
END SELECT
Related
I am just learning recursion and I am confused about the output of this recursive function.
int Run(int x)
{
if (x > 0)
cout << Run(x - 1) << " ";
return x;
}
If I call this function with Run(5), I get the output: 0 1 2 3 4
I expected to get the output: 0 1 2 3 4 5.
I'm confused as to why 5 is not returned at the end of the output. It's the value I plugged in to the function, so after all the recursive calls are made, shouldn't the function be returning that same value I plugged in?
Run does return 5, but the recursive calls to Run never do, and only the return of the recursive calls are printed.
Think about it like this: you aren't printing every x given to Run; you're only printing the return of the recursive calls. Your first call to Run that makes the recursive calls returns the 5 that you're expecting to be printed.
Basically, the 0 index means that your function will be executed n times, where n = the argument integer plus 1. the thing is the cout << Run(x - 1) << " "; will be executed n-1 times. This is because the first function call, once it executed all the other recursive calls, will return its value of x. but since it is not returning it to a previous recursive call which would then cout that value, it is not being displayed.
try this cout << Run(int x) on your main function.
We can see that Run(n) is a function that prints the non-negative whole numbers less than n and returns n. This means that for Run(n) you will get an output of 0 1 2 ... n-1 if n is positive.
What you have here:
cout << Run(x - 1) << " ";
is the same as
int r = Run(x - 1);
cout << r << " ";
That is, Run(x-1) will print the numbers less than x-1 and return x-1. So if x == 5 that means Run(x-1) printed the numbers from 0 to x-2 and r was set to 4. Then the function will print r and return. If you're still confused, it's also exactly the same as doing:
Run(x - 1);
cout << x-1 << " ";
Since we know that Run(x-1) will always return x-1 we can skip storing it in a variable and print it directly. A trick when analysing recursive functions is just to assume that they work - assume that Run(x-1) successfully prints the numbers from 0 to x-2 - the only thing left to do then is to print x-1.
So as you can see, the variable x never gets printed, only the numbers less than it. The function returns 5 but it just never prints it.
If you want it to print the non-negative whole numbers less than or equal to x, make this modification:
int Run(int x) {
if (x >= 0) {
cout << Run(x-1)+1 << " ";
}
return x;
}
The if statement has been changed to include 0 being printed now. Run(x-1) prints the numbers from 0 to x-1 and returns x-1, so adding 1 to the result allows us to print x.
Hello taking an online class on nested loops and this was provided as the example but I don't really know what is going on.
The following code example shows nesting for loops to output a chess or checkerboard representation using the characters X and O. Why do we need x and y variables to execute a certain amount of times. And what does alternate = !alternate; mean? About the x and y wouldn't it just do it 8 times total because its greater than the amount of times y supplies? what is the difference in purpose for the two for statements? Thank you.
for (int x = 0; x < 8; x++)
{
for (int y = 0; y < 4; y++)
{
if (alternate)
{
cout << "X ";
cout << "O ";
}
else
{
cout << "O ";
cout << "X ";
}
}
alternate = !alternate;
cout << endl;
}
The variable x used for the number of lines you want to print X-O pairs. Variable y used to specify the number of X-O pairs in 1 line. So for printing 8 lines of X-O pairs and in each line, 4 pairs of X-O, you should do just like that.
The operator '!' used for getting the opposite of a value (it's logical NOT) (for example, 1 to 0 or false to true). so alternate = !alternate; means that after every line of X-O pairs, it changes from true to false or vise versa.
So lines' first character (X or O) will change according to 'alternate' variable.
I am writing this program which will guess the number user is thinking about. After days of work, I could not figure out what is wrong in it.
Also my proposed grade for the assignment is not what I expected.
Please help.
User can guess 100, but my program uses mid-point rule so can only go up to 99. How can I make 100 inclusive?
If I keep pressing 'l' the program will eventually break out of loop and prints If you want to try again?
Is there a better way to code this program? Example please.
Here is the actual program:
Write a program in that can figure out a number chosen by a human user. The human user will think of a number between 1 and 100. The program will make guesses and the user will tell the program to guess higher or lower. The program should find the midpoint of the two numbers and ask if the number is higher or lower.
#include <iostream>
using namespace std;
int main() {
char check;
char tryagain;
do {
int midpoint;
const int MAX = 100;
const int MIN = 1;
int y = MAX;
int x = MIN;
cout << "Think of a number between 1 and 100." << endl;
midpoint = (x + y) / 2;
while (x != y || y != x) {
cout << endl << "Is it" << " " << midpoint << " " << "?";
cin >> check;
if (check == 'l' || check == 'L') {
y = midpoint;
midpoint = (x + y) / 2;
}
else if (check == 'h' || check == 'H') {
x = midpoint;
midpoint = (x + y) / 2;
}
else if (check == 'c' || check == 'C') {
break;
}
else {
cout << "Incorrect choice." << endl;
}
}
cout << "Great! Do you want to try again? (y/n)";
cout << endl;
cin >> tryagain;
} while (tryagain == 'y' || tryagain != 'n');
return 0;
}
Your problem is just a mis-think in the calculation of x and y like Alf suggested in the comments.
It should read
y = midpoint - 1;
and
x = midpoint + 1;
respectively. The reason is simple. You use midpoint as the guess. The guess is then no longer part of the available guesses. Your first guess is 50, x or y should then be either 51 or 49 as the new min or max in the interval.
This will also make 100 included in the available guesses. The last step in the calculation will be when midpoint was 99 and the user selects 'h'.
x = 99 + 1;
lower bound is 100, and the midpoint guess evaluates to
midpoint = (100 + 100) / 2;
which is correct.
As for better ways to write this program. This would depend on what your course has taught you, what's in the curriculum, and so on. You might want to check out code-review
When your x and y are too close, the division of their sum produces an incorrect midpoint. 100 + 99 / 2 = 99 (which is 99.5 rounded down). You need to check for this special case. At the end of the loop before the closing bracket insert:
if ( y-x < 2) midpoint = y;
User can guess 100, but my program uses mid-point rule so can only go up to 99. How can I make 100 inclusive?
Division of integers in c++ discards any decimal. It always rounds down. Consider what happens when midpoint is 99. You get midpoint = (99 + 100) / 2 which is 199 / 2 which is 99.5. Discarding the decimal leaves you with 99 every time. One possible solution is to change y = midpoint; to y = midpoint - 1; and x = midpoint; to x = midpoint + 1; This will prevent your application from guessing the same value more than once. With this change, when midpoint is 99, x will first be incremented to 100 giving us a new midpoint (100 + 100) / 2 which evaluates to 100.
If I keep pressing 'l' the program will eventually break out of loop and prints If you want to try again?
If the user keeps pressing l then eventually the only possible solution is 1. It seems that you chose not to propose your guess at that point and assume the user followed the rules. Add an extra print when the answer is deduced.
if (x == y) {
// Answer was deduced
cout << "You guessed " << x << ".\n";
}
Is there a better way to code this program? Example please.
See the first two parts of this answer. Other than that, it's difficult to say objectively what "better way to code this" means. You might want to consider a system to detect when the user is lying. For example, if midpoint == x then the user can't select l without lying.
How can I compare two cpp_int values like
boost::multipercision::uint256_t x = 100;
boost::multipercision::uint256_t y = 50;
if (x > y){
std::cout << "X is bigger than Y" <<endl;
} else {
std::cout << "Y is bigger than X" <<endl;
}
You have some syntax errors in your code.
boost::multiprecision is not spelled boost::multipercision.
Also, your if-else block should include a check for x == y, since in your code, it'll simply output Y is bigger than X when x and y are the same.
It just keeps looping. The numbers continue decreasing until the program is closed. Am I misusing something?
The playerHealth and orcHealth ints are 100.
randomNumber = ("%10d", 1 + (rand() % 100));
This was the way I seen the random number used on an srand() explanation page. If this is wrong, how should it be?
Are there any other problems here?
switch(charDecision)
{
case 1:
cout << "FIGHT" << endl;
do{
randomNumber = ("%10d", 1 + (rand() % 100));
if(randomNumber >= 50){
orcHealth = orcHealth - (randomNumber - (randomNumber / 5));
cout << "You hit the orc! He now has " << orcHealth << " life left!" << endl;
}
else
{
playerHealth = playerHealth - (randomNumber - (randomNumber / 5));
cout << "The orc hit you! You now have " << playerHealth << " life left!" << endl;
}
}while(playerHealth || orcHealth >= 0);
break;
default:
break;
}
playerHealth || orcHealth >= 0 does not mean "while playerhealth greater than zero OR orchealth greater than zero". It means "while playerhealth, cast to boolean is true OR orchealth greater than zero".
This
}while(playerHealth || orcHealth >= 0);
should probably be
}while(playerHealth > 0 && orcHealth > 0);
I think you want to exit the loop, if either one is 0 or less.
Also, change randomNumber = ("%10d", 1 + (rand() % 100)); to randomNumber = 1 + rand() % 100;
The comma operator just obfuscates the code.
Your condition for the do...while statement will stop at some point, but only at some point. This means, your condition will be met if either playerHealth is zero or orcHealth is smaller than zero. What if the playerHealth gets below zero? This is very likely since you are always deducting a number from both character's healths. A possibility of playerHealth becoming exactly zero is a very tiny one. And when the playerHealth goes below zero, its possibility of becoming zero is still very unlikely, even due to integer overflow. So, if you want to "kill" the character when one of their health becomes zero or less, you better change that line with something like
while ( playerHealth > 0 && orcHealth > 0 )
On a side note, the || statement works if any one of the statements is true. In C++, for an integer a 0 value is false, all other values -including negative ones- are treated as true. Also, the || checks from left to right and when it finds the first true statement, it stops searching. In your case, it checks playerHealth, which is very likely nonzero. When it sees this expression is true, it decides that the whole statement inside the parantheses is true and skips checking orcHealth >= 0. This results in the infinite loop. You might want to look at the order of evaluation of conditionals in C++, maybe something like this post.