The answer is B. Given a number 'n' we need to calculate the sum of the following series
which can further be simplified into the following form
To get the sum of the above series, we need to first find
compute the value of
and add it to the sum of the
, which is upto (n-1)th term.
Now, to get the sum of the above series, we need to first compute
the value of
and it to the sum of the remaining series
, which is upto (n-2)th term. And so on, we compute the value of
the entire series, in a recursive manner.
The following function does the same thing as discussed above. It
first computes the value of 1/(n * n) and add it to the sum of the
series upto (n-1)th term, computed in a recursive manner. Each time
you go on decrementing n by 1 until you reach 1. At this point you
will return 1 and all the terms starts up getting added.
double computePI(int number) {
if (number <= 1) {
return 1.0;
// This is the base case
}
return 1 / (number * number ) + computePI(number - 1);
}
below o A double coer utePI(int eurber) 1) return (number comput PI(number number) return (number return...
Suppose we have three variables declared as char *pc; int *pi; struct point {double x; double y;}; struct point *p1; struct point *p2[10]; Assume sizeof(char)=1, sizeof(int)=4, sizeof(double)=8. The values of pc, pi, p1 and p2 are 240, 258, 410 and 480 respectively. What are the values of pc+1, pi+2, p1+4 and p2+3
double sinLoops (double x) { int i = 7; //this didn't exist. double result = 1/(m+1) // line 3 double pi=3.14; //You need the individual taylor series element. do { double num = x; //these need to be reset each time. double fac = 1; //if not they overflow and infinity/infinity is NaN and it exits. for (int counter = 1; counter < (2 * i + 1); counter++) { } (2 * i + 1); counter2 >= 1; counter2--)...
Program 2 #include <iostream> #include <cmath> using namespace std; int main() { const double PI = 3.141592653; int degrees; double radians; cout << "Enter a value for the degree of an angle\n"; cin >> degrees; // translate the formula for converting degrees to radians into C++: // // degrees x PI // ------------ = radians // 180 radians = _____________________________________; // Refer to p. 127 for help below....
Given following method, public static double getQuotient (int num1, int num2){ return num1/(double) num2; } Write Java statement to invoke the method.
Consider the following program: # include <iostream> using namesapce std; void Func(int a, int bl double number-25.0: int main) f int x-18, y-20; cout<c"Before: x- kex<" and y-eyecendl; Fundxy 1// end of main void Funcfint a, int b) int sum a+b; a-200; b-300; numberanumber+1.0 Which of the statements below are correct? (Select only the correct answers. There may be more than one) D A The statement double number-25.0; declares a global variable number B. The variables x and y are...
Big-O notation for each
public static double accumulate (double[] a) { double sum = 0.0; for (int i = 0; i < a.length; i++) sum += a[i]; return sum; } public static double innerProduct(double[] a, double[] b) { // assume a. length == b.length double sum = 0; for (int i = 0; i < a.length; i++) sum += a[i] * b[i]; return sum; } public static int twoSum(int[] a) { int count = 0; for (int i = 0;...
c++
• return type function name (parameter); • Ex: int Joe(int, int); • Ex: void gg(int, float); • Ex: double Two_sol(double, double, double): 2. fC → function call • Ex: cout << Joe(); • Ex: Joe(5, 0); 3. fD → function definition return type function name of STATEMENT(S) return function name: **program84 Void functions to print "Welcome to the world of functions!" programs. To print the sum and product of 2 numbers using functions w/out parameters. program86: to print the...
typedef double (*price_fn)(void* obj); typedef double (*bulk_price_fn)(void* obj, unsigned int quantity); // Compares the price of obj1 with obj2 // Returns a negative number if the price of obj1 is less than the price of obj2 // Returns a positive number if the price of obj1 is greater than the price of obj2 // Returns 0 if the price of obj1 is equal to the price of obj2 int compare_by_price(Object* obj1, Object* obj2) { // IMPLEMENT THIS return...
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:
Given
variables int areaBound and int
sum, write a snippet of code that assigns to sum the result...
#include<iostream> using namespace std; double hey(int x){ if(x==2) return 1; int i=0; if (x%2==0){ while(i<=2){ cout<<(x/2)<<endl; i++; break; } } if (x%2==1){ while(i<=2){ i=3*x+1; cout<<i<<endl; } return 1; } } int main() { int n; cout<<"insert a number to check how many steps to reduce down to 2"<<endl; cin>>n; cout<<hey(n)<<endl; return 0; } I couldn't