kbjones81 has asked for the wisdom of the Perl Monks concerning the following question:

I have no idea how to do these problems. Any help would be greatly appreciated! Thanks!

  1. Write a Perl program to print the final values of variables G, H, and I. Create a flowchart for this program.

    Start G = 18 H = 2 I = 13 if H = 2 perform Change-GI endif while I > 7 perform Change-HI endwhile if G = 2 then perform Change-G endif if I = G then perform Change-G else perform Change-HI endif print G, H, I End Change-GI while I > 5 I = I - 3 G = 0 endwhile Return Change-HI H = H + 2 I = I - 4 Return Change-G G = G + 1 H = H - 1 Return
  2. Write a Perl program to print he final values of variables X, Y, and Z. Create a flowchart for the program. Submit via Blackboard (1) your Perl program, (2) your flowchart and (3) the runtime screen display.

    Start X = 3 Y = 2 Z = 12 while Z > 8 perform Change-YZ endwhile if X = 2 then perform Change-X endif if Z = 4 then perform Change-X else perform Change-YZ endif print X, Y, Z End Change-YZ Y =Y + 2 Z =Z - 4 Return Change-X X = X + 1 Y = Y - 1 Return

Formatting added by davido per consideration.

Replies are listed 'Best First'.
Re: Homework Problems
by planetscape (Chancellor) on Jul 17, 2006 at 00:18 UTC

    Kudos for letting us know right up front that this is homework. Show us what you have tried so far (to let us know you have made some effort), and what specific part is giving you problems, and help will flow freely.

    HTH,

    planetscape
Re: Homework Problems
by chromatic (Archbishop) on Jul 16, 2006 at 23:48 UTC

    Which part is giving you difficulty?

Re: Homework Problems
by Marza (Vicar) on Jul 17, 2006 at 03:42 UTC
    these are easy. The teacher has basically done the work for you.

    You are assigning values to variables. Check the variables to see if they match a value. If they do call a subroutine and return the value.

    That should get you on your way. To get more help, you will need to post some code.

    Remember to use strict and warnings; they will help you out

Re: Homework Problems
by GrandFather (Saint) on Jul 17, 2006 at 21:19 UTC

    A good first step is to add a little indentation and white space so you can more easliy see the control structures and flow of the program. A little like using sentence and paragraph structure in prose. Consider this:

    Start G = 18 H = 2 I = 13 if H = 2 perform Change-GI endif while I > 7 perform Change-HI endwhile if G = 2 then perform Change-G endif if I = G then perform Change-G else perform Change-HI endif print G, H, I End Change-GI while I > 5 I = I - 3 G = 0 endwhile Return Change-HI H = H + 2 I = I - 4 Return Change-G G = G + 1 H = H - 1 Return

    DWIM is Perl's answer to Gödel
Re: Homework Problems
by mikasue (Friar) on Jul 17, 2006 at 16:08 UTC
    I'm a beginner to perl also. This was a good problem for me to try. Here's my code. I left my debugging print statements in there. From what I observed, the G = 2 and the I = G blocks will never execute. G is set to 0 in the H = 2 block and never change to anything else. I and G never equal the same thing until the last else statement and the program doesn't loop back around that I = G statement again.
    use strict; use warnings; my $G = 18; my $H = 2; my $I = 13; print "Initial values\n"; print "g=$G\th=$H\ti=$I\n"; if($H == 2) { &ChangeGI($G, $I); } print "Values After H ==2\n"; print "g=$G\th=$H\ti=$I\n"; while ($I > 7) { &ChangeHI($H, $I); print "In while loop I > 7\n"; print "h=$H and i= $I"; } print "After while loop\n"; print "g=$G\th=$H\ti=$I\n"; if($G == 2) { &ChangeG($G, $H); } print "After G ==2\n"; print "g=$G\th=$H\ti=$I\n"; if($I == $G) { &ChangeG($G, $H); } else { &ChangeHI($H, $I); } print "After IF I== G\n"; print "g=$G\th=$H\ti=$I\n"; print "The final results are G\: $G and H\: $H and I\: $I\n"; ####################### sub ChangeGI { while($I > 5) { $I = $I - 3; $G = 0; } } sub ChangeHI { $H = $H + 2; $I = $I - 4; } sub ChangeG { my ($G, $H) = @_; $G = $G + 1; $H = $H -1; } #Initial values #g=18 h=2 i=13 #Values After H ==2 #g=0 h=2 i=4 #After while loop #g=0 h=2 i=4 #After G ==2 #g=0 h=2 i=4 #After IF I== G #g=0 h=4 i=0 #The final results are G: 0 and H: 4 and I: 0