Question

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 and the average score is returned.

The expected output should be:

-bash-4.1$ ./exam1_debug

Enter score #1:

100

Enter score #2:

91

Enter score #3:

80

The average score was: 90.3333

You must find and correct all six of the errors.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

#include <iostream>

using namespace std;

const NUM_SCORES = 3;

double average(int scores);

int main () {

int scores[NUM_SCORES];

for(i = 0; i < NUM_SCORES;i++){

    cout << "Enter score #" << i+1 << ":" << endl;

    cin >> scores[i+1];

}

cout << "The average score was: " << scores << endl;

return 0;

}

double average(int scores[]){

int total = 0;

for(int i = 0;i < NUM_SCORES;i++){

    total += scores[i];

}

return (total/NUM_SCORES);

}

Line Number

Correction

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

Dear Student,

Here is the complete correct program....along with the comment. I have specified the error as well as corrected it.

NOTE: Please note that the following program is tested on ubuntu 14.04 system and compiled under g++ compiler....

When you run the above given program then you will get the error messages in each line as indicated below...

Here is the screen shot of the error message.....

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

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

here is the complete correct program....

Program:

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

#include <iostream>

using namespace std;


//Error 1: Please specify the right data type.
//Here you have intialized the value as const. So value is not goin to change throughtout thr program.

int NUM_SCORES = 3;

//Error 2: Variable i data type is not declared

int i;

double score;

double average(int scores[]);

int main ()

{

int scores[NUM_SCORES];

for(i = 0; i < NUM_SCORES;i++)

{

cout << "Enter score #" << i+1 << ":" << endl;

//Error: 7 you are entering a i+1 value while the for loop start with i = 0;

cin >> scores[i];

}

//Error: 3 avergae calculating function must be called here;

score = average(scores);

//Error: 4 Here we are are displaying scores array that is not a valid operation
//cout << "The average score was: " << scores << endl;

//Correct Statement:

cout << "The average score was: " <<score<<endl;

return 0;

}

double average(int scores[])
{

//Data type Declaration of total, total should be of type double.

double total = 0;

for(int i = 0;i < NUM_SCORES;i++)
{

    total += scores[i];

}

//Error:5 average calculation should be done here then calculate the average

cout<<total<<endl;

double Avg = total/NUM_SCORES;

cout<<Avg<<endl;

//Error:6 only variable can be return here we are returning an expression

return Avg;

}


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

Here i have attached the output of the program as a screen shot..

0utput:

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

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

Now you can make a table of .with line number and correction required...

Kindly Check and Verify Thanks...!!!

Add a comment
Know the answer?
Add Answer to:
Debugging: The code below has six errors. The errors may be syntax errors or logic errors,...
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
  • C++ question The code below has five errors.  The errors may be syntax errors or logic errors,...

    C++ question The code below has five 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. You must find and correct all five of the errors. If there were no errors, this code would output: Paycheck for 123-45-678 : $690.625 Paycheck for 876-54-321 : $818.125 1...

  • Find errors in code and get 100% feedback!! Foblem (30 Points) This program This programaverages >...

    Find errors in code and get 100% feedback!! Foblem (30 Points) This program This programaverages > // It uses the ram has errors. Find the errors in the code: n averages 3 test scores. the variable perfect Score as a flag. Line # Line # include <iostream> using namespace std; int maino cout << "Enter your 3 test scores and I will ": <<"average them;": int score1, score2, score3, cin >> score1 >> score2 >> score3; double average; average (scorel...

  • 12) 8 pts. Find the errors in the following code fragment and correct them. #i nclude...

    12) 8 pts. Find the errors in the following code fragment and correct them. #i nclude <iostream> using namespace std; double fudge (double s) f return s 2.3; int mainO cout >> "Your original estimate" double estimate; cin >> estimate; cout << endl; for (int 1 = 0;1c3;i++); cout << "EStimate' < fudge(estimate) <<endl Hint: There are 4 syntax errors. There is one error that is syntactically correct but causes the output to not be what you would expect. Once...

  • C++ Fix the errors in the following code. (Not all errors are syntax related) #include <iostream>...

    C++ Fix the errors in the following code. (Not all errors are syntax related) #include <iostream> using namespace std; int main() { //Part A int numA numA = 10; cout << numA << end; /* Part B */*/ int numB = numA; cin >> usrInput; int usrInput = 0; numB = numB + usrInput; cout << numB <"/n";    /* Part C */ int numC = 10000000000; cout << numC << endl; return 0; }

  • What are the errors in the following code? My professor gave us this prewritten code and...

    What are the errors in the following code? My professor gave us this prewritten code and asked us to find the errors in it so that it can run properly #include <cstdlib> #include <ctime> #include <iostream> using namespace std; // input: integer // output: none // adds five to the given parameter void addFive( int x ) { x += 5; } // input: none // output: a random number int generateRandomNumber() { srand( time(0) ); return rand() % 100;...

  • Find the errors (both logic and syntax) in these segments of code and rewrite the entire...

    Find the errors (both logic and syntax) in these segments of code and rewrite the entire code shown with the corrections. Assume that all headers and directives are included. Note that some problems contain multiple errors, and not only syntax errors. If there are no errors, write ”No Errors.” 1. (2 points) Test if x is divisible by 3. int x; cin << x; if (!(x/3)) cout<< "x is divisible by 3"; can you solve this for c++? thanks

  • Today assignment , find the errors in this code and fixed them. Please I need help...

    Today assignment , find the errors in this code and fixed them. Please I need help with find the errors and how ro fixed them. #include <iostream> #include <cstring> #include <iomanip> using namespace std; const int MAX_CHAR = 100; const int MIN_A = 90; const int MIN_B = 80; const int MIN_C = 70; double getAvg(); char determineGrade(double); void printMsg(char grade); int main() { double score; char grade; char msg[MAX_CHAR]; strcpy(msg, "Excellent!"); strcpy(msg, "Good job!"); strcpy(msg, "You've passed!"); strcpy(msg, "Need...

  • CGALLENE 4.2: More syntax errors. ACTIVITY Each cout statement has a syntax error. Type the first...

    CGALLENE 4.2: More syntax errors. ACTIVITY Each cout statement has a syntax error. Type the first cout statement, and press Run to observe the error message. Fix the error, and run again. Repeat for the second, then third, cout statement cout << "Num: "<< songnum << endl; cout << int songNum << endl; cout << songNum " songs" << endl; Note: These activities may test code with different test values. This activity will perform two tests: the first with songNum-5,...

  • Find two syntax errors in the following program: #include <iostream> using namespace std; int area(int length,...

    Find two syntax errors in the following program: #include <iostream> using namespace std; int area(int length, int width = 0); int main() { int length, width; // for rectangle use both arguments cout << "Enter length and width of a rectangle" << endl; cin >> length >> width; cout << "The area is " << area(length, width) << endl; // for square, only need first argument cout << "Enter side of a square" << endl; cin >> length; cout <<...

  • Find and fix the errors in this C++ code: * This program illustrates a variety of...

    Find and fix the errors in this C++ code: * This program illustrates a variety of common loop errors. * Fix the errors in each section. */ #include <iostream> using namespace std; int main() { cout << "Welcome to Loop World" << endl; // SECTION I: update comment below on how you fixed this section's code, and tests run // FIX = // TESTS: cout << endl; cout << "******************" << endl; cout << "Section I" << endl; 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