Indentify and correct the errors in each of the following. [Note: There may be more than one error in each piece of code.]
a. To identify and correct the error in the given codes.
Explanation of Solution
The error is in the first line of the code. The “if” statement shouldn’t end with a semicolon.
The correction involves the removal of the statement terminator (;) at the end of the first line of code because if there will be semicolon the puts statements won’t get executed.
The correct code will be −
if(age>=65) puts("Age is greater than or equal to 65\n"); else puts("Age is less than 65\n");
b. To identify and correct the error in the given codes.
Explanation of Solution
There is as such no error with the syntax or logical of the given code segment.However, it is always recommended to initialize the variables with some initial values in order to avoid problem of garbage value. Therefore, the variable “total” should be initialized with 0.
The correct code
int x=1, total =0; while(x<=10) { total+=x; ++x; }
c. To identify and correct the error in the given codes.
Explanation of Solution
There are two errors in the given code.
The first correction involves the correction of name of the keyword while from “While” to “while”.
The second correction involves the enclosing the second and third statement withincurly braces.
So, the correct code will be as follows-
while(x<=100) { total+=x; ++x; }
d. To identify and correct the error in the given codes.
Explanation of Solution
The given code has the error in the while statement. The given statement is an infinite loop because y will keep on increasing (provided base value of y is positive) and will always be greater than 0.
The above problem can be solved by decrementing the y-value instead of incrementing.
The correct code will be as follows-
while(y>0) { printf("%d\n",y); --y; }
Want to see more full solutions like this?
Chapter 3 Solutions
MYPROGRAMMINGLAB WITH PEARSON ETEXT
Additional Engineering Textbook Solutions
Starting Out with Programming Logic and Design (4th Edition)
Starting Out with C++ from Control Structures to Objects (9th Edition)
C Programming Language
Absolute Java (6th Edition)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Starting Out With Visual Basic (8th Edition)
- Find the error(s) in the following code: (6)arrow_forward(Numerical) Using the srand() and rand() C++ library functions, fill an array of 1000 floating-point numbers with random numbers that have been scaled to the range 1 to 100. Then determine and display the number of random numbers having values between 1 and 50 and the number having values greater than 50. What do you expect the output counts to be?arrow_forward[Python (py3)] The error in the code below is if the number of rows of the matrix is not equal to its number of columns, matrix addition will not be performed. This should not be the case since the only requirement for matrices addition is that the dimension of Matrix A is equal to the dimension of Matrix B. Please resolve the error in the code below such that Matrix A and Matrix B can be added if the dimension of Matrix A is equal to the dimension of Matrix B. PLEASE do not just copy the code below and use it as the answer itself. I've encountered such case many times. Please modify the code. When the dimension of Matrix A is not equal to the dimension of Matrix B, print "Matrix addition cannot be performed; dimensions are unequal." The input will come from file1.txt, and the output should only be printed to output.txt Format of the input from file1.txt:First Line: type of operation (add)Second Line: matrix A dimension (example: if 3 rows and 2 columns, type 3 2)Third Line: matrix A…arrow_forward
- DO NOT USE EXISTING ANSWERS ON CHEGG OR COURSE HERO OR ANY OTHER SERVICES PLEASE! Thanks :) CODE IN PYTHON AND SHOW COMMENTS TO EXPLAIN CODE A confused Dutchman trying to speak English could say “I am in the war”, even though there is no hostile activity going on. The confusion1 here is that the English sentence “I am confused” is translated in Dutch as “Ik ben in de war”, which is phonetically (“sounding”) quite close to the first sentence. Such confusion leads to much enjoyment, but can complicate matters a bit. Given a sentence in Dutch and a dictionary containing both correct translations as well as phonetic (incorrect) translations of individual words, find the translation of the sentence and indicate whether it is correct, or in case there is more than one find the total number of correct and incorrect translations. A sentence is correctly translated when each word of the sentence is correctly translated. Input The input consists of: One line with an integer n (1≤n≤20), the…arrow_forward(Display patterns using loops) Use nested loops that display the following patterns in two separateprograms: pattern A Pattern B Pattern c 123456 1 3 1 6 1 2 3 3 3 1 6 1 2 3 4 5 3 3 3 1 6 1 2 3 4 5 6 7 3 3 1 6 1 2 3 4 5 6 7 8 9 3 123456arrow_forward(Display three patterns using loops) Use nested loops that display the following patterns in three separateprograms:arrow_forward
- vdfffarrow_forward(True/False): A segment descriptor does not contain segment size informationarrow_forward(Financial: credit card number validation) Credit card numbers follow certain pat- terns. A credit card number must have between 13 and 16 digits. It must start with: 4 for Visa cards 5 for Master cards 37 for American Express cards 6 for Discover cards In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or whether a credit card is scanned correctly by a scanner. Credit card numbers are generated following this validity check, commonly known as the Luhn check or the Mod 10 check, which can be described as follows (for illustra- tion, consider the card number 4388576018402626): 1. Double every second digit from right to left. If doubling of a digit results in a two-digit number, add up the two digits to get a single-digit number. 4388576018402626 → 2 * 2 = 4 → 2 * 2 = 4 → 4 * 2 = 8 → 1 * 2 = 2 6 * 2 = 12 (1+ 2 = 3) → 5 * 2 = 10 (1+ 0 = 1) → 8 * 2 = 16 (1 + 6 = 7) → 4 * 2 = 8arrow_forward
- Which of the following arithmetic operations may be performed on struct variables but not on array variables?arrow_forwardex program for thisarrow_forwardالمزيد ... التقويم :what is the value of x according to the calculation .4 x = (4 + 2 * 3) + (16/4 + 1) (3 + 8)/4 * (2 نقطة) Apply rules of precedence and show your work أدخل إجابتك النشارarrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,