Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Debugging grades Let's take the starter code below and debug it using onlinegdb.com. This is a tool that puts a WUI on a program called

Debugging grades

Let's take the starter code below and debug it using onlinegdb.com. This is a tool that puts a WUI on a program called gdb. GDB is the Gnu Gnu is not Unix) Debugger. Let's fix the problems that we find in the functions. I recommend putting a breakpoint in main right before entering getGrades to see how all of the local variables are set and to get an idea of how this onlinegdb tool works,

Follow the instructions below:

  1. Open the link above in a new browser window.
  2. Set the language to C++ in the right hand corner.
  3. Copy the code into the onlinegdb code window which is main.cpp
  4. Click the blue Debug button in the toolbar at the top and wait for the program to compile.
  5. [the program will be compiled with the following command: g++ -g main.cpp]

Reading symbols from /home/a.out...done.

(gdb)

You can use the buttons to _Start the program, but gdb is a command line tool. You can also get help by typing help. [See below.]

When the program stops, the stackframe will be printed and the prompt will appear. Again, you can use the buttons to

  • continue (proceeds to the next breakpoint, input or the end) also continue from command line
  • step over (doesn't step into functions) also next from command line
  • step into (does step into functions) also step from command line
  • step out (completes function and stops in caller) also finish from command line

Set Some Breakpoints

A breakpoint is a stopping point in the code. From there, you can inspect the values of local and global variables or parameters using the print command. The window to the right shows the stack frame at the top (function calls), the local variables in the middle, a window that allows you to enter display expressions and

Find the getGrades function and click to the left of the line number (37) until you see an orange-red ball.

Find the totalGrade function and click to the left of the line number [77]..

FInd the min function call and click to the left of the line number [87].

Click _Start and wait for the program to stop at the first breakpoint at line.

Type display grade

Use the buttons or gdb commands described above to step through the program to try and figure out what is wrong. There are a few problems.

HINT: Use the print command to inspect variables and arrays. Step into functions to make sure they are doing the correct calculations. The conversion functions DO NOT CONTAIN the errors. Assume these functions are correct..

GDB Help

You can type help and get the following output. If you want more detailed help, type help command.

(gdb) help

Type "help" followed by a class name for a list of commands in that class.

Type "help all" for the list of all commands.

Type "help" followed by command name for full documentation.

Type "apropos word" to search for commands related to "word".

Command name abbreviations are allowed if unambiguous.

Once you're done with debugging, answer the question:

Where and how did you find the bugs? If you were unable to find all of the bugs, what did you do to try and find the bugs. How did you find the ones you found? Remember, there are three problems. Sometimes test cases pass even though there are still problems.

** I had to add spaces between the word Exam in the code because Chegg was flagging the word**

This is the starter code:

#include

#include

#include

using namespace std;

// Added a const to use partially filled array

const int MAX_NUM_E X A M S=15;

float convert2number(string grade);

// Converts letter grade to numeric grade

string convert2letter(float grade);

// Converts numeric grade to letter grade

void getGrades(string grades[], int size);

// returns an array letter grades, generalize to array

float calcTotal(float gradeNum[], int size);

// Calculates total, generalize to array.

float min(float gradeNum[], int size);

// returns the minimum value, generalize to array.

int main()

{

// Change this to arrays

// declare variables for letter and number grades

// string grade1, grade2, grade3;

string grade[MAX_NUM_E X A M S], finalGrade;

// float grNum1, grNum2, grNum3;

float grNum[MAX_NUM_E X A M S], total=0, average;

int numExams=0;

// read the letter grades

// getGrades(grade1, grade2, grade3);

getGrades(grade, numE x a m s);

// call convert2number for all letter grades

for (int i = 0; i < numE x a m s; i++)

grNum[i] = convert2number(grade[i]);

// call minGrade

total = calcTotal(grNum, numE x a m s) - min(grNum, numE x a m s);

// take average

average = total/(numE x a m s-1);

// convert average to letter grade

cout.precision(3);

cout << "Final average is " << average << endl;

cout << "Final grade is " << convert2letter(average) << endl;

return 0;

}

// returns array of letter grades, generalize to array

void getGrades(string grades[], int size)

{

size = 0;

cout << "Enter up to " << MAX_NUM_E X A M S << " student e x a m grades or Q to quit: " << endl;

string input;

do {

cin >> input;

if (input != "Q") {

grades[size] = input;

size++;

}

} while (input != "Q" && size < MAX_NUM_E X A M S);

}

// Calculates total, generalize to array.

float calcTotal(float gradeNum[], int size)

{

// return gr1 + gr2 + gr3;

float total;

for (int i = 0; i < size; i++)

total = total + gradeNum[i];

return total;

}

// returns the minimum value, generalize to array.

float min(float gradeNum[], int size)

{

float min = gradeNum[0];

for (int i = 0; i < size; i++)

if (gradeNum[i] < min) {

int min = gradeNum[i];

}

return min;

}

// Converts letter grade to numeric grade

float convert2number(string grade)

{

float gradeNum = 0.0;

switch(grade[0]) {

case 'A':

gradeNum = 4.0;

break;

case 'B':

gradeNum = 3.0;

break;

case 'C':

gradeNum = 2.0;

break;

case 'D':

gradeNum = 1.0;

break;

case 'F':

gradeNum = 0.0;

break;

}

if (grade.length() > 1) {

if (grade[1] == '+')

gradeNum += .30;

else

gradeNum -= .30;

}

return gradeNum;

}

// Converts numeric grade to letter grade

string convert2letter(float gradeNum)

{

string grade;

switch((int)round(gradeNum))

{

case 4:

grade = "A";

break;

case 3:

grade = "B";

break;

case 2:

grade = "C";

break;

case 1:

grade = "D";

break;

case 0:

grade = "F";

break;

}

// if the fractional part is >= .85 && <= .15 has no +/-

float extra = gradeNum - round(gradeNum);

if (extra > .15)

grade += '+';

else if (extra < -.15)

grade += '-';

return grade;

} grade to numeric grade float convert2number(string grade) { float gradeNum = 0.0; switch(grade[0]) { case 'A': gradeNum = 4.0; break; case 'B': gradeNum = 3.0; break; case 'C': gradeNum = 2.0; break; case 'D': gradeNum = 1.0; break; case 'F': gradeNum = 0.0; break; } if (grade.length() > 1) { if (grade[1] == '+') gradeNum += .30; else gradeNum -= .30; } return gradeNum; }

// Converts numeric grade to letter grade string convert2letter(float gradeNum) { string grade; switch((int)round(gradeNum)) { case 4: grade = "A"; break; case 3: grade = "B"; break; case 2: grade = "C"; break; case 1: grade = "D"; break; case 0: grade = "F"; break; } // if the fractional part is >= .85 && <= .15 has no +/- float extra = gradeNum - round(gradeNum); if (extra > .15) grade += '+'; else if (extra < -.15) grade += '-'; return grade; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Databases Technologies And Applications

Authors: Zongmin Ma

1st Edition

1599041219, 978-1599041216

More Books

Students also viewed these Databases questions