In: Computer Science
Part 1:
Write a program in which you create a const whose value is determined at runtime by reading the time when the program starts (hint: use the <ctime> standard header). In a separate function, have the program create two arrays of 10,000 doubles. Initialize the first array with sequential integral values starting with 100, and initialize the second array with the same numbers, but in reverse order (i.e., the first array would contain 100, 101, 102… while the second array contains 10,099, 10,098, 10,097…). Loop through both arrays using a single loop, and multiply the corresponding array elements from each array together and display the result. Read the time when the program completes the multiplication, and compute and display the elapsed time. Do not use inline functions in this program
Part2:
Rewrite program 1 using an inline function to perform the calculation. In the test plan for this program (actual results section), compare the time required by this program to execute against the time required by the first (non-inline) program.
Language C++
Points to consider:
Code
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
const time_t start_time = time(NULL);
double arr1[10000], arr2[10000];
for(int i =100,j=0,k=9999;i<10100;i++,
j++,k--)
{
arr1[j] = i;
arr2[k] = i;
}
int sum = 0;
for(int i = 0;i<10000;i++)
{
//cout<<arr1[i] << " "
<<arr2[i] << endl;
sum += arr1[i] * arr2[i];
}
cout<<"Sum Of Product:
"<<sum<<endl;
int time_elapsed = time(NULL) - start_time;
cout<<"Time elaspsed: "
<<time_elapsed<<endl;
}
Output
Being a Subject
Matter Expert, I am obliged to do only one question at a
time.
I hope you understand.
Thanks