Question

In: Computer Science

Part 1: Write a program in which you create a const whose value is determined at...

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++

Solutions

Expert Solution

Points to consider:

  1. We need to find the time using time() of the ctime.
  2. Then we need to define two arrays and initialize the arrays.
  3. Then we need to multiply both of them.
  4. Print the result of sums.

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


Related Solutions

Write a program Write a program whose inputs are three integers, and whose output is the...
Write a program Write a program whose inputs are three integers, and whose output is the smallest of the three values. Ex: If the input is: 7 15 3 the output is: 3 C++ please
Write a program whose input is a string which contains a character and a phrase, and...
Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. Ex: If the input is: n Nobody the output is: 0...
C language <stdio.h> use double and const Write a program that asks the user for the...
C language <stdio.h> use double and const Write a program that asks the user for the radius of a circle (as a double) and displays the diameter, circumference, and area of the circle. Use a constant of 3.14159 as the value of pi. Have a blank line between the input and the output. Follow the 3 steps in the Information Processing Cycle - Input, Processing, and Output. The formulas needed are: diameter = 2 * radius circumference = 2 *...
Create program which sorts letters of a string based on ASCII value. The program will then...
Create program which sorts letters of a string based on ASCII value. The program will then print the sorted string to stdout. Use C programming language. - Only use stdio.h - Input prompt should say "Enter string of your choice: " - Remove any newline \n from input string - Implement sorting operation as a function. Should use selection sort algorithm, but you may use a different algorithm - Output should print sorted string on new line Example:     Enter...
Part A: Create a project with a Program class and write the following two methods (headers...
Part A: Create a project with a Program class and write the following two methods (headers provided) as described below: A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number if the number is not in the range or a non-numeric...
Create a Visual Studio console project (c++) containing a main() program that declares a const int...
Create a Visual Studio console project (c++) containing a main() program that declares a const int NUM_VALUES denoting the array size. Then declare an int array with NUM_VALUES entries. Using a for loop, prompt for the values that are stored in the array as follows: "Enter NUM_VALUES integers separated by blanks:" , where NUM_VALUES is replaced with the array size. Then use another for loop to print the array entries in reverse order separated by blanks on a single line...
PART 1: WRITE A PROGRAM THAT TAKES IN TWO INTEGERS VALUE N AND M (USER INPUT)...
PART 1: WRITE A PROGRAM THAT TAKES IN TWO INTEGERS VALUE N AND M (USER INPUT) AND CALCULATES THE NTH FIBONACCI SUM USING RECURSION. EXAMPLE: OLD VERSION 0,1,1,2,3.. USING USER INPUT: 0, N, M ,N+M, (N+M)+M PART 2: WRITE THE SAME PROGRAM USING USER INPUT BUT USING A LOOP IN STEAD OF RECURSION. PYTHON
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using...
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using Notepad) into a one-dimensional array and then analyzes the numbers as described below. Your program must use loops to read the numbers into the array and to analyze the contents of the array. The program’s main function should do the following:  Read eight floating-point numbers from the file named numbers.txt into a onedimensional array, displaying each number on the screen.  Pass the...
Write a program whose inputs are three integers, and whose output is the smallest of the...
Write a program whose inputs are three integers, and whose output is the smallest of the three values
Write a program whose inputs are three integers, and whose output is the smallest of the...
Write a program whose inputs are three integers, and whose output is the smallest of the three values. Use else-if selection and comparative operators such as '<=' or '>=' to evaluate the number that is the smallest value. If one or more values are the same and the lowest value your program should be able to report the lowest value correctly. Don't forget to first scanf in the users input. Ex: If the input is: 7 15 3 the output...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT