You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#include<stdio.h>
#include<iostream>
#include<fstream>
#include<thread>
#include<vector>usingnamespacestd;
string rd = "<ABSOLUTE-PATH-TO-FILES-FOLDER>";
vector<thread> threads;
long sum = 0;
string generateFolderName(int a)
{
char fn[24];
if (a % 10 == 0) {
a -= 1;
}
int h = a + (10 - (a % 10));
int l = h - 9;
sprintf(fn, "%06d-%06d", l, h);
return fn;
}
string generateFileName(int a)
{
char fn[12];
sprintf(fn, "%06d.csv", a);
return fn;
}
intsumNumbersInFile(string fd)
{
ifstream f;
f.open(fd);
long sum = 0;
int num = 0;
char c;
while (!f.eof() ) {
f.get(c);
if (c != ',' && c != '\n') {
int ic = c - '0';
if (num == 0) {
num = ic;
} else {
num = (num * 10) + ic;
}
}
if (c == ',' || c == '\n') {
sum += num;
num = 0;
}
}
f.close();
//last number//division is done because the last digit is repeated
sum += num / 10;
return sum;
}
intsumNumbers(int i)
{
string fd;
fd = rd;
fd.append(generateFolderName(i)).append("/").append(generateFileName(i));
sum += sumNumbersInFile(fd);
}
intmain()
{
for (int i = 1; i <= 1000; i++) {
threads.emplace_back(sumNumbers,i);
}
for (thread & t : threads) {
t.join();
}
cout << sum << '\n';
return1;
}
solution hosted on https://github.com/the-fanan/sum-files-challenge/cpp-solution
Command
From the root directory of the solution run the following commands
g++ -std=c++11 -pthread main.cpp -o main
./main
Output =49947871404
The text was updated successfully, but these errors were encountered:
Hardware
Processor Name: Intel® Core™ i5-2540M CPU @ 2.60GHz × 4
Memory: 8 GB
Time
~= 2.5s
Program
solution hosted on
https://github.com/the-fanan/sum-files-challenge/cpp-solution
From the root directory of the solution run the following commands
g++ -std=c++11 -pthread main.cpp -o main
./main
=49947871404
The text was updated successfully, but these errors were encountered: