Anyone can help me to change it to pseudocode??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
   double x;
   int n;
   cout << "x: ";   cin >> x;
   cout << "n: ";   cin >> n;
   double answer = 1;
   for ( int i = 2, s = 1; i <= n; i++, s = -s) answer += s * tgamma( i + 1.0 ) / ( x - i );
   cout << "Answer: " << answer << '\n';
}
can anyone change "Lastchance" code to pseudocode?
Last edited on
KingOcean34 wrote:
can anyone change my code to pseudocode?


YOUR code? Pardon me!
http://www.cplusplus.com/forum/general/254113/#msg1116510
sorry bro its your code.. i was just in hurry.
im really sorry again. i didnt mean to.
can anyone change "Lastchance" code to pseudocode?

Do you understand what that code actually does, and how it works?

Because if you do, representing it as pseudocode should be straightforward.

Why are you even turning real code into pseudocode? That's a weird thing to do. The whole point of pseudocode is as an intermediate step in designing software. It's about taking human-language requirements, and expressing the steps needed to fulfill those requirements in a way that has the structure of code, but uses human natural language rather than actual code.

If you understand the code, then you should already be able to:

(a) describe in natural language, the operations it performs
(b) describe the structure of those operations

So... do you understand that code?
IM looking for help here...
does anybody willing to help?
I'm trying to help, by understanding your problem, and providing you with explanations of what pseudocode is and how it relates to the code.

I asked you a question. Any chance you could do us the courtesy of answering it?
you are going backwards. first you write p-code, then you write real code. going the other way serves no purpose.

pcode has no set format. so the easy thing to do here is to de-c++ify it.
that is, make things like cin english like 'read' and << goes away:
so the I/O around x becomes:
read(x)
write(x)
; is optional but you can lose them in pcode.
also change c cryptic for loop to human readable. its doing a bunch of stuff, move the i++ out of the loop definition, move the variable declares for i and s out, move the modification statments out...
i = 2
s = 1
while(i <=n)
{
i = i+1 //etc
}

basically, just get rid of the nuances of c++ like the weird for loop syntax and get it into something any programmer could read and understand with no language specific weirdness left.
++ and += type stuff is not universal. just put it in explicit form.
Last edited on
i understand the code.. the line 12 in that code how to change to pseucode or even flowchart will do.. thats where im stuck. Thanks for helping.
12
13
14
15
for (int i = 2, s = 1; i <= n; i++, s = -s)
   {
      answer += s * tgamma(i + 1.0) / (x - i);
   }

Now, what part are you not understanding? tgamma()?

http://www.cplusplus.com/reference/cmath/tgamma/
well you change the loop as I already said, and then the body becomes
answer = answer + s * tgamma(i + 1.0) / (x - i);

where you need to write or explain what tgamma is, as not all languages have that either.
depending on the audience of your pcode, what you do there could vary from just using the word (everyone in your audience knows what it is) to writing it as a pcode routine yourself with comments saying what it is and why you used it.

be very clear, are you having trouble understanding what is expected in pseudocode, or do you not know what the math here is doing? Pcode really should not be that hard to grasp -- its just words that look a bit like code that bridge the gap between pure english or pure math of the algorithm and language details (like c's complicated for loops). If nothing else, just rewrite it in BASIC -- its close to compiled pseudocode if you do not get too exotic.
Last edited on
line 12 in that code how to change to pseucode or even flowchart will do
A flowchart for a for() loop? Could help, but maybe following makes it clearer:

https://latex.codecogs.com/gif.latex?%5Cdpi%7B150%7D%201&plus;%5Csum_%7Bi%3D2%7D%5E%7Bn%7D-1%5Ei%5Ccdot%20%5Cfrac%7B%5CGamma%20%281&plus;i%29%7D%7Bx-i%7D%20%3D%201%20&plus;%20%5Cfrac%7B%5CGamma%281&plus;2%29%7D%7Bx-2%7D%20-%20%5Cfrac%7B%5CGamma%20%281&plus;3%29%7D%7Bx-3%7D%20&plus;%20%5Csum_%7Bi%3D4%7D%5E%7Bn%7D-1%5Ei%5Ccdot%20%5Cfrac%7B%5CGamma%20%281&plus;i%29%7D%7Bx-i%7D

To the right of the equal sign is ment as an example for n>3.

Edit: A single check only -- for x=9 and n=6 I get as result 7528/35
Last edited on
Topic archived. No new replies allowed.