Question

In: Computer Science

I have to write an initial algorithm and a refined algorithm for the following problem: Display...

I have to write an initial algorithm and a refined algorithm for the following problem:

Display the square of numbers from 0 to some user inputted value. For example if the user enters 3 then the program will need to display the square of 0, 1, 2 and 3. Must use a counter loop. Must use 2 methods. One method that gets the number from the user and returns it. A second method is passed that number as a parameter then the method display the squares. So all main does is call the methods and gets the data from one method and passes it to the other.

I'm also suppose to use modules for this. I guess I'm just not sure how to even begin writing the initial algorithm down to the refined algorithm. Any help or explanation would be much appreciated, thank you!

This is what I have written so far:

Initial Algorithm:

Declare variables

Get user input

Set counter equal to 0

Initiate counter loop

Get squares of all numbers up until user’s input number

Display squares

Data Requirements:(I/O)

Input – user number

Output – square of numbers up until user’s input number

Formulas:

User input + user input * user input

         

Refined Algorithm:

Module Main()

         

          Declare int userNumber

         

         

          Get the user number

                   Display “Enter a number”

                   Input userNumber

         

          Calculate square of numbers up to user input number

                    Call CalculateSquares

           

            Calculate square of user input one number at a time

                   Call CalculateSquare

End Module Main

Module CalculateSquares

Counter = 1

While counter <= userNumber

  1. Set counter = counter + 1
  2. Display “The square of” + counter + “is” + counter * counter, EOL

End Module CalculateSquares

Module CalculateSquare

Solutions

Expert Solution

The initial algorithm what you have mentioned is seems to be correct.

The refined algorithm is as follows:

Module Main()
  DECLARE int num
  SET num = call getUserInput()
  call computeSquares(num)
End Module

Module getUserInput()
 Display "Enter a number"
 Input number
 return number
End Module

Module computeSquares(int inputNumber)
  DECLARE int counter
  SET counter = 0
  WHILE counter <= inputNumber
    call computeIndividualSquare(counter)
    INCREMENT counter
  ENDWHILE
End Module

Module computeIndividualSquare(int counter)
  Display "The square of "+counter+" is "+(counter*counter)
End Module

Explanation:

There are four functions in the refined algorithm:

  • getUserInput() function to get the input from the user and return it.
  • computeIndiviualSquare() function to compute a square of a number (Number has to be passed as parameter in this case counter variable). This function also used to display the output. If you wish you can create a separate module to display the output.
  • computeSquares() function to loop till counter, increment the value of the counter and call the function computeIndividualSquare to compute square of each value of counter
  • Main() module is used to call the two methods. One is getUserInput() an another one is computeSquares(). It passes the user input value to the computeSquares() module.

Related Solutions

Hello, I am writing the initial algorithm and refined algorithm for a program. I just need...
Hello, I am writing the initial algorithm and refined algorithm for a program. I just need to make sure I did it correctly. I'm not sure if my formula is correct. I appreciate any help, thank you! TASK: Write a program that will calculate the final balance in an investment account. The user will supply the initial deposit, the annual interest rate, and the number of years invested. Solution Description: Write a program that calculates the final balance in an...
Write an algorithm (flowchart) for the following problems: Take ten different numbers as input and display...
Write an algorithm (flowchart) for the following problems: Take ten different numbers as input and display the sum of there squares (SS). Example: let the inputs are: 3, 0, -1, 0, 9, -5, 6, 0, 2, 1 then 157 is displayed, because: SS=32+02+(-1)2+02+92+(-5)2+62+02+22+12=157
solution using stack Reversing a word or a sentence: Write an algorithm that will display a...
solution using stack Reversing a word or a sentence: Write an algorithm that will display a given word in a reverse order. - e.g. "Data Structures" will be "serutcurtS ataD".
C programming problem I have to design an efficient algorithm which solves this problem. Also, it...
C programming problem I have to design an efficient algorithm which solves this problem. Also, it needs to include time and space complexities of this algorithm. There is a matrix (N times N) of integers which rows and columns are sorted in non-decreasing order. A sorted matrix and integer M will be given and we need to find the position(row,column) of M in this matrix. it's okay to report only one position if there are more than one answers. when...
Show that the worst-case number of entries computed by the refined dynamic programming algorithm for the...
Show that the worst-case number of entries computed by the refined dynamic programming algorithm for the 0-1 Knapsack problem is in Ω(2n). Do this by considering the instance in which W = 2n −2 and wi = 2i−1 for 1 ≤ i ≤ n. Please answer with clear explanations!!! Excerpt From: Richard Neapolitan. “Foundations of Algorithms.”
how to write a genetic algorithm for a knapsack problem .
how to write a genetic algorithm for a knapsack problem .
Write an algorithm and draw a flowchart for the following problem: Create an adaptive cruise control...
Write an algorithm and draw a flowchart for the following problem: Create an adaptive cruise control for a car. Include functions for enabling/disabling the cruise control; setting up the speed. The system should always maintain the speed unless there is a slower car in front of it. Make sure to define inputs and outputs. Do not forget that computer’s microprocessor works in very small steps (tiny steps!), so be careful not to assign large number of steps to one flowchart...
/* my calculator is messed up. I need to display interest rate + initial investment. I...
/* my calculator is messed up. I need to display interest rate + initial investment. I keep getting huge numbers, Just need to fix my calculator thats all. import java.util.Scanner;//Import java scanner class public class InterestCalculator1 { //create fields / variables private double initialInvestment; private double annualInterestRate; private double numberOfYears; private double investmentReturn; private double finalAmount; Scanner input = new Scanner(System.in);//create new scanner object //constructor public InterestCalculator1(double initialInvestment, double annualInterestRate, double numberOfYears){ super(); this.initialInvestment = initialInvestment; this.annualInterestRate = annualInterestRate; this.numberOfYears...
Please write a Java algorithm solving the following problem: Implement a Java method to check if...
Please write a Java algorithm solving the following problem: Implement a Java method to check if a binary tree is balanced. For this assignment, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one. 1. First, please create the following two classes supporting the Binary Tree Node and the Binary Tree: public class BinTreeNode<T> { private T key; private Object satelliteData; private BinTreeNode<T> parent;...
Write an algorithm (flowchart OR Pseudocode) for the following problem Take input character from the user...
Write an algorithm (flowchart OR Pseudocode) for the following problem Take input character from the user unless he enters '$'. Thereafter display how may vowels were entered by the user. Also display the number of each vowel ('a', 'e', 'i', 'o' and 'u') separately. For example if the user enters B a b e c o o d i u g o a l $ Then we have the output below: #A=2 #E=1 #I=1 #O=3 #U=2
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT