
Concept explainers
(Correct the Code Errors) Identity and correct the errors (s) in each of the following:
- if (age >= 65); { cout << “Age is greater than or equal to 65” << end1;
- if (age >= 65) { cout << “Age is greater than or equal to 65” << end1;
- unsigned int x {1}; unsigned int total;
- While (x <=100) total +=x;
- while (y>0) { cout << y << end1;
}
else {
cout << “Age is less than 65 << and end1” ;
}
else: {
cout << “Age is less than 65<< end1”;
}
while (x <=10) {
total += x;
++x;
}
++x;
++y;
}

a)
To find and correct errors in the given program segment.
Explanation of Solution
The given program segment contains errors in the following highlighted statements:
if(age>=65); { cout<<"Age is greater than are equal to 65"<<endl; } else { cout<<"Age is less than 65<<endl"; }
Errors:
- If the statement is ending with a semi-colon.
- The keyword endl is enclosed within double-quotes. Double quotes should only contain string statements.
Correct code:
if(age>=65){ cout<<"Age is greater than are equal to 65"<<endl; } else { cout<<"Age is less than 65"<<endl; }

b)
To find and correct errors in the given program segment.
Explanation of Solution
The given program segment contains errors in the following highlighted statements:
if(age>=65){ cout<<"Age is greater than are equal to 65"<<endl; } else; { cout<<"Age is less than 65 <<endl"; }
Errors:
- Else statement is ending with a semi-colon.
- The keyword endl is enclosed within double-quotes. Double quotes should only contain string statements.
Correct code:
if(age>=65){ cout<<"Age is greater than are equal to 65"<<endl; } else { cout<<"Age is less than 65"<<endl; }

c)
To find and correct errors in the given program segment.
Explanation of Solution
The given program segment contains errors in the following highlighted statements:
unsigned int x{1}; unsigned int total; while(x<=10){ total + = x; ++x; }
Errors:
- Incorrect use of assignment operator (=).
Correct code:
unsigned int x{1}; unsigned int total; while(x<=10){ total =+ x; ++x; }

d)
To find and correct errors in the given program segment.
Explanation of Solution
The given program segment contains errors in the following highlighted statements:
if(age>=65); { cout<<"Age is greater than are equal to 65"<<endl; } else { cout<<"Age is less than 65<<endl"; }
Errors:
- If the statement is ending with a semi-colon.
- The keyword endl is enclosed within double-quotes. Double quotes should only contain string statements.
Correct code:
if(age>=65){ cout<<"Age is greater than are equal to 65"<<endl; } else { cout<<"Age is less than 65"<<endl; }

e)
To find and correct errors in the given program segment.
Explanation of Solution
The given program segment contains errors in the following highlighted statements:
while(y>0){ cout<<y<<endl; ++y; }
Errors:
- The code segment contains a logical error. The while loop is an infinite loop since it will keep running since y is always incremented and will always remain positive.
Correct code:
while(y>0){ cout<<y<<endl; --y; }
Want to see more full solutions like this?
Chapter 4 Solutions
C++ How to Program (Early Objects Version)
- 6. See the code below and solve the following. import java.io.*; public class DataStream { } public static void main(String[] args) } DataOutputStream output = new DataOutputStream(new FileOutputStream("temp.dat")); output.writeUTF("Book1"); output.writeInt(85); output.writeUTF("Book2"); output.writeInt(125); output.writeUTF("Book3"); output.writeInt(70); output.close(); // ToDo: Read all data from temp.dat and print the data to the standard output (monitor) 6-1. This program has a compile error, and the message is “Unhandled exception type FileNotFoundException". How do you fix this error? (1 point) 6-2. Is FileNotFoundException a checked exception or an unchecked exception? (1 point) 6-3. What is the difference between checked exception and unchecked exception? (1 point) 6-4. Please complete the above program by reading all data from temp.dat and print the data to the standard output (monitor) by using System.out.print, System.out.println or System.out.printf method. (2 points)arrow_forwardWrite a program that reads a list of integers from input and determines if the list is a palindrome (values are identical from first to last and last to first). The input begins with an integer indicating the length of the list that follows. Assume the list will contain a maximum of 20 integers. Output "yes" if the list is a palindrome and "no" otherwise. The output ends with a newline. Hints: - use a for loop to populate the array based on the specified size (the first number entered) - use a for loop to check first value with last value, second value with second from end, etc. - if the values do not match, set a Boolean variable to flag which statement to output (yes or no) Ex: If the input is (remember to include spaces between the numbers): 6 1 5 9 9 5 1 the output is: yes Ex: If the input is: 5 1 2 3 4 5 the output is: C++ codingarrow_forwardDesign and draw a high-level "as-is" process diagram that illustrates a current process related to a product or service offered through the SSDCI.gov database.arrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,Np Ms Office 365/Excel 2016 I NtermedComputer ScienceISBN:9781337508841Author:CareyPublisher:CengageProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage



