Question

Given the function definition, which of the following are correct? int func(int n, double d) {...

Given the function definition, which of the following are correct? int func(int n, double d) { int j = n; double sum = 0; while( j >= 0) { sum += d; -j; } return sum; } It compiles but computes none of these returns 7+2 returns 7! returns 7*2

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

ANSWER-- IT WILL COMPILE BUT COMPUTES NONE.

The reason is that when code is executing

sum += d;

-j;

We are just doing an operation with j that is -j

means if j is 7 then -7 but we are not saving it in j itself.So j will be 7 all time and the loop will run infinite number of times and it will never come out of while loop and hence sum will never be returned.

Proof of infinite loop

THANK YOU !!

Add a comment
Know the answer?
Add Answer to:
Given the function definition, which of the following are correct? int func(int n, double d) {...
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
  • Question 111 pts Given the method definition: public int spin( int n ) { if (...

    Question 111 pts Given the method definition: public int spin( int n ) { if ( n <= 1 ) return 0; if ( n % 2 != 0 ) return spin( n - 1 ); return n + spin( n - 2 ); } What is the value of the following expression? spin( 7 ) 0 7 10 12 15 Flag this Question Question 121 pts What does the spin() method do? public int spin( int n ) {...

  • 8. Given the following code fragment and function definition, what is(are) the output(s)? int funct (int...

    8. Given the following code fragment and function definition, what is(are) the output(s)? int funct (int n) int var = 1; while(n > 0) cout << funct(7)<<" "<<funct(0); var *= n; n--; return var;

  • Consider the following program: # include <iostream> using namesapce std; void Func(int a, int bl double...

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

  • a) Make a function which returns x/3 with a given integer x. double third(int x) {...

    a) Make a function which returns x/3 with a given integer x. double third(int x) { //Complete your code below. ______________________________________ return y; } b) Make a program which shows 2 ∗ n with a given user input n. #include<iostream> using namespace std; // Complete the blank. _______________________ int main(){ int n; cout<<"Enter:"; cin>>n; cout<<twice(n); return 0; } int twice(int x){ return 2*x; } c) Make a function which returns true if n is positive, otherwise returns false. ________ positive(int...

  • 3. Recursive Program (6 points) Consider the following recursive function for n 1: Algorithm 1 int...

    3. Recursive Program (6 points) Consider the following recursive function for n 1: Algorithm 1 int recurseFunc(int n) If n 0, return 1. If n 1, return 1 while i< n do while j <n do print("hi") j 1 end while i i 1 end while int a recurse Func(n/9); int b recurse Func (n/9) int c recurse Func (n/9) return a b c (1) Set up a runtime recurrence for the runtime T n) of this algorithm. (2) Solve...

  • QUESTION 10 Which of the following is the correct definition of a function that calculates the...

    QUESTION 10 Which of the following is the correct definition of a function that calculates the area of a rectangle by multiplying its length by its width? The length and width parameters are double values and the function returns a double. double area (double length, double width) return lengthwidth double area( double length, double width) double area length width return double area(double length, width) return length width area(double length, double width) return length width; QUESTION 11 What is the output...

  • 2) Consider the following C function: ----------------------------------------------------------------------------- int vdiv(double * a, double * b, double *c,...

    2) Consider the following C function: ----------------------------------------------------------------------------- int vdiv(double * a, double * b, double *c, int siz) {// divide vectors element-wise: c=a/b; // replaces divide by almost 0 with 1e99 and returns count int result=0; int i=0; 1: while(i<siz) 2: { if( abs(b[i])<1e-10 ){ // close to 0 3: c[i]=1e99; 4: result++; 5: }else{ 6: c[i]=a[i]/b[i]; 7: } 8: i++; 9: } 10: return result; } -------------------------------------------------------------------------- Executable lines are numbered. -Find all basic blocks, -draw control flow graph...

  • What is the selection that best describes the following statement: int function( int, double [ ] ...

    What is the selection that best describes the following statement: int function( int, double [ ] ); A) function prototype B) function call C) function definition D) none of the above

  • Consider the following function definition and variable declarations: void square(int &n){n= n*n;} int arr[] = {1,...

    Consider the following function definition and variable declarations: void square(int &n){n= n*n;} int arr[] = {1, 2, 3}; int number = 4; Which of the following function calls are acceptable? (can have multiple answer) a.square(1); b.square(2); c.square(arr[number]); d.square(number); What is the output of the following code segment? int arr[] = {1, 4, 1, 0}; for (int i=0; i < 4; ++i)     cout<<arr[i]*2; a.1014 b.1 4 1 0 (space in between each number) c.1410 d.0140 e.None of the above Given...

  • Convert the C program into a C++ program.Replace all C input/output statements with C++ statements (cin,...

    Convert the C program into a C++ program.Replace all C input/output statements with C++ statements (cin, cout, cin.getline) . Re-make the func function by the following prototype: void func( double, double &); #include int user_interface(); double func(double); void print_table(); int main (int argc, char *argv[]) {    print_table(user_interface());    return 0 ; } int user_interface(int val){    int input = 0;    printf("This function takes in x and returns an output\n");    printf("Enter Maximum Number of X:");    scanf("%d", &input);...

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