Question

Hi, I need help with this practice problem!

Chapter 2 Algorithm Discovery and Design 64C FIGURE 2.10 Get values for a and b If (either a 0orb 0) then Set the value of pr

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

First i'm uploading the manual workout of above algorithm for above questions:

SHEET No._T ANITS Els 2lsc pa* od Cauat val ANIL NEERUKONDA INSTITUTE OFTECHNOLOGY & SCIENCES

Oop - Produe Cort. Product productワ product+ 2 Count Counttl ProdetPrdet- As Ceont be cam<c Yoduct valud

ause erld SHEET No. ANITS AS Givea in abave algarithm ik Ast rm piints fad sLor have in ele pad it checks cawaLithb,valul ANIElse Counb。 roduct-a if Ca >) Covnt Count+ Preduets produe+a tails for Some Examples as esc seen in thirs jston Case2 . As inANITS Pv Ottca Moltipli Catión elsc Praduet:o else Count- Countt ( ht produek

------------------------------------------------------------------------------------------------------------------------------------------------------------------

Now i will upload c code what the above algorithm gives for above inputs

#include <iostream>
using namespace std;
int main(){
   int a,b;
   cin>>a>>b;
   int product;
   int count;
   if(a==0 || b==0){
       product=0;
   }
   else{
       product=0;
       count=0;
       while(count<b){
           product=product+a;
           count=count+1;
       }
      
   }
   cout<<product;
   return 0;
}

1)output

4 Process exited aftr 1.446 seconds with return value Press any key to continue . - -

2)output

Process exited after 2.018 seconds with return value 0 Press any key to continue . - .

3) case 1 output

C:Usersluser Desktopx.exe -2 4 8 3.88 seconds with Process exited after Press any key to continue . -- return value

3) case 2 output

İİ] CAUserslusenDesktopx.exe 2-4 Process exited after 8. Press any key to continue - . - 186 seconds with return value 0

4) new code :

#include <iostream>
using namespace std;
int main(){
   int a,b;
   cin>>a>>b;
   int product;
   int count;
   if(a==0 || b==0){
       product=0;
   }
   else{
       product=0;
       count=0;
       if(b>0){
      
       while(count<b){
           product=product+a;
           count=count+1;
       }
   }
   else if(a>0){
       while(count<a){
           product=product+b;
           count=count+1;
}
   }
}
   cout<<product;
   return 0;
}

output

İ CAUserslusenDesktopx.exe 8 Process exited after 2.626 seconds with return value Press any key to continue . . -

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

Add a comment
Know the answer?
Add Answer to:
Hi, I need help with this practice problem! Chapter 2 Algorithm Discovery and Design 64C FIGURE...
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
  • Exercise 1 Use Top-Down Design to “design” a set of instructions to write an algorithm for...

    Exercise 1 Use Top-Down Design to “design” a set of instructions to write an algorithm for “travel arrangement”. For example, at a high level of abstraction, the algorithm for “travel arrangement” is: book a hotel buy a plane ticket rent a car Using the principle of stepwise refinement, write more detailed pseudocode for each of these three steps at a lower level of abstraction. Exercise 2 Asymptotic Complexity (3 pts) Determine the Big-O notation for the following growth functions: 1....

  • This is all I have 2. Consider the following approximation algorithm for the bin packing problem....

    This is all I have 2. Consider the following approximation algorithm for the bin packing problem. Algorithm Last Fit(1,S) Input: Set I of items and set S of item sizes; item I; E I has size S; Output: A packing of I into unit size bins B {} for each item I; e I do { if I; fits in one of the bins of B then Put I; in the last bin where it fits else { Add a...

  • 4.1 4.1 Insertion Sort 4. Design 137 the a algorithm for generating the power set of...

    4.1 4.1 Insertion Sort 4. Design 137 the a algorithm for generating the power set of a set of n elements. (The power set of a set s is the set of all the subsets of S,including empty set and S itself.) 5. Consider the following algorithm to check connectivity of graph defined by adjacency a ALGORITHM Connected (A 0...n-1, 0..n ij) Input: Adjacency matrix Alo..n 1,0. n -1) of an undirected graph G //Output: 1 (true) if G is...

  • 3. Write the function find sorted elt whose input is a vector sorted in increasing order...

    3. Write the function find sorted elt whose input is a vector sorted in increasing order and an element x. The output is 0 if the element x does not occur in v, 1 if the element x does occur in v. Below is the algorithm you should implement, known as the binary search algorithm. Note that the code below returns the index, but we want to return true or false, not the index, so adjust your code accordingly. Algorithm....

  • SHELL SCRIPT: Provide the following questions with the codes and command line Problem 5: Take 5...

    SHELL SCRIPT: Provide the following questions with the codes and command line Problem 5: Take 5 input arguments in a for loop, but only add the even i values to sum and display the result. Problem 6: Write a shell script that accepts path of the directory and returns a count of all the files within the specified directory. (Hint: use alias to perform cd operation) Problem 7: Write a shell script that takes an ‘count’ value as an input...

  • Problem 2 We have seen in class an algorithm for the design of state feedback controller using po...

    Problem 2 We have seen in class an algorithm for the design of state feedback controller using pole placement for multi-input systems. Consider the system-A Bu with 0 0 4 1. Using the algorithm seen in class, design a state feedback control K, or the gain K, to place the closed loop poles at-2,-3,-4. 2. Exploiting the structure of A and B, find a different feedback gain that place the poles in the same location. This steps shows that there...

  • Practice Problem [no points]: You should read and understand Fibonacci algorithm, write a code in a...

    Practice Problem [no points]: You should read and understand Fibonacci algorithm, write a code in a high level language of your choice and in Assembly to find a Fibonacci number and check your result. At the end of this assignment a possible solution to this problem is given both in Python and MIPS assembler. Note that it would have been possible to write the Python differently to achieve the same function. . [20 points] Write and debug a MIPS program...

  • Problem 5. (Lexicographical Optimisation with Paths) Provide pseudocode and an expla- nation for an algorithm that comp...

    Problem 5. (Lexicographical Optimisation with Paths) Provide pseudocode and an expla- nation for an algorithm that computes a path between two nodes in an undirected graph such that: . The maximum weight in the path is minimised, ie., there does not exist another path with a smaller maximum weight .Amongst all such paths, it finds the path with minimum cost. . The time complexity is no worse than 0(( and V is the set of nodes. ·IvD-log(IVD), where E is...

  • Hi, I would appreciate help with a problem from my Algorithms class. Thanks! *1- This is...

    Hi, I would appreciate help with a problem from my Algorithms class. Thanks! *1- This is the Euclid algorithm that computes the greatest common divisor of two non-negative integers a and b assuming that a > b >= 0. Euclid(a, b)        if b = 0                          then return a                  else Euclid(b, a mod b) Is this algorithm efficient? If so, what is the reason? If not, why not? Explain your opinion. *2- Prove that both versions of the knapsack problem possess...

  • Question 4 CLO3 The following Python script implements an algorithm to find and prints the max value in a list of values. MAX 0 def MaxVal (Ist): for i in Ist: if( MAX < i): MAX = i return (MA...

    Question 4 CLO3 The following Python script implements an algorithm to find and prints the max value in a list of values. MAX 0 def MaxVal (Ist): for i in Ist: if( MAX < i): MAX = i return (MAX) Marks (20,24,26,19,5,31,24,32,32,45 print (MaxVal (Marks) a. Express the number of operations in terms of a function f(n), where n is the input size. (1 Mark) b. What is the total number of operations in the for loop of the algorithm...

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