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...
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 .
/* 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
To write a C++ program for following scenario and display requirements: Scenario-based Problem: AIG Insurance wants...
To write a C++ program for following scenario and display requirements: Scenario-based Problem: AIG Insurance wants to create an insurance management system for their clients. The insurance management system will compute the required payments from the clients. The commission of the agent shall also be computed which depends on the amount of insurance type. Insurance Type Amount of Insurance type Agent Commission Life 2500 12.5% of amount Health 1500 10.5% of amount Other inputs 0 0 Computation of monthly payments...
Design an algorithm that will read an array of 200 characters and display to the screen...
Design an algorithm that will read an array of 200 characters and display to the screen a count of the occurrences of each of the five vowels (a, e, i, o, u) in the array. Your string is: The quick brown fox jumped over the lazy dog That is the question for my pseudo code question, I don't know if I have this correct can i have someone look this over and finish this and explain what i needed to...
Write VHDL code for the following: Use HEX-to-seven segment display converters to display the inputs and...
Write VHDL code for the following: Use HEX-to-seven segment display converters to display the inputs and results for a 4-bit adder. The inputs are unsigned 4-bit binary numbers. The outcome is a 4-bit binary adder with LED display. First you need to create a symbol for the HEX-to-seven segment display converter. Then implement a 4-bit adder using VHDL. Finally, connect three HEX-to-seven segment display converters to display input X, input Y, and sum S.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT