Converting pseudocode to C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • asc173
    New Member
    • Sep 2007
    • 2

    Converting pseudocode to C++

    can anyone tell me how to write this in normal c++? thanks!

    input a value to factor
    while input value > 1

    let residual = input value
    let factor = 2
    while factor <= residual
    while factor divides residual
    (i.e. remainder = 0)
    output factor
    divide residual by factor
    increment factor

    input next value to factor

    as well as this:

    open an input file named "polytest" (or "polytest.t xt")
    while end of file not yet reached

    input number of vertices in the polygon
    assume first vertex is the origin (0,0)
    input second vertex
    determine vector from origin to second vertex
    for each additional vertex in the data
    input next vertex
    determine vector from last vertex to new vertex
    determine whether vector is a left or right turn
    determine vector back to the origin
    and whether it is a left or right turn
    if all turns are left turns, or all are right turns
    the polygon is convex
    else the polygon is concave
  • hsn
    New Member
    • Sep 2007
    • 237

    #2
    THIS IS an easy question
    you have to work a little hard to solve it
    you question is the answer
    just convert the lines that you posted to a c++ code

    and believe me it is very easy just think a little harder

    GOOD LUCK

    Comment

    • Laharl
      Recognized Expert Contributor
      • Sep 2007
      • 849

      #3
      What specific areas are giving you a problem?

      If the answer is everything, you've got a good algorithm base, but if you fill in the details, it'll go a long way towards helping you implement it.

      Comment

      • asc173
        New Member
        • Sep 2007
        • 2

        #4
        i'm just trying to figure out the spot where to put the loops
        here's what i got so far:


        cout <<"Please input number to be factored:" <<
        cin >> number >> endl;
        while (number>1){

        res = number;
        factor = 2;
        rem = (res/factor)
        while (factor <= res){
        while ((res/factor), rem ==0){
        cout << factor <<;

        Comment

        • Laharl
          Recognized Expert Contributor
          • Sep 2007
          • 849

          #5
          That looks good, except that the third while loop needs proper syntax. The % operator (modulus) finds the remainder, eg 1 % 2 = 1, because 1/2 = 0x2 + 1.

          Note that you can't input an endl, that's an output flag. Just move it to after the dangling << operator, like this:

          [code=cpp]
          cout << "Enter the number." << endl;
          cin >> number;
          [/code]

          Also, you need to have some sort of increment/decrement operation as part of the loop, as the algorithm you're using says. Basically, when a variable's size is being used as part of a while loop, you need to change that value, otherwise you'll get an infinite loop...

          You're on the right track, just keep following your algorithm and you'll get there just fine.

          Comment

          Working...