Data structures and algorithms in C++
Data structures and algorithms in C++
2nd Edition
ISBN: 9780470460443
Author: Goodrich
Publisher: WILEY
bartleby

Videos

Expert Solution & Answer
Book Icon
Chapter 4, Problem 1P

Explanation of Solution

Program code:

//include the required header files

#include <iostream>

#include<time.h>

//use the std namespace

using namespace std;

//Declare n as type of constant

const int n = 10;

//Implementation of prefixAverages1 array

double *prefixAverages1(int X[])

{

//Declare a as integer type

int a;

//Decalre A as double pointer type array

double *A = new double[n];

//Iterate the loop

for (int i = 0; i <= n - 1; i++)

{

//assign 0 to a

a = 0;

//Iterate the loop

for (int j = 0; j <= i; j++)

{

//calculate a = a+X[j] value

a = a + X[j];

}

//assign a / (i + 1)

A[i] = a / (i + 1);

}

//return value of the array A

return A;

}

//Implementation of prefixAverages1 array

double *prefixAverages2(int X[])

{

//Declare s and assign 0

int s = 0;

//Decalre A as double pointer type array

double *A = new double[n];

//Iterate the loop

for (int i = 0; i <= n - 1; i++)

{

//calculate s = s+X[i] value

s = s + X[i];

//assign s / (i + 1)

A[i] = s / (i + 1);

}

//return A

    return A;

}

int main()

{

//Decalre X as type of an array

int X[n];

//Declare differenceValue1, differenceValue2 as type of double

double differenceValue1, differenceValue2;

//Declare timeInseconds1, timeInseconds2 as type of double

double timeInseconds1, timeInseconds2;

//Declare i as type of integer

int i = 0;

//Iterate the loop

while (i <= n)

{

//assign i to X[i]

X[i] = i;

//increment i

i++;

}

//Declare resultimeEntrie1,resultimeEntrie2 as type of double

double *resultimeEntrie1 =0, *resultimeEntrie2 = 0;

// declare clock_t objects as timeEntrie1,timeEntrie2

clock_t timeEntrie1, timeEntrie2;

timeEntrie1 = clock();

//store the return value of prefixAverages1(x)

resultimeEntrie1 = prefixAverages1(X);

//Declare k as integer type and assign 0

int k = 0;

// initialize timeEntrie2

timeEntrie2 = clock();

// get the differenceValueerence of two object values

differenceValue1 = (double)timeEntrie2 - (double)timeEntrie1;

// calculate timeInseconds

timeInseconds1 = differenceValue1 / CLOCKS_PER_SEC;

// display statement for timeInseconds

cout << " prefixAverages1(X) " << endl;

//Display statement

cout<<"For n = " << n << " elements then it take " << timeInseconds1 << " seconds" << endl;

// declare clock_t objects as timeEntrie3,timeEntrie4

clock_t timeEntrie3, timeEntrie4;

timeEntrie3 = clock();

//store the return value of prefixAverages2(x)

resultimeEntrie2 = prefixAverages2(X);

timeEntrie4 = clock();

// calculate the differenceValue of two object values

differenceValue2 = (double)timeEntrie4 - (double)timeEntrie3;

// calculate timeInseconds

timeInseconds2 = differenceValue2 / CLOCKS_PER_SEC;

// display statement for timeInseconds

cout << " prefixAverages2(X)" << endl;

//Display statement

cout<<"\n For n = " << n << " elements then it take " << timeInseconds2 << " seconds" << endl;

    return 0;

}

Explanation:

The above program snippet is used to implement “prefixAverages1()” and “prefixAverages2()”. In the code,

  • Include the required header files.
  • Use the “std” namespace.
  • Declare the constant “n”.
  • Define the method “prefixAverages1()”.
    • Declare the required variables.
    • Declare an array “A”.
    • Iterate a “for” loop.
      • Calculate the value of “a”.
      • Iterate a “for” loop.
        • Calculate the value of “a”.
      • Assign a value “a/(i+1)”.
    • Return the value of “A”...

Blurred answer
Students have asked these similar questions
Dijkstra's Algorithm (part 1).  Consider the network shown below, and Dijkstra’s link-state algorithm. Here, we are interested in computing the least cost path from node E (note: the start node here is E) to all other nodes using Dijkstra's algorithm. Using the algorithm statement used in the textbook and its visual representation, complete the "Step 0" row in the table below showing the link state algorithm’s execution by matching the table entries (i), (ii), (iii), and (iv) with their values.  Write down your final [correct] answer, as you‘ll need it for the next question.
4. |z + 5 - 5i| = 7
14. dz, C: |z❘ C: |z❘ = 0.6 ze² - 2iz H
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
Binary Numbers and Base Systems as Fast as Possible; Author: Techquikie;https://www.youtube.com/watch?v=LpuPe81bc2w;License: Standard YouTube License, CC-BY
Binary Number System; Author: Neso Academy;https://www.youtube.com/watch?v=w7ZLvYAi6pY;License: Standard Youtube License