Sep 8, 2022 at 3:38pm UTC
Well I've looked at the chegg site indicated. It describes the layout of the log file - but what is expected to be done with this data? Reading a sample data file in that format into a std::vector of an appropriate struct is fairly easy. But then what?? The ids' are unsigned int and the amount is a double.
Sep 8, 2022 at 3:38pm UTC
I didn't see a question, just a description of the logfile format. Not sure what you're supposed to do with it.
I any event, your code doesn't seem to match that description, you don't respect the 3 input fields for start. Also that result
local variable doesn't seem to be used.
Sep 8, 2022 at 4:03pm UTC
To simply read a data file of this format and display it's contents, then consider:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <iostream>
#include <fstream>
#include <vector>
struct Log {
unsigned sender {};
unsigned receipt {};
double amount {};
};
int main() {
std::ifstream ifs("log.dat" );
if (!ifs)
return (std::cout << "Cannot open log file\n" ), 1;
std::vector<Log> data;
for (Log l; ifs >> l.sender >> l.receipt >> l.amount; data.emplace_back(l.sender, l.receipt, l.amount));
for (const auto & [s, r, a] : data)
std::cout << s << " " << r << " " << a << '\n' ;
}
Data file is:
123456789 234567890 123.45
345678901 456789012 234.78
567889012 678901234 456.87
which displays:
123456789 234567890 123.45
345678901 456789012 234.78
567889012 678901234 456.87
Last edited on Sep 8, 2022 at 4:03pm UTC
Sep 9, 2022 at 7:33am UTC
Below are updated links.
https://www.chegg.com/homework-help/questions-and-answers/amazonian-team-responsible-maintaining-monetary-transaction-service-transactions-tracked-l-q99513835
https://www.chegg.com/homework-help/questions-and-answers/beta-t-read-text-switch-theme-1-amazon-transaction-logs-example-question-amazonian-team-re-q101271274
Below is updated code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
#include <algorithm>
#include <unordered_map>
#include <string>
#include <sstream>
using namespace std;
vector<string> finddatalog(vector<string>& log, int threshld) {
vector<string> res;
unordered_map <string, int > umap;
vector<string> tmp;
int id;
string word;
for (id = 0; id < log.size(); id++) {
stringstream strss(log[id]);
while (strss >> word) {
tmp.push_back(word);
}
if (tmp[0] != tmp[1]) {
umap[tmp[0]]++;
umap[tmp[1]]++;
}
else
umap[tmp[0]]++;
tmp.clear();
}
for (auto &i1r : umap) {
if (i1r.second >= threshld)
res.push_back(i1r.first);
}
sort(res.begin(), res.end(), [](const string& i, const string& j)
{return stoi(i) > stoi(j); });
return res;
}
int main() {
}
OUTPUT: Below are errors.
main.cpp:8:16: error: implicit instantiation of undefined template 'std::vector<std::string>'
vector<string> finddatalog(vector<string>& log, int threshld) {
^
/root/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1/iosfwd:260:28: note: template is declared here
class _LIBCPP_TEMPLATE_VIS vector;
^
main.cpp:9:20: error: implicit instantiation of undefined template 'std::vector<std::string>'
vector<string> res;
^
/root/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1/iosfwd:260:28: note: template is declared here
class _LIBCPP_TEMPLATE_VIS vector;
^
main.cpp:11:20: error: implicit instantiation of undefined template 'std::vector<std::string>'
vector<string> tmp;
^
/root/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1/iosfwd:260:28: note: template is declared here
class _LIBCPP_TEMPLATE_VIS vector;
^
main.cpp:14:26: error: implicit instantiation of undefined template 'std::vector<std::string>'
for (id = 0; id < log.size(); id++) {
^
/root/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1/iosfwd:260:28: note: template is declared here
class _LIBCPP_TEMPLATE_VIS vector;
^
main.cpp:15:31: error: type 'vector<string>' (aka 'vector<basic_string<char, char_traits<char>, allocator<char>>>' ) does not provide a subscript operator
stringstream strss(log[id]);
~~~^~~
5 errors generated.
Last edited on Sep 9, 2022 at 8:29am UTC
Sep 9, 2022 at 8:56am UTC
Below is modified code. Is this correct?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include <algorithm>
#include <unordered_map>
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
using namespace std;
vector<string> finddatalog(vector<string>& log, int threshld) {
vector<string> res;
unordered_map <string, int > umap;
vector<string> tmp;
int id;
string word;
for (id = 0; id < log.size(); id++) {
stringstream strss(log[id]);
while (strss >> word) {
tmp.push_back(word);
}
if (tmp[0] != tmp[1]) {
umap[tmp[0]]++;
umap[tmp[1]]++;
}
else
umap[tmp[0]]++;
tmp.clear();
}
for (auto &i1r : umap) {
if (i1r.second >= threshld)
res.push_back(i1r.first);
}
sort(res.begin(), res.end(), [](const string& i, const string& j)
{return stoi(i) > stoi(j); });
return res;
}
int main() {
vector<string> ids = {"123456789 234567890 45" , "293230 38240 24" ,
"123456788 234567891 15" , "293230 38240 78" ,
"123456790 234567892 23" , "293230 345377 14" ,
"123456791 234567893 23" };
vector<string> resl(finddatalog(ids, 3));
for (auto &s2: resl)
cout<<s2<<" " ;
return 0;
}
OUTPUT: 293230
Last edited on Sep 9, 2022 at 9:01am UTC
Sep 9, 2022 at 9:06am UTC
Modified code again. But how to keep order of users id?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include <algorithm>
#include <unordered_map>
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
using namespace std;
vector<string> finddatalog(vector<string>& log, int threshld) {
vector<string> res;
unordered_map <string, int > umap;
vector<string> tmp;
int id;
string word;
for (id = 0; id < log.size(); id++) {
stringstream strss(log[id]);
while (strss >> word) {
tmp.push_back(word);
}
if (tmp[0] != tmp[1]) {
umap[tmp[0]]++;
umap[tmp[1]]++;
}
else
umap[tmp[0]]++;
tmp.clear();
}
for (auto &i1r : umap) {
if (i1r.second >= threshld)
res.push_back(i1r.first);
}
sort(res.begin(), res.end(), [](const string& i, const string& j)
{return stoi(i) > stoi(j); });
return res;
}
int main() {
vector<string> ids = {"123456789 234567890 45" , "293230 38240 24" ,
"123456788 123456789 15" , "293230 38240 78" ,
"123456789 123456788 23" , "293230 123456789 14" ,
"123456788 123456788 23" };
vector<string> resl(finddatalog(ids, 3));
for (auto &s2: resl)
cout<<s2<<" " ;
return 0;
}
OUTPUT: 123456789 123456788 293230
Last edited on Sep 9, 2022 at 9:08am UTC
Sep 9, 2022 at 9:26am UTC
OK. Consider:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include <iostream>
#include <vector>
#include <map>
#include <sstream>
#include <string>
std::vector<std::string> finddatalog(const std::vector<std::string>& log, unsigned threshld) {
std::vector<std::string> res;
std::map <std::string, unsigned > idmap;
for (const auto & l : log) {
std::istringstream iss(l);
std::string id1, id2;
iss >> id1 >> id2;
++idmap[id1];
if (id1 != id2)
++idmap[id2];
}
for (const auto & [id, cnt] : idmap)
if (cnt >= threshld)
res.push_back(id);
return res;
}
int main() {
const std::vector<std::string> ids { "123456789 234567890 45" , "293230 38240 24" ,
"123456788 123456789 15" , "293230 38240 78" ,
"123456789 123456788 23" , "293230 123456789 14" ,
"123456788 123456788 23" };
for (const auto & r : finddatalog(ids, 3))
std::cout << r << '\n' ;
}
which displays:
123456788
123456789
293230
Last edited on Sep 9, 2022 at 10:56am UTC