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
Whentheuserenters!!,themostrecentcommandinthehistoryisexecuted.In the example above, if the user entered the command: Osh> !! The ‘ls -l’ command should be executed and echoed on user’s screen. The command should also be placed in the history buffer as the next command. Whentheuserentersasingle!followedbyanintegerN,theNthcommandin the history is executed. In the example above, if the user entered the command: Osh> ! 3 The ‘ps’ command should be executed and echoed on the user’s screen. The command should also be placed in the history buffer as the next command. Error handling: The program should also manage basic error handling. For example, if there are no commands in the history, entering !! should result in a message “No commands in history.” Also, if there is no command corresponding to the number entered with the single !, the program should output "No such command in history."
Activity No. Activity Time (weeks) Immediate Predecessors 1 Requirements collection 3 2 Requirements structuring 4 1 3 Process analysis 3 2 4 Data analysis 3 2 5 Logical design 50 3,4 6 Physical design 5 5 7 Implementation 6 6 c. Using the information from part b, prepare a network diagram. Identify the critical path.
2. UNIX Shell and History Feature [20 points] This question consists of designing a C program to serve as a shell interface that accepts user commands and then executes each command in a separate process. A shell interface gives the user a prompt, after which the next command is entered. The example below illustrates the prompt osh> and the user's next command: cat prog.c. The UNIX/Linux cat command displays the contents of the file prog.c on the terminal using the UNIX/Linux cat command and your program needs to do the same. osh> cat prog.c The above can be achieved by running your shell interface as a parent process. Every time a command is entered, you create a child process by using fork(), which then executes the user's command using one of the system calls in the exec() family (as described in Chapter 3). A C program that provides the general operations of a command-line shell can be seen below. #include #include #define MAX LINE 80 /* The maximum length command */ { int…
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.
Similar questions
SEE MORE QUESTIONS
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