1)
Consider the file input.txt which contains all information about software installed on various servers in a data center:
Server1, Database, MySQL, 5.5 Server2, Database, MySQL, 5.1 Server3, OS, Ubuntu, 10.04 Server1, OS, Ubuntu, 10.04 Server2, OS, Ubuntu, 12.04 Server3, Language, Python, 2.6.3
This file indicates that Server1, has version 5.5 of MySQL installed, and Server2 has version 5.1 installed, and Server3 has version 10.04 of Ubuntu installed. For the purposes of this program, assume that all version numbers are of the form X.Y or X.Y.Z where X, Y, and Z are strings of digits.
Write a C++ program that reads this file, and prints the number of software package names for which an out-of-date version (i.e. a version which is not the latest version) is installed on at least 2 different servers. Thus, in this case, the output of your program should be:
1
Because only 1 software package has an out-of-date version on multiple servers (Ubuntu 10.04 is an out-of-date version (the latest version is 12.04), and it is installed on two servers (Server 3, and Server 1)).
The program must read input from file input.txt (in the current directory) and write its output to the file output.txt (in the current directory). To save some time, we have already written a basic program which just reads the input into an array of lines, calls a method processData on it, and prints the output. However, currently the processData method doesn't do anything. You have to download this file, modify the processData method, and upload the modified program.
2)
Consider the following (bad) program:
#include <iostream>
#include <cstring>
class Name {
char *name;
private:
void set_name(const char *name = 0) {
delete [] this->name;
if (name) {
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
} else {
this->name = 0;
}
}
public:
Name(const char *name = 0) {
this->name = 0;
set_name(name);
}
~Name() {
delete[] name;
name = 0;
}
int len() const {return name ? strlen(name) : 0;}
friend std::istream& operator>>(std::istream &is, Name &);
friend std::ostream& operator<<(std::ostream &os, const Name &);
};
/* Do not make any changes below this point */
std::istream& operator>>(std::istream &is, Name &n) {
std::string temp;
std::getline(std::cin, temp);
n.set_name(temp.c_str());
return is;
}
std::ostream& operator<<(std::ostream &os, const Name &n) {
os << (n.name ? n.name : "<None>") << std::endl;
return os;
}
int main() {
Name longest;
while (std::cin) {
Name temp;
std::cin >> temp;
if (longest.len() < temp.len()) {
longest = temp;
}
}
std::cout << "Longest name = " << longest;
}
It is trying to read a bunch of names in the standard input and output the longest name. However, there is a serious bug in the program due to which it crashes. Please fix the bug and re-submit the program. Note 1: Just fix the bug, do not change the whole program. Specifically, don't change the mainprogram. Note 2: Please add a comment at the top of your submission indicating what changes you made.