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 (10th Edition)
- Find errors / syntax error. Write line numberarrow_forwardint x1 = 66; int y1 = 39; int d; _asm { } mov EAX, X1; mov EBX, y1; push EAX; push EBX; pop ECX mov d, ECX; What is d in decimal format?arrow_forwardLook at the following code. int x = 7; int *ptr = &x;What will be displayed if you send the expression *iptr to cout? What happens if you send the expression ptr to cout?arrow_forward
- void funOne(int a, int& b, char v); void main() { int num1=10; char ch='A'; funOne(num1, 15,ch)<arrow_forwardlint power (int base, int exponent); Define the recursive function power() that when invoked returns baseonent Assume that exponent is an integer greater than or equal to 1. Hint: The recursion step would use the relationship basenent = base * baseonert- and the terminating condition occurs when exponent is equal to 1 because base' = base or when exponent is equal to o because base = 1.arrow_forward#include void main () { int a=5,b=10,c=1; if (a && b>c) { printf("cquestionbank"); } else{ break; } }arrow_forwardC# (Odd or Even) Write an app that reads an integer, then determines and displays whether it’s odd or even. (Hints: use the remainder operator) (Multiples) Write an app that reads two integers, determines whether the first is a multiple of the second, and displays the result. (Hints: use the remainder operator). Create a class called Date that includes three pieces of information as auto-implemented properties: a month (type int), a day (type int), and a year (type int). Your class should have a constructor that initializes the three fields. Provide a method DisplayDate() that displays the three fields (separated by '/'). Write a test app named DateDemo that demonstrates class Date’s capabilities.arrow_forward#include using namespace std; int main(){ float age; int subscriptions; cin>>age; switch (age/10) { case 1: cout<arrow_forwardComplete C++ source code for Write a complete C++ program to help the manufacturer determine the greatest profit obtainfrom making tables and chairs with a given number of M type A and N type B wood blocks.• The program should prompt the user to input M and N.• Then tabulates all the combinations of tables and chairs possibly made, and calculatesthe profit obtain using array.• The program should also identify the greatest profit and make a concluding remark.• The program should be written in modular form with the main() function calling otherfunctions. If arrays are involved in the function, the arrays should be passed into thefunction as parameters.• Lastly, display the output to an external .txt file.arrow_forwardarrow_back_iosSEE MORE QUESTIONSarrow_forward_ios
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education