Question

•Given the following code, what is the value of g[5]? The answer is 3.231994 please show...

•Given the following code, what is the value of g[5]? The answer is 3.231994 please show me how it is calculated each iteration.

   int i;

   double g[15] = {0.5, 1.5, 2.0, 2.8, 3.2, 0.01};

   double sum = 0;

   for(i = 0; i < 15; i++)

   {

   sum += g[i];

   }

   g[3] = 1.5 + g[0]*g[2];

   g[5] = g[4] + sin( g[4]*g[5]);

0 0
Add a comment Improve this question Transcribed image text
Answer #1

From the above code:

g[0] = 0.5

g[1] = 1.5

g[2] = 2.0

g[3] = 2.8

g[4] = 3.2

g[5] = 0.01

In for loop,in each iteration the elements in array g[ ] will add and store in sum.

iteration 1 : i = 0 , g[0] = 0.5 , sum = 0

sum += g[i] means sum = sum + g[i]

sum = sum + g[0] = 0 + 0.5 = 0.5

sum = 0.5

iteration 2 : i = 1 , g[1] = 1.5 , sum = 0.5

sum = sum + g[1] = 0.5 + 1.5 = 2.0

sum = 2.0

iteration 3 : i = 2 , g[2] = 2.0 , sum = 2.0

sum = sum + g[2] = 2.0 + 2.0 = 4.0

sum = 4.0

iteration 4 : i = 3 , g[3] = 2.8 , sum = 4.0

sum = sum + g[3] = 4.0 + 2.8 = 6.8

sum = 6.8

iteration 5 : i = 4 , g[4] = 3.2 , sum = 6.8

sum = sum + g[4] = 6.8 + 3.2 = 10.0

sum = 10.0

iteration 6 : i = 5 , g[5] = 0.01 , sum = 10.0

sum = sum + g[3] = 10.0 + 0.01 = 10.01

sum = 10.01

iteration 7 - 15 :g[6] to g[14] are not initialized so these have value = 0

So, sum will not change,sum = 10.01

g[3] = 1.5 + g[0] * g[2] = 1.5 + (0.5*2.0) = 2.5

g[3] = 2.5

g[5] = g[4] + sin( g[4] * g[5]) -> Assume this as equation 1

Here sin() function return the sine of a number.

g[4] = 3.2 , g[5] = 0.01

sin( g[4] * g[5]) = sin( 3.2 * 0.01) = sin(0.032) = 0.031995 .Substitute this value in  equation 1 ,Then

g[5] = 3.2 + 0.031994 = 3.231994

So g[5] = 3.231994

Add a comment
Know the answer?
Add Answer to:
•Given the following code, what is the value of g[5]? The answer is 3.231994 please show...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (C++) Given the following code, please help me answer these three parts and please explain as...

    (C++) Given the following code, please help me answer these three parts and please explain as simply as you can. 1.) How many iterations of the innermost loop? (please show calculation) 2.) What is the largest output? 3.) How many zeroes at the start? (please show/explain calculation) for (int y 0; y 〈 4; y++) for (int z 0: z 〈 5; z++) cout << x * y * z くく endl;

  • 1. (TCO 1) What is the output of the following C++ code? int list[5] = {0,...

    1. (TCO 1) What is the output of the following C++ code? int list[5] = {0, 5, 10, 15, 20}; int j; for (j = 0; j < 5; j++)      cout << list[j]; (Points : 4)         0 1 2 3 4         0 5 10 15         0 5 10 15 20         5 10 15 20 Question 2.2. (TCO 1) What is stored in alpha after the following code executes? int alpha[5] = {0}; int j; for (j = 0; j...

  • 4. Given the following code, int x = 5, n; do{ n = 0; for (int...

    4. Given the following code, int x = 5, n; do{ n = 0; for (int i = 0; i < x; i++) { n = n + x; System.out.println(n); } while (x > 1); a) What will be the value of x after the code segment is executed? Initial value of x 5 b) What is the output of the code segment? c) Explain how the flow of control moves in the given code segment. When explaining, show how...

  • Given the following pseudo-code, please show what the outputs will be at LINE A, B, C...

    Given the following pseudo-code, please show what the outputs will be at LINE A, B, C and D. please put down “inconclusive” if you think the CPU process scheduling could potentially result in multiple values for integer i. You will receive partial credit if you draw the process tree. int i = 0; int main(){                         if (fork() == 0) {                         i++;                         if (fork() == 0){                                     if (fork() == 0){                                                 i++;                                                 print value...

  • What are the errors in the following code? My professor gave us this prewritten code and...

    What are the errors in the following code? My professor gave us this prewritten code and asked us to find the errors in it so that it can run properly #include <cstdlib> #include <ctime> #include <iostream> using namespace std; // input: integer // output: none // adds five to the given parameter void addFive( int x ) { x += 5; } // input: none // output: a random number int generateRandomNumber() { srand( time(0) ); return rand() % 100;...

  • BSP2014 5. Given xe 20 Cr) 0 x<0 i) Show thatAx) is a pdf. ii) Find...

    BSP2014 5. Given xe 20 Cr) 0 x<0 i) Show thatAx) is a pdf. ii) Find the cumulative distribution function (CDF) of X 6. The p.d.fof a random variable Y is given by y +1 for2<y<4 v) 0 elsewhere Find i) the value of c ii) P(Y<3.2) iii)P(2.9 < γ< 3.2) 7. The p.d.fof a random variable X is given by elsewhere i) Find P(X<1.5) ii) Find P(0.5 X1.5)

  • Can someone explain me parts of this code I do not fully understand what its doing...

    Can someone explain me parts of this code I do not fully understand what its doing or what its purpose is. I have highlighted the parts I'm confused over. Please explain thoroughly. import java.util.Scanner; class A1Stats { public static void main(String args[]) { double min, max, sum, mean; int n; Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); String[] numbers = input.split(" "); double value[] = new double[numbers.length]; if(numbers.length > 0){ for (int i = 0; i < numbers.length;...

  • 4. (20%) Suppose you are given the below C code and the corresponding CUDA code: //...

    4. (20%) Suppose you are given the below C code and the corresponding CUDA code: // Invoke DAXPY with 256 threads per Thread Block host nt nbTocks (n 255) /256; /I Invoke DAXPY daxpy(n, 2.0, x, y) // DAXPY in vold daxPy(int n, double a, double .x, double "y) daxpyceenblocks, 256(n, 2.0,x.Y): DAXPY 1n CUDA device void daxpy(int n, double a, double ., double- for (int 1 -0:1n:1 int 1 blockidx.x blockDim.x threadIdx.x (1). (14%) Rewrite the CUDA code so...

  • Given the following code segment, int x = 20; int y = 7; what is the...

    Given the following code segment, int x = 20; int y = 7; what is the data type of the value of the expression (x % y) ? (3 points) Question 1 options: 1) int 2) double 3) numeric 4) Error: possible loss of precision 5) Unable to be determined Question 2 (3 points) Given the following code segment, double x = 42.3; double y = 11.7; what is the data type of the value of the expression (x %...

  • please help! im not sure how to answer these two questions! show work 1- Given that...

    please help! im not sure how to answer these two questions! show work 1- Given that an object of mass 6 kg, initial speed 2 m/s and final speed 9 m/s, find the total work done on the object. 2- The diagram below shows two 20 kg rods of the same 2.0-m length free to rotate about axes through the rods, as indicated. Find the torque for each case. Which rod experiences the greatest magnitude gravitational torque? I a. 0...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT