Question

Identify and correct the errors in each of the following [Note: There may be more than...

Identify and correct the errors in each of the following [Note: There may be more than one error in each piece of code]:

a) if ( age >= 65 );  

printf( "Age is greater than or equal to 65\n" );

else

printf( "Age is less than 65\n" );

b) int x = 1, total;

    while ( x <= 10 )

               {

              total += x;

                ++x;

           }

c) While ( x <= 100 )

total += x;

++x;

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Correct code with explanation and error is given below for part a,b and c:

Answer a)

Error:

1. (age variable is undeclared and undefined)

2. ";" after if condition

Correct Code:

#include <stdio.h>

int main()
{
//age variable is undeclared and undefined
//declared age
int age=23;
  
//there should not be ; after the condition of if statement
if ( age >= 65 )
printf( "Age is greater than or equal to 65\n" );
else
printf( "Age is less than 65\n" );
return 0;
}

------------------------------------------------------------------------------------------------------------------

Screenshot of code executed successfully and its output:

C (Goc 6.3) Hinclude <stdio.h> 1 2 3 4- 5 6 7 int main() //age variable is undeclared and undefined //declared age int age-23'

Answer b) There is no error in the given piece of code for part b)

#include <stdio.h>

int main()
{
int x = 1, total;
while ( x <= 10 ){
total += x;
++x;
}
}

--------------------------------------------------------

Screenshot of code executed successfully:

C (Goc 6.3) 1 #include <stdio.h> 1 2 3 4- 5 int main() int x = 1, total; while ( x <= 10 ){ total = x; 7 8 9 10 } 9:1 Open Fi

Answer c)

Error:

1. variable x and total is not declared and defined
2. W should be small in while statement.
3. There should be open and close bracket {} because there is more than one statement in while loop

Correct Code:

#include <stdio.h>

int main()
{
//variable x and total is not declared and defined
int x = 1, total;
  
//W should be small in while statement
//there should be open and close bracket {}
//since there is more than one statement in while loop
while ( x <= 10 ) {
total += x;
++x;
}
}

--------------------------------------------------------------------------------------------------

Screenshot of code executed successfully and its output:

C (Goc 6.3) Hinclude <stdio.h> 1 2. 3 int main() //variable x and total is not declared and defined int x = 1, total; 5 6 7 8

Add a comment
Know the answer?
Add Answer to:
Identify and correct the errors in each of the following [Note: There may be more than...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Which of the following statements are true? Note that there may be more than one correct...

    Which of the following statements are true? Note that there may be more than one correct answer; select all that are true. In order for the inference procedures for proportions to be valid, the population from which the random sample is drawn must be normally distributed. As the desired margin of error decreases, then the required minimum sample size will increase. When determining the minimum sample size, if the value of p^ is not given, we should use the "worst-case...

  • /* •   The following code consists of 5 parts. •   Find the errors for each part...

    /* •   The following code consists of 5 parts. •   Find the errors for each part and fix them. •   Explain the errors using comments on top of each part. */ #include <stdio.h> #include <string.h> int main( ) { ////////////////////////////////////////////////////////////////////////// //////////////        Part A. (5 points)                ////////////////// int g(void) { printf("%s", Inside function g\ n " ); int h(void) {       printf(" % s ", Inside function h\ n "); } } printf("Part A: \n "); g(); printf(" \n"); ////////////////////////////////////////////////////////////////////////// //////////////       ...

  • Debugging: The code below has six errors. The errors may be syntax errors or logic errors,...

    Debugging: The code below has six errors. The errors may be syntax errors or logic errors, and there may be more than one per line; examine the code carefully to find them. Indicate each of the errors you find by writing the line number and correction in the space provided below. This program is designed to take a constant number of scores (3 in this case) and store them into an array. Then the array is passed to a function...

  • answer two question,each question has 3syntax errors,find it. 1(a) The following segment of code contains at...

    answer two question,each question has 3syntax errors,find it. 1(a) The following segment of code contains at least three (3) syntax errors that will compilation to fail with errors. Identify each of these errors and specify the solution. [2 marks each, 6 marks in total] cause #include <stdio.h> void main (void) float height; float area; printf "input height of the rectangle: ") scanf( "%f", &height) printf"input length of the rectangle: scanf"", &length); height length; area printf( area of rectangle %f high...

  • Consider the following pseudo code. What are the possible outputs (.e., the complete output from each...

    Consider the following pseudo code. What are the possible outputs (.e., the complete output from each execution run of this program)? Explain in less than 50 words. Note: keep in mind that fork) may fail in that case no child process is created int main(int arge, char argvt int X- int CwO; re - fork(); 15 tre > 0) X=S; else x-rc +2: printf(" \n", x); return 0;

  • 31. The following code segment is syntactically correct:              int number{20};          cout << number <<...

    31. The following code segment is syntactically correct:              int number{20};          cout << number << setbase(16) << " " << number                               << setbase(10) << " " << number                                 << showpos << " " << number << endl;                 T__   F__       32. The following statement wants to determine if ‘count’ is outside the     range of 0 through 100:             if (count < 0 && count > 100)                                             T__   F__ 33. There is...

  • 14. Given the following declaration char x = -11; char y = 63; if the computer...

    14. Given the following declaration char x = -11; char y = 63; if the computer uses 2’s complement to represent the numbers then which statement is correct: a. The number of bits in x is equal to the number of bits in y b. The number of bits in x is less than the number of bits in y c. The number of bits in x is greater than the number of bits in y d. None of the...

  • 3. The short program below has one or more syntax, runtime, or semantic errors. Identify the...

    3. The short program below has one or more syntax, runtime, or semantic errors. Identify the errors and correct them. The number of errors to locate is identified. (Total = 8 pts) Consider a sequence of non-negative integers, where each number is written in a separate line. The sequence ends with 0. Find how many elements of this sequence are equal to its largest element. (4 errors) EXAMPLE LO: Input: Input: Output: Output: while True: a = input () if...

  • What will be the output of the following code int x = 5; int y =...

    What will be the output of the following code int x = 5; int y = 2; f(x,y); printf(“%s \n”, (x/y > 2) ? “x is more than two times larger than y” : “x is less than or equal to 2*y\n ”); f(int x, int y) { x = 2*y; printf(“%s \n”, (x/y > 2) ? “x is more than two times larger than y” : “x is less than or equal to 2*y \n”); } a. x is...

  • S. The following code has some errors Please point out and correct them. (There are more...

    S. The following code has some errors Please point out and correct them. (There are more than five errors #ineludo <iostream> uaing std reout uning stdsiendi class Time public Time (int-o, int0) void printTime) const void setMinute(int m) const private int hour int minute Time: :Time (int h, int m) hour (h o & hour< 24) h 0 setMinute (m) Time::setMinute (int m) minute = (m >-O && m < 60) ? m : 0; void printTime () cout <...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT