1. How can we compare strings in our programs? Provide an example C++ code. 2. A
ID: 3629869 • Letter: 1
Question
1. How can we compare strings in our programs? Provide an example C++ code.2. Although C++ has a stand-alone logical data type, it might treat integers as logical values. The rule is: if the value is zero, it is false otherwise, it is true. Provide an example of such use and comment on whether it is a recommended programming practice or not.
3. Can we compare floating point values for exact equality? Justify your answer AND provide an example code.
4. Short circuiting can have advantages and disadvantages. Discuses this phrase in details. Use small code examples to explain your discussion.
5. Describe the logic flow of the switch statement. How can we make it the same as the logic of the if-else-if construct?
Explanation / Answer
Question 1: 1)Include the string library in your program. This library contains a lot of useful functions used when working with strings, including one that compares them. Place the following code at the top of the program, with the other includes. include 2)Declare the string variables. Those will be the strings you will compare. Note that strings in C are declared as arrays of characters, not as a special string type. char string1[limit], string2[limit]; // Replace "limit" with the maximum length of the strings 3)Decide how many characters you wish to compare. The more you compare, the higher precision you will achieve (For example, if the strings were "Hell" and "Hello" and you compared four characters, they would appear the same). If you compare a lot of characters, the comparison will be slower. There is no need to make this number greater than the strings' lengths (there are no characters to compare after their end). 4)Create a variable that contains the number of characters to compare. int compareLimit = 100; // Here, the limit is 100. However, you may change it to satisfy your needs. 5)Initialize the strings. You may assign the value manually, read it from the input, read it from a file... 6)Compare the strings. The comparison is done using the strncmp() function, and the returned type is an integer. int result = strncmp(string1, string2, compareLimit); 7)Examine the result. If it is positive, the first string is larger. If it is negative, the second is larger. If it is 0, they are equal. if(result > 0) printf("string1 > string2"); else if(result < 0) printf("string2 > string1"); else printf("string1 == string2"); Question 5: Switch Case Statement The switch case statement, also called a case statement is a multi-way branch with several choices. A switch is easier to implement than a series of if/else statements. The switch statement begins with a keyword, followed by an expression that equates to a no long integral value. Following the controlling expression is a code block that contains zero or more labeled cases. Each label must equate to an integer constant and each must be unique. When the switch statement executes, it compares the value of the controlling expression to the values of each case label. The program will select the value of the case label that equals the value of the controlling expression and branch down that path to the end of the code block. If none of the case label values match, then none of the codes within the switch statement code block will be executed. Java includes a default label to use in cases where there are no matches. We can have a nested switch within a case block of an outer switch. Its general form is as follows: switch () { case label1: case label2: … case labeln: default: } // end switch When executing a switch statement, the program falls through to the next case. Therefore, if you want to exit in the middle of the switch statement code block, you must insert a break statement, which causes the program to continue executing after the current code block. Below is a java example that demonstrates conditional execution based on nested if else statement condition to find the greatest of 3 numbers. public class SwitchCaseStatementDemo { public static void main(String[] args) { int a = 10, b = 20, c = 30; int status = -1; if (a > b && a > c) { status = 1; } else if (b > c) { status = 2; } else { status = 3; } switch (status) { case 1: System.out.println("a is the greatest"); break; case 2: System.out.println("b is the greatest"); break; case 3: System.out.println("c is the greatest"); break; default: System.out.println("Cannot be determined"); } } } Output c is the greatestRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.