Download the attached file/s, copy and paste the code segment/s into your visual
ID: 3880412 • Letter: D
Question
Download the attached file/s, copy and paste the code segment/s into your visual studio or any other C++ IDE and run it. You will have to implement a small intentional bug in your program and post it for other students to debug.
To be able to receive your full discussion points, you need to submit the following.
Following is your check list and rubric
Attach your .cpp file/s with an implemented bug - 20pnts
Describe what the code does in detail - 20pnts
Debug at least one other program and submit your .cpp file - 40pnts (note what the bug was in comments)
After running the debugged program, attach the screen shot of your output- 20pnts
Here is the code I was given:
// This program demonstrates a function, countChars, that counts
// the number of times a specific character appears in a string.
#include <iostream>
using namespace std;
int countChars(char *, char); // Function prototype
int main()
{
const int SIZE = 51; // Array size
char userString[SIZE]; // To hold a string
char letter; // The character to count
// Get a string from the user.
cout << "Enter a string (up to 50 characters): ";
cin.getline(userString, SIZE);
// Get a character to count occurrences of within the string.
cout << "Enter a character and I will tell you how many ";
cout << "times it appears in the string: ";
cin >> letter;
// Display the number of times the character appears.
cout << letter << " appears ";
cout << countChars(userString, letter) << " times. ";
return 0;
}
//****************************************************************
// Definition of countChars. The parameter strPtr is a pointer *
// that points to a string. The parameter Ch is a character that *
// the function searches for in the string. The function returns *
// the number of times the character appears in the string. *
//****************************************************************
int countChars(char *strPtr, char ch)
{
int times = 0; // Number of times ch appears in the string
// Step through the string counting occurrences of ch.
while (*strPtr != '')
{
if (*strPtr == ch) // If the current character equals ch...
times++; // ... increment the counter
strPtr++; // Go to the next char in the string.
}
return times;
}
I would like the code to build successfully without any errors. So, I guess I would like the bug to be "hidden" in a way.
Thank you in advance.
Explanation / Answer
At line 34 I have introduced a bug. Though there are multiple ways to introduce bug in program intentionally, I am showing only one below:
// This program demonstrates a function, countChars, that counts
// the number of times a specific character appears in a string.
#include <iostream>
using namespace std;
int countChars(char *, char); // Function prototype
int main() {
const int SIZE = 51; // Array size
char userString[SIZE]; // To hold a string
char letter; // The character to count
// Get a string from the user.
cout << "Enter a string (up to 50 characters): ";
cin.getline(userString, SIZE);
// Get a character to count occurrences of within the string.
cout << "Enter a character and I will tell you how many ";
cout << "times it appears in the string: ";
cin >> letter;
// Display the number of times the character appears.
cout << letter << " appears ";
cout << countChars(userString, letter) << " times. ";
return 0;
}
//****************************************************************
// Definition of countChars. The parameter strPtr is a pointer *
// that points to a string. The parameter Ch is a character that *
// the function searches for in the string. The function returns *
// the number of times the character appears in the string. *
//****************************************************************
int countChars(char *strPtr, char ch) {
int times = 0; // Number of times ch appears in the string
// Step through the string counting occurrences of ch.
while (*strPtr != '') {
if (*strPtr == ch) // If the current character equals ch...
//Bug :: At first iteration it will return
return times++; // ... increment the counter
strPtr++; // Go to the next char in the string.
}
return times;
}
At line 34 value is returned without further calculation. So basically their exist calculation bug. It will always return 0.
Bug : A bug is a system behavior that does not meet the requirements and produces the incorrect results. So if you write anything that is not required in the code to process or something that will change the output is actually a bug. Also, its very simple when you are asked to intentionally add bug in software code, as you know what is the bug and from where it is generating.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.