Question

In: Computer Science

Examples Example 1: Write pseudo code that reads two numbers and multiplies them together and print...

Examples


Example 1: Write pseudo code that reads two numbers and multiplies them together and print out their product. Example 2: Write pseudo code that tells a user that the number they entered is not a 5 or a 6.


Example 3: Write pseudo code that performs the following: Ask a user to enter a number. If the number is between 0


and 10, write the word blue. If the number is between 10 and 20, write the word red. if the number is between


20 and 30, write the word green. If it is any other number, write that it is not a correct color option. Example 4: Write pseudo code to print all multiples of 5 between 1 and 100 (including both 1 and 100). Example 5: Write pseudo code that will count all the even numbers up to a user defined stopping point. Example 6: Write pseudo code that will perform the following.


a) Read in 5 separate numbers.


b) Calculate the average of the five numbers.


c) Find the smallest (minimum) and largest (maximum) of the five entered numbers.


d) Write out the results found from steps b and c with a message describing what they are


Homework 1: Write pseudo code that reads in three numbers and writes them all in sorted order.


Homework 2: Write pseudo code that will calculate a running sum. A user will enter numbers that will be added to the


sum and when a negative number is encountered, stop adding numbers and write out the final result


Solutions

Expert Solution

Pseudocode is an informal way of communicating your algorithm for any computation problem that does not require any specific knowledge of syntax of any programming language. As We know, Once you know the algorithm, you can implement in with any programming language. Many interviewers ask for pseudocode rather than asking to write a program with a proper language syntax. This way they want to check your problem solving skills.

For the below pseudocodes // and /*---*/ represent comments.
Example 1:
Begin:
   Read: x , y // Read two numbers in x and y
   Set: Product = x * y
   Print: product
End

Example 2:
Begin:
   Read: x
   if x != 5 or x != 6 then // != represent not equal
       Print: It's not a 5 or 6
   else
       Print: It's a 5 or 6
End

Example 3:
Begin:
   Read: x
   if x >= 0 and x < 10 then
       Print: Blue
   else if x >= 10 and x < 20
       Print: Red
   else if x >= 20 and x < 30
       Print: Green
   else
       Print: Not a correct color option
End      

Example 4:

/* Start from 5 to 100 and increment by 5 everytime and print it. We could have started the loop from 1 as well and incrementing it 1 each time and check if it is a multiple of 5 or not, but that process will be slow*/
Begin:
   for i = 5 to 100 by 5
       Print: i and goto new line
End  

Or,
Begin:
   for i = 5; i<=100; i = i + 5
       Print: i and goto new line
End  

Example 5:
Solution (a)
Begin:
   Read: x
   Initialize: count_of_even = 0
   for i=2 to x by 2
       count_of_even = count_of_even + 1
   Print: count_of_even
End
Solution (b)
Begin:
   Read: x
   Initialize: count_of_even = 0
   for i = 2; i <= x; i = i+1
       if i % 2 == 0
           count_of_even = count_of_even + 1
   Print: count_of_even
End

Example 6:
Begin:
   // Suppose A[0:4] represent an array of 5 numbers and Array index starts from 0 thus it will be A[0], A[1], A[2], A[3], A[4]
   Initialize: A[0:4]
   for i = 0 to 4 by 1:
       Read: A[i]
   Initialize: sum = 0, minimum = A[0], maximum = 0
   for i = 0 to 4 by 1:
       sum = sum + A[i]
       if A[i] < minimum
           minimum = A[i]
       if A[i] > maximum
           maximum = A[i]
   Print: "Average will be": sum/5
   Print: "Minimum of Array": minimum
   Print: "Maximum of Array": maximum
End

Homework 1:
Begin:
   Read: a, b, c
   // Since there are three numbers We can just check and swap their positions based on their values
   if a > b
       swap a <=> b
   if a > c
       swap a <=> c
   // By this time you know that minimum number would have been assigned to a, now we just need to compare b and c
   if b > c
       swap b <=> c
   Print: a, b, c
End

Homework 2:
//Below pseudocode implements a while loop which means keep continuing the loop until your condition is satisfied
Begin:
   Initialize: sum=0
   Read: x
   while x >= 0:
       sum = sum + x
       Read: x
   //while loop will exit as soon as you enter a number which is less than 0. It will not even go inside while loop if the first number you enter is 0.
   Print: sum  
End


Related Solutions

Write a c++ code that prompts the user to enter three float numbers and print them...
Write a c++ code that prompts the user to enter three float numbers and print them in acsending order
(Do the algorithm and flowchart) Write a C++ program that reads integer numbers and print it...
(Do the algorithm and flowchart) Write a C++ program that reads integer numbers and print it in horizontal order of the screen
I am to write a code that uses two queues, and print the generated random numbers...
I am to write a code that uses two queues, and print the generated random numbers and content of both queues. I need to use the enqueue and dequeue functions in the code.  The instructions are below. Program should be in C Instructions: Generate n random numbers with values between 10 - 100. Note: n>9 if u write a function (for example, called generateRand) to do this - what is the data or input we have to give it? Create a...
1. Here is a pseudo-code of a program that takes two non-negative numbers X and Y...
1. Here is a pseudo-code of a program that takes two non-negative numbers X and Y as its inputs and outputs a number Z. Write this program in C. What does this program do? Explain how it computes the output Z from its inputs X and Y. Determine the big-Oh of this program in terms of its input sizes.                                                                 f (int x, int y) {      z = 0; k = y; w = x;      while (k != 0) {           if...
Write assembler code to print even numbers from 1-20. Submit code and screenshot. I am coding...
Write assembler code to print even numbers from 1-20. Submit code and screenshot. I am coding in NASM on a SASM system.
Write a program that reads numbers from scanf1 (keyboard) and then sums them, stopping when 0...
Write a program that reads numbers from scanf1 (keyboard) and then sums them, stopping when 0 has been entered. Construct three versions of this program, using the while, do-while, and for loops.
Fill in the blanks of this code to print out the numbers 1 through 7. number...
Fill in the blanks of this code to print out the numbers 1 through 7. number = 1 while number ___ 7:     print(number, end=" ")     ___
Write a JavaScript program with a function named fives that reads two numbers from two text...
Write a JavaScript program with a function named fives that reads two numbers from two text fields and then outputs to a div "True" if both of the numbers are greater than 5 or if their sum is greater than 20. Otherwise your function should output "False" to the div. If you wish, you may use the following HTML code to begin your program.
write a code using c++. Find the first 10 prime numbers and store them in an...
write a code using c++. Find the first 10 prime numbers and store them in an array.
Design in pseudo code a multiple recursive version of Tetranacci calculators. Tetranacci numbers are a more...
Design in pseudo code a multiple recursive version of Tetranacci calculators. Tetranacci numbers are a more general version of Fibonacci numbers and start with four predetermined terms, each term afterwards being the sum of the preceding four terms. The first few Tetranacci numbers are: 0, 0, 0, 1, 1, 2, 4, 8, 15, 29, 56, 108, 208, 401, 773, 1490, …
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT