Sarthaks Test
0 votes
in Programming by (15 points)
edited by

Write a pseudocode to solve s=(A+B+C)/Y

Please log in or register to answer this question.

1 Answer

0 votes
by (13.3k points)

Let’s assume the meaning of the statement is to compute the thing on the right and store the result into the thing on the left. (This matches most common high-level languages, but in some languages, this might just as easily have been a comparison rather than an assignment.) Let’s further assume that all the letters are variables, all of them have the same data type, the data types are numeric, and the +, /, and parentheses operators all have the typical algebraic meanings for numeric expressions. (In some languages and/or with some data types, the + might mean concatenation instead of addition, etc.)

If your goal is to implement the assignment statement in a common high-level language, the statement itself should be just fine as pseudocode:

  • S = (A + B + C) / Y

But the variable names here are not very descriptive. Both in pseudocode and in actual code, it would be best to use variable names that reflect what the variables actually contain. Since I don’t know what your formula represents, I won’t suggest specific names.

If you want to be explicit about the order in which everything happens, then a pseudocode description might look something like:

  • Compute the sum of A, B, and C, storing the result into T.
  • Compute the quotient T divided by Y, storing the result into S.

I introduced T as a temporary variable.

If your goal were to implement the assignment statement in an assembly language, you might want to break it down further into the individual steps, which would better map to your target language. In this case, the pseudocode might be something like:

  • ADD A and B, storing the result into T.
  • ADD T and C, storing the result into T.
  • DIVIDE T by Y, storing the result into S.

Or even more detail that gets even closer to what’s actually happening at the low level in your target system:

  • FETCH A into R1.
  • FETCH B into R2.
  • ADD R1 and R2, storing the result into R1.
  • FETCH C into R2.
  • ADD R1 and R2, storing the result into R1.
  • FETCH Y into R2.
  • DIVIDE R1 by R2, storing the result into R1.
  • STORE R1 into S.

I introduced two temporaries here, R1 and R2, which might map to CPU registers in the actual assembly language code.

Pseudocode can vary widely and be used for many different purposes. So, you need to figure out the goal of the pseudocode, and choose a level of abstraction and detail that serves that goal. And follow the pseudocode guidelines you’ve been given, if any.

By the way, writing pseudocode after your write the actual code is like writing an outline an outline after you've written the book report. The act of writing pseudocode has a lot less value, if you’ve already written the actual code.

Related questions

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.