Question

In: Computer Science

Write a c++ program to load two column of numbers, do arithmetic operations to the data...

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

Solutions

Expert Solution

// 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:


Related Solutions

Write a C++ arithmetic calculator program that performs four arithmetic operations namely addition, subtraction, multiplication and...
Write a C++ arithmetic calculator program that performs four arithmetic operations namely addition, subtraction, multiplication and division. This program prompts user to enter data in the following order: First data is the number, second data is an operator and the third data is the number. You are required to: Write a function for addition. Write a function for subtraction. Write a function for multiplication. Write a function for division. Write a main program which calls four functions based on the...
(Fraction calculator c++) | Write a program that lets the user perform arithmetic operations on fractions....
(Fraction calculator c++) | Write a program that lets the user perform arithmetic operations on fractions. Fractions are of the form a/b, in which _a_ and _b_ are integers and b ≠ 0. Your program must be menu driven, allowing the user to select the operation (+, –, *, or /) and input the numerator and denominator of each fraction. Furthermore, your program must consist of at least the following functions: Function menu: This function informs the user about the...
Write a C++ program to generate two random numbers (Rnd1 and Rnd2). These two numbers should...
Write a C++ program to generate two random numbers (Rnd1 and Rnd2). These two numbers should be within a range [100, 500]. If Rnd1 greater than or equals to Rnd2, print out the result of (Rnd1-Rnd2). Otherwise, print out the result of (Rnd2-Rnd1). In all cases, print Rnd1, Rnd2, and the results of subtractions in suitable messages.
C++ Program: Write a program that prompts the user for two numbers and stores them in...
C++ Program: Write a program that prompts the user for two numbers and stores them in signed integers. The program should then add those two numbers together and store the result in a signed integer and display the result. Your program should then multiply them by each other and store the result in another integer and display the result. Then do the same but with dividing the first number by the second. Display an error message to the screen if...
Write a program in c++ that merges numbers from two files and writes all the numbers...
Write a program in c++ that merges numbers from two files and writes all the numbers into a third file in ascending order. Each input file contains a list of 50 sorted double floating-point numbers from the smallest to largest. After the program is run, the output file will contain all 100 numbers between in the two input files, also sorted from smallest to largest. Format the output into two columns – the first column contains the numbers 1-100, and...
Write a C++ program to swap two numbers and show your output
Write a C++ program to swap two numbers and show your output
Fraction calculator: Write a program that lets the user perform arithmetic operations on fractions.
Fraction calculator: Write a program that lets the user perform arithmetic operations on fractions. Fractions are of the form a / b, in which a and b are integers and b != 0.Your program must:be menu driven, allowing the user to select the operation (+, -, *, or /) and input the numerator and denominator of each fraction.The input must be validated for correct operation (+, -, *, or /) and b != 0. If any of these cases are...
(Do the algorithm and flowchart) Write a C++ program that reads integer numbers and print it...
(Do the algorithm and flowchart) Write a C++ program that reads integer numbers and print it in horizontal order of the screen
in c++ Write a program that can calculate the arithmetic mean, the geometric mean, and the...
in c++ Write a program that can calculate the arithmetic mean, the geometric mean, and the harmonic mean of a set of five numbers. •The program should ask the user to enter fiver numbers, calculate the means, and print all the data to a text file. The program should output the expected results.•Example: The text file should read: For the set of numbers {1,2,3}. The arithmetic mean is 2, the geometric mean is about 1.82, and the harmonic mean is...
: In this assignment you will write a C++ program that evaluates an arithmetic expression (represented...
: In this assignment you will write a C++ program that evaluates an arithmetic expression (represented with an infix notation), then outputs this expression in prefix form and also outputs the result of the calculation. The program will first convert the input infix expression to a prefix expression (using the Stack ADT) and then calculate the result (again, using the Stack ADT). The details are provided in the following sections.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT