Question

In *PERL* Use a loop structure and code a program that produces the following output (Take...

  1. In *PERL* Use a loop structure and code a program that produces the following output (Take one parameter to let user specify how many lines need to be printed):

A
AA
AAA
AAAB

AAABA

AAABAA

AAABAAA

AAABAAAB

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Executable Perl Code:

# Number of A in a block
$numA = 3;

# Number of B in a block
$numB = 1;

# Variable for loop
$val_i = 1;

# Output
$out = "";

# Variable for loop
$val_j = 1;

# Number of space to be printed
$spaceAvail=0;

# Loop for number of lines
while ($val_i <= $ARGV[0])
{
while ($val_j <= $val_i)
{
# Whwn new block starts
if ($numA == 0 && $numB == 0)
{
$numA = 3;
$numB = 1;
}
# Output A
if ($numA > 0)
{
$out.= "A";
$numA--;
}
# Output B
else
{
$out.= "B";
$numB--;
$spaceAvail++;
}
$val_j++;
}
# Print output
print($out . "\n");
  
# Print required spaces
for (my $i=0; $i <$spaceAvail; $i++) {
print(" ");
}
$val_i++;
}

Add a comment
Know the answer?
Add Answer to:
In *PERL* Use a loop structure and code a program that produces the following output (Take...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please answer (1,2,4)having issues Use a loop structure and code a program that produces the following...

    Please answer (1,2,4)having issues Use a loop structure and code a program that produces the following output (Take one parameter to let user specify how mans lines need to be printed): AA AAA AAAB AAABA AAABAA AAABAAA AAABAAAB ... Implement a program that processes an input file by changing every occurrence of an old string into a new string. (The usage is: chstr file oldstring newstring, chstr is your program name, file, oldstring and newstring are parameters specified by user.)...

  • Please help with the following perl program 3. Rewrite the following Perl code in a more...

    Please help with the following perl program 3. Rewrite the following Perl code in a more Perl-specific programming style. Take advantage of as many Perl shortcuts as you can to make the code shorter and/or easier to read: @words = ("good", "bad", "ugly", "nice"); for ($i = 0; $i <= $#words; $i++) { printf("%s %s %s", "I have been very", $words[$i], "\n"); }

  • this is for script for terminal or putty question 1------ 1. Write a PERL script/program based...

    this is for script for terminal or putty question 1------ 1. Write a PERL script/program based on the following requirements. The program should to prompt the user for how many courses the plan to take next semester and ask the user to enter each of those courses before displaying all the information entered back on the screen. question 2------ 2. Write a PERL script that defines/declares an array or a list of elements. The array should hold three-letter abbreviations for...

  • create perl script to check all command arguments. Display the argument one by one (use a...

    create perl script to check all command arguments. Display the argument one by one (use a for loop). If there is no argument provided, remind users about the mistake create perl script to create the following directory structure in a directory of user’s choice. The user can supply this input as an argument; if not, prompt the user to enter one from the command line. (User selected existing directory) -->Data -->Image -->Cache

  • i'm doing a take home test and was wondering if anyone could help me out with...

    i'm doing a take home test and was wondering if anyone could help me out with these On the computer, using jgrasp: You will write a java program named Party that will output lines of stars with how many stars and how many lines, both determined by parameters.. This program will have a method named starOutput that takes two integers as parameters, the first for how many times the outer loop will repeat the inner loop and the second for...

  • In C++ Programming, Try using loops only. This lab demonstrates the use of the While Loop...

    In C++ Programming, Try using loops only. This lab demonstrates the use of the While Loop and the Do While Loop as error checking mechanisms. You will be using each of these loops to solve the same problem. Please put them both in the same program. When you test the code, you will not see a difference in the way they execute - but there will be a difference in the logic when writing the code. You will want to...

  • whats the answers For the following code, indicate how many times the loop body will execute...

    whats the answers For the following code, indicate how many times the loop body will execute for the following input values: 2 1 0? userNum - 3 while (userNum > 0) { // Do something // Get user um from input } 3 Given the following code, how many times will the inner loop body execute? int row: int col; for (row = 0; row < 2; row - row + 1) { for (col = 0; col < 3;...

  • Create a Java program that analyzes a program and produces the output into another file. The...

    Create a Java program that analyzes a program and produces the output into another file. The program must retrieve the path to the .java file. Once the path to the file is found the program must figure out the length of the longest line is in the program. For example, working with a program called MainFile.java. Once it figures out the longest line in the code it will put the output into MainFile.java.stats and it will be located in the...

  • NOTE: Write the Program in python as mentioned below and use while loop and flags as...

    NOTE: Write the Program in python as mentioned below and use while loop and flags as necessary. And please write the code following the program description given below. Directions: Read the program description below carefully. All requirements must be met by your program to receive full credit. Thus, pay particular attention to input and output requirements, structural requirements, and so on. Furthermore, code that contains syntax errors will not receive any credit, so be sure that your code interprets correctly...

  • NOTE: Write the Program in python as mentioned below and use while loop and flags as...

    NOTE: Write the Program in python as mentioned below and use while loop and flags as necessary. And please write the code following the program description given below. DON'T use Function concept or any other concept except while loop,if-else. Directions: Read the program description below carefully. All requirements must be met by your program to receive full credit. Thus, pay particular attention to input and output requirements, structural requirements, and so on. Furthermore, code that contains syntax errors will not...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT