In: Computer Science
Write a c++ program to load two column of numbers, do arithmetic operations to the data points by following the steps below carefully. Also we need to capture the running time in microseconds for each of these and also the time taken for basic operation.
Steps to complete.
0. Download the data file named Datafile1.data from the file area of the assignment.
1. Load two columns of data points, each column into a storage area, preferably a vector.
2. Initialize/start the clock to capture start of addition clock.
3. Add each position of the data and store it in another storage area.
4. Capture the clock for capturing time end for additions.
5. Start the clock for capturing multiplication
6. Multiply each position of the data and store it to another storage area.
7. Capture the clock for end of multiplications.
8. Print both elapsed time for addition and multiplication in microseconds.
9. Print basic operation time for addition and multiplication in microseconds.
10. Write the results of addition and multiplication to a file named Output.data
Datafile1.data
9.144548371 0.62072663
7.766500067 1.107554175
6.428523445 0.409904615
4.864271967 1.142139412
7.064106885 1.138740729
2.262955152 1.198965847
9.800966285 0.624420569
8.466914743 0.172152387
7.089199692 0.650149663
6.042680886 0.526675179
1.320701358 0.893658642
3.576178301 0.966546628
4.939756105 0.53583099
3.386905283 0.24428521
5.231554752 0.660683916
3.736280728 0.378121052
7.527971435 0.917047269
2.533630929 1.029850114
9.188222671 0.237647807
1.800088589 0.763132902
9.702171329 1.187835787
4.713240732 0.602808579
0.350114291 0.126105779
1.375572924 0.534626533
3.699384174 0.670885379
7.183110563 1.327536861
7.434890545 1.174302317
9.781202522 1.271484048
7.153021655 0.99647769
0.516259776 0.868291689
9.637573135 0.596947716
7.056519481 0.350322259
0.160533268 0.565683307
9.008315924 0.919842547
9.173718748 0.593500412
6.921833783 0.015908284
6.736418485 0.719292318
3.353631576 1.363438995
1.604967361 0.679785674
5.672035588 0.171718917
7.794867595 0.550310846
5.889160186 0.07760882
1.690701207 0.829202546
4.155650455 0.94739278
6.018503489 0.989127529
1.46468541 1.171169011
2.670443291 0.505688273
8.22853784 0.514127061
9.631840542 0.238298413
1.669453846 1.27499486
3.311515543 0.789657118
5.969493897 0.053853209
2.478574394 1.206080543
7.696244495 0.099599082
3.769765061 1.193693451
1.992200015 0.895544331
4.030882115 0.398607671
2.939993773 0.360149058
8.499182913 0.645159168
1.33467738 0.264472403
3.475730127 0.826362657
7.471431245 1.220818624
6.140279634 0.462733534
3.740656241 1.363952935
5.174154405 1.157155608
9.466008548 0.261690906
9.225005657 0.158991829
7.061522707 0.200296564
2.527050453 0.668705617
3.989058951 0.659984653
5.969333965 0.297768248
6.813173789 1.23032852
7.47467435 0.289477322
0.27004337 0.808100642
0.226724804 0.947863653
8.491775489 0.703714179
9.67169797 0.439579426
7.171630039 0.893035833
8.435125276 0.053497387
1.740878827 1.135864744
1.624571489 0.537973501
9.398492585 1.285333242
9.855769556 1.156707773
5.536670988 0.524629176
// do comment if any problem arises
//code
#include <iostream>
#include <vector>
#include <chrono>
#include <fstream>
using namespace std;
int main()
{
//input and output filenames
string infile = "Datafile1.data", outfile =
"Output.data";
//open input file
ifstream input(infile);
//open output file
ofstream output(outfile);
//vector for first number in each line
vector<long double> first_number;
//vector for second number in each line
vector<long double> second_number;
//read numbers till end of file
while (input)
{
long double first, second;
input >> first >>
second;
first_number.push_back(first);
second_number.push_back(second);
}
//vector holding addition of 2 numbers
vector<long double> addition;
//for calculation of basic operation time of
addition
auto start =
chrono::high_resolution_clock::now();
long double temp = first_number[0] +
second_number[0];
auto finish =
chrono::high_resolution_clock::now();
//calculate basic operation time in microseconds
long double addition_basic_operation_time =
chrono::duration_cast<chrono::nanoseconds>(finish -
start).count();
addition_basic_operation_time *= 0.001;
//for calculation of total elapsed time for
addition
start = chrono::high_resolution_clock::now();
for (int i = 0; i < first_number.size(); i++)
{
long double sum = first_number[i] +
second_number[i];
addition.push_back(sum);
}
finish = chrono::high_resolution_clock::now();
//calculate total elapsed time in microseconds
long double addition_elapsed_time =
chrono::duration_cast<chrono::microseconds>(finish -
start).count();
//multiplication
vector<long double> multiplication;
//calculate basic operation time
start = chrono::high_resolution_clock::now();
temp = first_number[0] * second_number[0];
finish = chrono::high_resolution_clock::now();
//calculate basic operation time in microseconds
long double mult_basic_operation_time =
chrono::duration_cast<chrono::nanoseconds>(finish -
start).count();
mult_basic_operation_time *= 0.001;
//for calculation of total elapsed time of
multiplication
start = chrono::high_resolution_clock::now();
for (int i = 0; i < first_number.size(); i++)
{
long double sum = first_number[i] *
second_number[i];
multiplication.push_back(sum);
}
finish = chrono::high_resolution_clock::now();
//calculate total elapsed time in microseconds
long double mult_elapsed_time =
chrono::duration_cast<chrono::microseconds>(finish -
start).count();
//print to screen
cout << "Elapsed time:\n";
cout << "Addition: " <<
addition_elapsed_time << " ms\nMultiplication: " <<
mult_elapsed_time << " ms\n";
cout << "\nBasic operation time:\n";
cout << "Addition: " <<
addition_basic_operation_time << " ms\nMultiplication: "
<< mult_basic_operation_time << " ms\n";
//write to file
output << "Elapsed time:\n"
<< "Addition: " <<
addition_elapsed_time << " ms\nMultiplication: " <<
mult_elapsed_time << " ms\n";
output << "\nBasic operation time:\n"
<< "Addition: " <<
addition_basic_operation_time << " ms\nMultiplication: "
<< mult_basic_operation_time << " ms\n";
//close input file
input.close();
//close output file
output.close();
}
Output:
Output.data: