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
C How to Program (8th Edition)
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
- Write C code that calculates and prints the volumes of different shapes (Sphere and Frustumarrow_forwardIdentify and fix the errors in the following code segment.arrow_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_forwardUSE C++ (Display matrix of 0s and 1s) Write a function that displays an n-by-n matrix using the following header: void printMatrix(int n) Each element is 0 or 1, which is generated randomly. Write a test program that prompts the user to enter n and displays an n-by-n matrix. Here is a sample run:arrow_forwardComputer science Cs 102arrow_forward
- (Q1)This is a Data Structures problem and the programming language used is Lisp. Solve the question we detailed steps and make it concise and easy to understand. Please and thank you.arrow_forward*Please help in javascript* Summary: Given integer values for red, green, and blue, subtract the gray from each value. Computers represent color by combining the sub-colors red, green, and blue (rgb). Each sub-color's value can range from 0 to 255. Thus (255, 0, 0) is bright red, (130, 0, 130) is a medium purple, (0, 0, 0) is black, (255, 255, 255) is white, and (40, 40, 40) is a dark gray. (130, 50, 130) is a faded purple, due to the (50, 50, 50) gray part. (In other words, equal amounts of red, green, blue yield gray). Given values for red, green, and blue, remove the gray part. Ex: If the input is: 130 50 130 the output is: 80 0 80 import java.util.Scanner; public class LabProgram {public static void main(String[] args) {/* Type your code here. */}}arrow_forwardplease help on this python code. Fill in the #lines and what is asking. Write a new code please.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,