MYPROGRAMMINGLAB WITH PEARSON ETEXT
MYPROGRAMMINGLAB WITH PEARSON ETEXT
8th Edition
ISBN: 9780134225340
Author: Deitel
Publisher: PEARSON
bartleby

Concept explainers

bartleby

Videos

Textbook Question
Book Icon
Chapter 4, Problem 4.5E

Find the error in each of the following. (Note: There may be more than one error.)

  1. Chapter 4, Problem 4.5E, Find the error in each of the following. (Note: There may be more than one error.) The following , example  1
  2. The following code should print whether a given integer is odd or even:
  3. Chapter 4, Problem 4.5E, Find the error in each of the following. (Note: There may be more than one error.) The following , example  2

  4. The following code should input an integer and a character and print them. Assume the usere types as input 100 A.
  5. Chapter 4, Problem 4.5E, Find the error in each of the following. (Note: There may be more than one error.) The following , example  3

    Chapter 4, Problem 4.5E, Find the error in each of the following. (Note: There may be more than one error.) The following , example  4

  6. The following code should output the odd integers from 999 to 1:
  7. Chapter 4, Problem 4.5E, Find the error in each of the following. (Note: There may be more than one error.) The following , example  5

  8. The following code should output the even integers from 2 to 100:
  9. Chapter 4, Problem 4.5E, Find the error in each of the following. (Note: There may be more than one error.) The following , example  6

  10. The following code should sum the integers from 100 to 150 (assume total is initialized to 0):
  11. Chapter 4, Problem 4.5E, Find the error in each of the following. (Note: There may be more than one error.) The following , example  7

(a)

Expert Solution
Check Mark
Program Plan Intro

To find and debug the error using proper syntax and logic in the given program.

Program Description Answer
for (int x = 100; x >= 1;--x) {
printf ("%d\n", x);
 }

Explanation of Solution

Given information:

  • For (x = 100, x >= 1, ++x) {
  • printf ("%d%n", x);
  • }

Explanation:

  1. Loop variable needs to be initialized as an intthat is int x.
  2. The syntax of a for loop is not followed as well as C is the case-sensitive language that is For and for are the different for C compiler.
  3. Therefore, For should be written as for and the commas need to be changed into the semi-colons (;) inside the for-loop parenthesis.
  4. The correct syntax of a for loop is:
  5. for (Initializing variable; Testing condition; Updating variable){

    //for block to write the statements

    }

  6. The logical error wasthat loop going to execute the infinite times as it’s variablestarts from 100 and is updating its value by incrementing 1 at every run. Therefore, at every update, the value of x is greater than 1. Thus, instead of updating x as ++x use -- x.
  7. The last error is that to print every integer in newline \n is used instead of %n .

Output:

MYPROGRAMMINGLAB WITH PEARSON ETEXT, Chapter 4, Problem 4.5E , additional homework tip  1

(b)

Expert Solution
Check Mark
Program Plan Intro

To find and debug the error using proper syntax and logic in the given program to print whether the given integer is odd or even.

Program Description Answer
switch (value % 2)
{
     case 0:
puts ("Even integer");
	 break;
     case 1:
puts ("Odd integer");
	 break;
}

Explanation of Solution

Given information:

switch (value % 2)
{
     case 0:
puts ("Even integer");
     case 1:
puts ("Odd integer");
}

Explanation:

The cases act as the start point for every switch and case block where the switch takes the data and matches it with the corresponding case. Therefore, if any case is matched then all the statements from that case will be executed till it terminates or breaks. Therefore, cases should have a break keyword to exit the switch and case block, once the case is matched.

(c)

Expert Solution
Check Mark
Program Plan Intro

To find and debug the error using proper syntax and logic in the given program to print an integer value and the character value A as 100 A.

Program Description Answer

scanf ("%d", &intVal);

scanf ("\n%c", &charVal);

printf ("Integer: %d\nCharacter: %c\n", intVal, charVal);

Explanation of Solution

Given information:

scanf("%d", &intVal);

charVal = getchar ();

printf("Integer: %d\nCharacter: %c\n", intVal, charVal);

Explanation:

To stop reading the variable charValblank character, the second statement need to skip the preceding blanks when the user enters the integer value and press return, to rectify this scanf must be used. That is getchar() reads the next line after pressing by inputing the integer value.

Output:

MYPROGRAMMINGLAB WITH PEARSON ETEXT, Chapter 4, Problem 4.5E , additional homework tip  2

(d)

Expert Solution
Check Mark
Program Plan Intro

To find and debug the error using proper syntax and logic in the given program

Explanation of Solution

Given information:

for (x = .000001; x == .0001; x += .000001)

printf("%.7f\n", x);

Explanation:

The numbers are quite small to be compared and the for-loop must not compare the floating-point number using == as it causes impression which might cause the infinite loop. Thus, it is recommended to use integer values as int datatype in the for-loop.

(e)

Expert Solution
Check Mark
Program Plan Intro

To find and debug the error using proper syntax and logic in the given program to print odd number from 999 to 1.

Program Description Answer
for (int x = 999; x >= 1; x -= 2)
        if (x % 2 == 1)
printf ("%d\n", x);

Explanation of Solution

Given information:

for(x = 999; x >= 1; x += 2)

printf("%d\n", x);

Explanation:

  1. Loop variable need to be initialized as intthat is int x.
  2. To print the odd values from 999 to 1 the loop variable should decrease by two, instead of increasing by 2 as we need to decrease the value.
  3. The correct syntax of a for loop is:
  4. for (Initializing variable; Testing condition; Updating variable) {

    //for block to write a statement

    }

  5. If condition is used to check whether the variable x is odd or not.

Output:

MYPROGRAMMINGLAB WITH PEARSON ETEXT, Chapter 4, Problem 4.5E , additional homework tip  3

(f)

Expert Solution
Check Mark
Program Plan Intro

To find and debug the error using proper syntax and logic in the given program to print even numbers between 2 to 100.

Program Description Answer

int counter = 2;

do{

if(counter % 2== 0)

printf("%u\n", counter);

counter += 2;

} while(counter <= 100);

Explanation of Solution

Given information:

counter = 2;

Do {

if (counter % 2== 0) {

printf ("%u\n", counter);

}

counter += 2;

} While (counter < 100);

Explanation:

  1. The variable need to be initialized as intthat is int counter.
  2. The programming language - C is the case-sensitive language that is Do and do are different for C compiler and the same goes for While and while.
  3. To print the even values to 100 inclusively whileloop needs to <= instead of <.

Output:

MYPROGRAMMINGLAB WITH PEARSON ETEXT, Chapter 4, Problem 4.5E , additional homework tip  4

(g)

Expert Solution
Check Mark
Program Plan Intro

To find and debug the error using proper syntax and logic in the given programto find the sum of the numbers between 100 to 150.

Program Description Answer
int total = 0;
    for(int x = 100; x <= 150; ++x){
        total += x;
    }
printf("%d",total);

Explanation of Solution

Given information:

for(x = 100; x <= 150; ++x);{

total += x;

}

Explanation:

  1. The declaration of the total variable is missing.
  2. Loop variable needs to be initialized as an intthat is int x.
  3. The syntax of a for loop is not followed as there should be no comma (,) after the parenthesis.
  4. The correct syntax of a for loop is:
  5. for (Initializing variable; Testing condition; Updating variable) {

    //for block to write statement

    }

  6. The logical error was that there is no print statement.

Output:

MYPROGRAMMINGLAB WITH PEARSON ETEXT, Chapter 4, Problem 4.5E , additional homework tip  5

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
This battle room is focused on entry level tasks for a network analyst where you will be given trials and reconnaissance, sensor tuning, log aggregation, SIEM queries, and network analysis.   For this week’s project, complete the following tasks:   From your Project Ares portal, LOG IN Click on LAUNCH GAME. Select the region NORTH AMERICA Click on Battle School Under the BATTLE SCHOOL pop-up window, click on START TRAINING. Under the BATTLE ROOMS tile, click on ENTER. Under the NETWORK ANALYST tile, click on PLAY. Wait for the Battle Room to load. While loading, the BATTLE ROOM button will display red. Once the Battle Room is loaded, the BATTLE ROOM button will turn yellow and the center of the disk display will indicate CONNECTED. Click on the BATTLE ROOM button to enter the Battle Room. Below the TASKS folder, make sure you click on INSTRUCTIONS to download the Network Analyst Fundamentals material. In the Battle Room, under the TASKS menu select task INTRUSION DETECTION. Complete…
Create a relationship between the common field (Technician Number) of the two tables. Make sure that each client must have 1 and only 1 technician assigned, and each technician can have multiple clients. 2. Create a query to show the Client Number, Client Name, Billed, Paid for clients in Anderson city. Save the query. 3. Create a query to show the Technician Number, Last Name, First Name, YTD Earnings for technicians whose Hourly Rate is greater than or equal to 30. Save the query. 4. Create a query to show Client Number, Client Name, Billed, Paid for clients whose technician number is 22 and whose Billed is over 300. Save the query. 5. Create a query to show the Technician Number, Last Name, First Name, Client Number, Client Name for clients whose technician number 23. Save the query. 6. Create a query to show the Technician Number, Last Name, First Name, Client Number, Client Name for clients whose technician number 23 or 29. Save the query Help please Microsoft office access
Dijkstra's Algorithm (part 1).  Consider the network shown below, and Dijkstra’s link-state algorithm. Here, we are interested in computing the least cost path from node E (note: the start node here is E) to all other nodes using Dijkstra's algorithm. Using the algorithm statement used in the textbook and its visual representation, complete the "Step 0" row in the table below showing the link state algorithm’s execution by matching the table entries (i), (ii), (iii), and (iv) with their values.  Write down your final [correct] answer, as you‘ll need it for the next question.

Chapter 4 Solutions

MYPROGRAMMINGLAB WITH PEARSON ETEXT

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Why is the CPU the most important component in a computer?

Starting Out with Programming Logic and Design (5th Edition) (What's New in Computer Science)

Car Instrument Simulator For this assignment, you will design a set of classes that work together to simulate a...

Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Text book image
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
Computer Science
ISBN:9780357392676
Author:FREUND, Steven
Publisher:CENGAGE L
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Literals in Java Programming; Author: Sudhakar Atchala;https://www.youtube.com/watch?v=PuEU4S4B7JQ;License: Standard YouTube License, CC-BY
Type of literals in Python | Python Tutorial -6; Author: Lovejot Bhardwaj;https://www.youtube.com/watch?v=bwer3E9hj8Q;License: Standard Youtube License