#include <limits.h>
#include <iostream>
#include <numeric>
#include <fstream>
#include <vector>
#include <string.h>
#include <iomanip>
#include <omp.h>
#include <time.h>
using namespace std;
int main()
{
double start = omp_get_wtime();
vector<string> codons = { "ttt" }; // Better always initialize any variable or array or objects to zero or NULL or empty string.
codons.push_back("ttc"); // { "ttt", "ttc"
codons.push_back("tta"); // { "ttt", "ttc", "tta"
codons.push_back("ttg"); // { "ttt", "ttc", "tta", ...
codons.push_back("tct");
codons.push_back("tcc");
codons.push_back("tca");
codons.push_back("tcg");
codons.push_back("tat");
codons.push_back("tac");
codons.push_back("taa");
codons.push_back("tag");
codons.push_back("tgt");
codons.push_back("tgc");
codons.push_back("tga");
codons.push_back("tgg");
codons.push_back("ctt");
codons.push_back("ctc");
codons.push_back("cta");
codons.push_back("ctg");
codons.push_back("cct");
codons.push_back("ccc");
codons.push_back("cca");
codons.push_back("ccg");
codons.push_back("cat");
codons.push_back("cac");
codons.push_back("caa");
codons.push_back("cag");
codons.push_back("cgt");
codons.push_back("cgc");
codons.push_back("cga");
codons.push_back("cgg");
codons.push_back("att");
codons.push_back("atc");
codons.push_back("ata");
codons.push_back("atg");
codons.push_back("act");
codons.push_back("acc");
codons.push_back("aca");
codons.push_back("acg");
codons.push_back("aat");
codons.push_back("aac");
codons.push_back("aaa");
codons.push_back("aag");
codons.push_back("agt");
codons.push_back("agc");
codons.push_back("aga");
codons.push_back("aag");
codons.push_back("gtt");
codons.push_back("gtc");
codons.push_back("gta");
codons.push_back("gtg");
codons.push_back("gct");
codons.push_back("gcc");
codons.push_back("gca");
codons.push_back("gcg");
codons.push_back("gat");
codons.push_back("gac");
codons.push_back("gaa");
codons.push_back("gag");
codons.push_back("ggt");
codons.push_back("ggc");
codons.push_back("gga");
codons.push_back("ggg"); // // { "ttt", "ttc", "tta", ..., "ggg"}
// codons.size() is 64
vector<int> counts(64, 0);
string line = ""; // Always initialize.
// int numberOfLines=0; // warning: unused variable numberOfLines
string FileContent = "";
string curLine = "";
// No need to open the file inside a loop
ifstream myfile("mouse.dat");
if (myfile.is_open())
{
while (!myfile.eof())
{
curLine = ""; // Added this line after finding following exception when searching words "tct" "ctc"
/*
------------- Exception result 4991 ------------------
tct 4991
Actual count: 4990
------------- Exception result 4687 ------------------
ctc 4687
Actual count: 4686
*/
myfile >> curLine;
FileContent += curLine;
if ("" == curLine) // Remove blank lines if present
{
continue;
}
}
myfile.close();
}
else
{
cout << "Unable to open file ";
perror("Error: ");
exit(1);
}
omp_set_num_threads(16);
int id;
id = omp_get_thread_num();
#pragma omp parallel private(id)
{
#pragma omp sections
{
#pragma omp section
{
for (int indx = 0; 64 > indx; indx++) // Better compare using "number comparison variable" way
{
ifstream myfile("mouse.dat");
// Use this line for debugging
// cout << "FileContent:" << FileContent << '\n';
unsigned long CodonsLoc = 0;
if ("tct" == codons[indx])
{
// cout << "debuging\n";
}
while (ULONG_MAX != (CodonsLoc = FileContent.find(codons[indx], CodonsLoc)))
{
counts[indx]++;
// cout << "counts[indx] " << counts[indx] << '\n';
CodonsLoc += codons[indx].length();
}
}
ofstream newFile("results.txt");
if (newFile.fail())
{
perror("Opening results.txt file failed");
exit(2);
}
else
{
unsigned int TotalCount = accumulate(counts.begin(), counts.end(), 0);
cout << "TotalCount: " << TotalCount << '\n';
newFile << "TotalCount: " << TotalCount << '\n';
for (int indx = 0; 64 > indx; indx++) // Better compare using "number comparison variable" way
{
// using setprecision having 9 digits at float
// using fixed to keep trailing zeros.
// Using setw(4) for counts[indx] alignment
cout << codons[indx] << " " << setw(4) << counts[indx] << " percent: " << fixed << setprecision(9) << (float)100 * counts[indx] / TotalCount << '\n';
newFile << codons[indx] << " " << setw(4) << counts[indx] << " percent: " << fixed << setprecision(9) << (float)100 * counts[indx] / TotalCount << '\n';
}
newFile.close();
}
}
}
}
return 0;
printf("Time: \t %f \n", omp_get_wtime() - start);
}
Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.
When asked, what has been your best career decision?
Deciding to stick with EE.
Being involved with EE helped me to grow personally and professionally.
Connect with Certified Experts to gain insight and support on specific technology challenges including:
We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE