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.)...

  • Write a perl program Use a while loop to get user input until the user enters...

    Write a perl program Use a while loop to get user input until the user enters "quit" Use a regex to replace any occurrence of the word "python" with the word "perl" and print out the updated version Provide the perl program (.pl file) as well as a screenshot of the output (show at least one example with python being replaced)

  • 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...

  • In this program, you will write a Perl program to perform simple checks. You must use...

    In this program, you will write a Perl program to perform simple checks. You must use loops and if statements to complete the task. 1. Given the following numbers 10, 20, 30, 40, 50, 60, 70, 80, Put the numbers in an array and print the array 2. Assume the user will check if his input, using keyboard, is one of the numbers, the program should behavior as follows: a. User input 100 the program prints not found b. User...

  • 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...

  • Write a java program that makes use of the ‘DO WHILE ’ loop. The program asks...

    Write a java program that makes use of the ‘DO WHILE ’ loop. The program asks a user to enter information about several of their friends. The information is their last name, the state they live in, their age, and their expected graduation year. After you enter one friend, write out each individually. You must use JOptionPane to input the 4 fields, and to output the 4 fields. Refer to the textbook Code Listing 4-6 to see how to use...

  • 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;...

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