Question

Rewrite the following for loop into a whileloop. 1 2 3 4 int s = 0;...

Rewrite the following for loop into a whileloop.

1

2

3

4

int s = 0;

for (int i = 1; i <= 10; i++) {

   s = s + i;

}

Given variables int n and double pi, write a snippet of code that assigns to pi the approximation of π resulting from adding the first nterms in the Gregory-Leibniz series:

 

gregory-leibniz.pngGiven variables int areaBound and int sum, write a snippet of code that assigns to sum the result of adding up all integers of the form n2+ m2 where:

both n and m are at least 1, and

n2 < areaBound, and

m2 < areaBound.

n2 < areaBound, and

m2 < areaBound.

Modify the loop to approximate pi with the Gregory-Leibniz series so that, instead of adding a given number of terms, it keeps adding terms until the difference between two consecutive estimates is less than some predefined tolerance, say double epsilon = 0.0001.

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Rewrite the following for loop into a whileloop. 1 2 3 4 int s = 0;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Convert the following while loop into a for loop. int 1 - 50: int sum-07 while...

    Convert the following while loop into a for loop. int 1 - 50: int sum-07 while (sum < 1000) sum - sum + 1; Question 35 (2 points) Saved Given an int variable k that has already been declared, use a while loop to print a single line consisting of 80 dollar signs. Use no variables other than k. int sum = 0; for(int i = 100;sum < 10000;1-- sum = sum + i;

  • Refer to the following array definition for questions 1 & 2 int numberArray[9)[11] 1. Write a...

    Refer to the following array definition for questions 1 & 2 int numberArray[9)[11] 1. Write a statement that assigns 145 to the first column of the first row of this array. 2. Write a statement that assigns 18 to the last column of the last row of this array. values is a two-dimensional array of floats, with 10 rows and 20 columns. Write code that sums all of the elements in the array and stores the sum in the variable...

  • Consider the following loop nest: int sum = 0; for(int j = 0; j < N...

    Consider the following loop nest: int sum = 0; for(int j = 0; j < N * N; j += 2) for(int i = 2*N; i > 0; i--) sum++; What is the Big-O behavior? Group of answer choices O(1) O(log N) O(N) O(N log N) O(N2) O(N3) 2.Consider the following loop nest: int sum = 0; for(int j = 1; j < N; j *= 2) for(int i = 0; i < N; i += 2) sum++; What is...

  • in c++ language 1.Re-write the following for loop statement by using a while loop statement: int...

    in c++ language 1.Re-write the following for loop statement by using a while loop statement: int sum = 0; for(int i=0;i<=1000;i++){                 sum+=i; } 1 (b). Following is the main () function of a program. The program asks a user to enter 150 integers and print the largest integer user entered. int main() {    int score, highest;             //missing portion of the program             return 0;    } 1(c). Find the missing portion of the following code, so the...

  • 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...

  • 1.Given an int variable n that has been initialized to a positive value and, in addition,...

    1.Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a for loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Thus if n equals 4, your code should put 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 into total. Use no variables other than n, k, and total.(c++) 2.Given an int variable...

  • C++ programming: Write a function to sum the following series: m(i)= 1/2 + 2/3 + ......

    C++ programming: Write a function to sum the following series: m(i)= 1/2 + 2/3 + ... + i/i+1           // i is the number of item in the series double m(int i){ }

  • What does the following code output? int[] array = { 1, 4, 3, 6, 8, 2,...

    What does the following code output? int[] array = { 1, 4, 3, 6, 8, 2, 5); int sum = array[0]; Il scan the array for (int i=0; i < array.length; i++) { if ( array[i] > sum) sum = array[i]; 3 System.out.println( sum );

  • I need not in pic or handwriting, in keyboard Spring 19 (1) ESC 152 Programming with...

    I need not in pic or handwriting, in keyboard Spring 19 (1) ESC 152 Programming with MATLAB Section 2 1) The Taylor series expansion for cos(x) is: coS(x). where x is in radians. Write a MATLAB program that determines cos (x) using the Taylor series expansion. The program asks the user to type a value for an angle in degrees. Then the program uses a loop for adding the terms of the Taylor series. If a is the nth term...

  • Consider the following C+s code (call to addToArray not shown int at] - (1. 2, 3 yoid addr oArray Cint "a) int bt] (1, 2, 3) for Cint i o: i<2; 1++) In the data section, include set the...

    Consider the following C+s code (call to addToArray not shown int at] - (1. 2, 3 yoid addr oArray Cint "a) int bt] (1, 2, 3) for Cint i o: i<2; 1++) In the data section, include set the array size as arrsze using S and TYPE The code must include a call to addToArray, passing the appropriate argument(s). Your code MUST be implemented to mimic as close as possible the C++ code, including the local variable b in addTOArray...

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