Question
You should open the program use gdb to debug a buggy program. The program should run t like: /bugging test.tsv. Can you give details about
You should open the program use gdb to debug a buggy program. The program should run t like: /bugging test.tsv. Can you give details about where you found the buggy by use gdb?
/** 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, */
bugging.cpp
#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] << "
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;}
test.tsv
Skywalker Luke 10000123 24
Organa Leia 10000121 24
Solo Han 10000117 29
? Rey 10000030 21
Dameron Poe 10000204 27
Vader Darth 100001A7 58
Ren Kylo 100002EB 26
FN-2187 Finn 10000101 23
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started