Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ take a look at the source code of debugging.cpp. This program has a few bugs in it. Compile and run the program with the

C++

take a look at the source code of debugging.cpp. This program has a few bugs in it. Compile and run the program with the provided data file, data.tsv, to see how the program behaves. Using gdb to run and investigate debugging3. Fix all of the bugs in the program and give a detailed explanation. You can access the data file and source code from these links: http://www.filedropper.com/data_45

Error in this line :

people[i++] = new_person;

ther is also a segmentation fault error

http://www.filedropper.com/debugging-problem-3

The program debugging.cpp source code is below:

/*
* This program should compute the average and maximum age of all of the
* people in a TSV data file whose lines each look like this:
*
* LAST_NAME FIRST_NAME ID AGE
*
* Once compiled, you can run this program like so:
* ./debugging-problem-3 data.tsv
*/
#include
#include
#include
struct person {
std::string last_name;
std::string first_name;
int id;
int age;
};
/*
* This function counts and returns the number of entries in a TSV file. The
* number of entries is counted as the number of non-empty lines.
*
* Hint: there shouldn't be any bugs in this function.
*/
int count_tsv_file_entries(std::ifstream& infile) {
std::string line;
int count = 0;
// Make sure the file cursor is at the beginning and then count the lines.
infile.seekg(0);
do {
getline(infile, line);
if (line.length() > 0) {
count++;
}
} while (!infile.eof());
// Make sure the file's ready to read with the cursor at the beginning.
infile.clear();
infile.seekg(0);
return count;
}
/*
* This function reads people from a specified data file (with TSV format as
* described in the file header) into an array of person structs and returns
* the number of people read.
*/
int read_people(char* filename, struct person*& people) {
std::ifstream infile(filename);
int i = 0, n = count_tsv_file_entries(infile);
struct person new_person;
people = new struct person[n];
do {
infile >> new_person.last_name >> new_person.first_name >> new_person.id
>> new_person.age;
people[i++] = new_person;
} while (!infile.eof());
return n;
}
/*
* This function computes and returns the average age of all of the people in
* an array of person structs.
*/
float compute_avg_age(struct person* people, int n) {
float avg = 0;
for (int i = 0; i < n; i++) {
avg += people[i].age / n;
}
return avg;
}
/*
* This function computes and returns the maximum age of all of the people in
* an array of person structs.
*/
int compute_max_age(struct person* people, int n) {
int max = 0;
for (int i = 0; i < n; i++) {
if (max > people[i].age) {
max = people[i].age;
}
}
return max;
}
int main(int argc, char** argv) {
if (argc < 2) {
std::cerr << "Error: must specify a data file."
<< std::endl << std::endl;
std::cerr << "usage: " << argv[0] << " " << std::endl;
return 1;
}
struct person* people;
int n = read_people(argv[1], people);
float avg_age = compute_avg_age(people, n);
int max_age = compute_max_age(people, n);
std::cout << "The average age of the " << n << " people is: " << avg_age
<< std::endl;
std::cout << "The maximum age of the " << n << " people is: " << max_age
<< std::endl;
}

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_2

Step: 3

blur-text-image_3

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

More Books

Students also viewed these Databases questions