Pesudocode to C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Fakhir
    New Member
    • Jan 2023
    • 1

    Pesudocode to C++

    Hi, I need help in converting the pesudocode into c++ code.


    Initialize key = ROLLNO, extra = NAME_POSITION
    Start the iterator with value 1
    If the iteration number is greater than 5, go to step 12
    if the ROLLNO is greater or equal to 50
    add five to counter
    subtract 2 from extra
    go to step 6
    Otherwise
    Double the value of key
    Add 3 to extra
    Go to step 8
    If the ROLLNO is greater than 74
    Add 25 to the key
    Multiply the extra by 2
    Go to step 10
    Otherwise
    Add ROLLNO to the key
    Subtract extra from key
    Go to step
    If ROLLNO is less than 25
    Divide key by 3
    Add 4 to extra
    Go to step 10
    Otherwise
    Add 6 to both key and extra
    Go to step 10
    Add 1 to iterator
    Go to step 3
    Output key, extra
    Stop
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 656

    #2
    Hi, I need help in converting the pesudocode into c++ code.


    Initialize key = ROLLNO, extra = NAME_POSITION
    Start the iterator with value 1
    If the iteration number is greater than 5, go to step 12
    if the ROLLNO is greater or equal to 50
    add five to counter
    subtract 2 from extra
    go to step 6
    Otherwise
    Double the value of key
    Add 3 to extra
    Go to step 8
    If the ROLLNO is greater than 74
    Add 25 to the key
    Multiply the extra by 2
    Go to step 10
    Otherwise
    Add ROLLNO to the key
    Subtract extra from key
    Go to step
    If ROLLNO is less than 25
    Divide key by 3
    Add 4 to extra
    Go to step 10
    Otherwise
    Add 6 to both key and extra
    Go to step 10
    Add 1 to iterator
    Go to step 3
    Output key, extra
    Stop
    What have you done so far?

    Comment

    • BloomS
      New Member
      • Jan 2023
      • 8

      #3
      Code:
      // C++ Code
      
      int key = ROLLNO;
      int extra = NAME_POSITION;
      
      for (int i = 1; i <= 5; i++) {
          if (ROLLNO >= 50) {
              key += 5;
              extra -=2;
          } else {
              key *= 2;
              extra += 3;
              
              if (ROLLNO > 74) {
                  key += 25;
                  extra *= 2;
              } else {
                  key += ROLLNO;
                  key -= extra;
                  
                  if (ROLLNO < 25) {
                      key /= 3;
                      extra += 4;
                  } else {
                      key += 6;
                      extra += 6;
                  }
              }
          }
      }
      
      cout << key << " " << extra << endl;

      Comment

      Working...