Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ QUESTION AND CODE ATTACHED BELOW: (i)Getting a debugger to work, where it st

ID: 3589656 • Letter: C

Question

C++ QUESTION AND CODE ATTACHED BELOW:  (i)Getting a debugger to work, where it stops at a line and allows you to examine variables, (ii)Using the debugger, step through the program and find the bug, (iii) If you did not have a debugger, how would you find the problem?

I am suppose to know how to do similar problems to this and I don't know where to start. I don't know what a debugger is and this is my first coding class.


#include <iostream>

using namespace std;

#define LIMIT 25

/*

Swap the ith and jth elements of the character array letters[].

Example: swapping the 2nd and 4th elements of "palindrome" is "panildrome"

*/

void swap(char letters[], int i, int j){

   char temp = letters[j];

   letters[j] = letters[i];

   letters[i] = temp;

}

/*

Display the contents of the character array letters[].

Example: if letters[] = "palindrome", then this function should print "palindrome" to the output console.

*/

void displayLetters(char letters[]){

   cout << letters << endl;

}

/*

Reverse the contents of the character array letters[].

Example: if letters[] = "palindrome", then this function updates to letters[] = "emordnilap"

*/

void reverseLetters(char letters[]){

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

   int j = LIMIT - i - 1;

   swap( letters, i, j);

   }

}

/*

This program will reverse the contents of the character array letters[].

*/

main()

{

   //initialize the character array letters[]

   char letters[]="Rats live on no evil star";

   //display the contents of the character array letters[]

   displayLetters(letters);

   //reverse the contents of the character array letters[]

   reverseLetters(letters);

   //display the contents of the character array letters[]

   displayLetters(letters);

   return(0);

}

Explanation / Answer

Hi,
Debugging is one of the most important aspect of coding, if you face an issue, you have to know how to find the root cause of that issue in a given code,
debugger is utility that runs target program in controlled environment where you can control execution of program and see the state of program when program is paused.Most common debugger for c++ is GDB, which is pre installed almost in all systems,
So, to control the execution of program, we use something called break points,
We can tell debugger when to pause a program by setting breakpoints.
To set a breakpoint, click on blank area seen on left side of line number in editor. When you click it, it should display red circle; which means breakpoint is set on that line number,
so once the flow reaches this point, it pauses there, now you can use stepping commands like,
1. continue – Resume until next breakpoint is reached
2. step into – Execute line by line stepping into function/next lines
3. step over – Execute line by line but don’t go inside function call
4. step out – Resume execution until current function is finished
The most important part is, you can see the stack and values of variables when the program is paused, so you can set various break points and can tell when a variable is getting changed.
This is a way and the recommended way of debugging, there is one more crude way, which is just crude printing to console and analsysing like below,
#include <iostream>
using namespace std;

#define LIMIT 25

/*
Swap the ith and jth elements of the character array letters[].

Example: swapping the 2nd and 4th elements of "palindrome" is "panildrome"
*/
void swap(char letters[], int i, int j){
char temp = letters[j];
letters[j] = letters[i];
letters[i] = temp;
cout<<letters[i]<<" "<<temp<<endl;//pritnting to see if values got swapped
}

/*
Display the contents of the character array letters[].

Example: if letters[] = "palindrome", then this function should print "palindrome" to the output console.
*/
void displayLetters(char letters[]){
cout << letters << endl;
}

/*
Reverse the contents of the character array letters[].

Example: if letters[] = "palindrome", then this function updates to letters[] = "emordnilap"
*/
void reverseLetters(char letters[]){
  
for(int i=0; i<LIMIT; i++) {
int j = LIMIT - i - 1;
cout<<i<<" "<<j<<endl;//values of i and j at each iteration
swap(letters, i, j);
}
}

/*
This program will reverse the contents of the character array letters[].
*/
main()
{
//initialize the character array letters[]
char letters[]="Rats live on no evil star";

//display the contents of the character array letters[]
displayLetters(letters);

//reverse the contents of the character array letters[]
reverseLetters(letters);

//display the contents of the character array letters[]
displayLetters(letters);

return(0);
}
I recommend you to use the debugger in IDE, to debug code efficiently.

Thumbs up if this was helpful, otherwise let me know in comments

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote