c++ program Write a program in which you create a const whose value is determine
ID: 3857223 • Letter: C
Question
c++ program
Write a program in which you create a const whose value is determined at runtime by reading the time when the program starts. 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.Explanation / Answer
As we need to display the elapsed time, we would be using clock() method from ctime header, which returns the number of clock ticks. Comments have been added wherever necessary.
Code:
#include<bits/stdc++.h>
#include<ctime>
using namespace std;
void multiplication(){
double a[10000],b[10000]; //double array declaration
int ai=100,bi=10099; //ai and bi are initialized to the start value of a and b array
//Generation of a and b array
for(int i=0;i<10000;i++){
a[i]=ai;
ai++;
b[i]=bi;
bi--;
}
cout<<fixed;//This line is used to avoid printing in scientific notation
for(int i=0;i<10000;i++){
cout<<a[i]*b[i]<<endl;
}
}//multiplication method ends
int main(){
//clock_t is a type that helps to represent clock ticks
const clock_t starttime=clock(); // start time(clock tick) stored
multiplication(); //method call
clock_t endtime=clock(); //end time stored
clock_t clockticks= endtime - starttime; //total clock ticks is calculated
//CLOCKS_PER_SEC gives the number of clock ticks that happen per second
double elapsed = clockticks/ (double) CLOCKS_PER_SEC;
cout<<"Elapsed time is "<<elapsed<<"seconds"<<endl;
return 0;
}//main ends
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.