Question

Rewrite calcs.pl to use subroutines. The program should prompt the user for 2 numbers, take those...

Rewrite calcs.pl to use subroutines. The program should prompt the user for 2 numbers, take those 2 numbers in and pass them to the 5 different subroutines for each operator example. Each of the subroutines should pass the result back out of the subroutine to the main routine for printing to the user.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
  • I assumed that the program to be rewritten is calculator program for basic arithmetic operators in Perl.
  • The basic arithmetic operators were not mentioned so i did the solution for six operators namely addition, subtraction, multiplication, division, power and remainder operators in Perl.
  • In the code written below, the symbol "#" means a comment .

The code is as follows :-

use warnings;

#Here are the subroutines for each operator.

sub Add($$){

    my ($a, $b) = @_;

    my $c = $a + $b;

    return ($c);

}

sub Subtract($$){

    my($a, $b) = @_;

    my $c = $a - $b;

    return($c);

}

sub Multiply($$){

    my($a, $b) = @_;

    my $c = $a * $b;

    return ($c);

}

sub Div($$){

    my ($a, $b) = @_;

    my $c = $a / $b;

    return ($c);

}

sub Remainder($$){

    my ($a, $b) = @_;

    my $c = $a % $b;

    return ($c);

}

sub Pow($$){

    my ($a, $b) = @_;

    my $c = $a ** $b;

    return ($c);

}

#The main routine starts here.

print "Enter two numbers for calculation \n";

#Taking user input.

my $input1 = <>;

my $input2 = <>;

#Calling subroutines and storing corresponding results for printing.

my $add_result = Add($input1, $input2);
my $sub_result = Subtract($input1, $input2);
my $mul_result = Multiply($input1, $input2);
my $div_result = Div($input1, $input2);
my $pow_result = Pow($input1, $input2);
my $remainder = Remainder($input1, $input2);

#Printing the results obtained from the subroutines.

print "The result of addition is = $add_result \n";
print "The result of subtraction is = $sub_result \n";
print "The result of multiplication is = $mul_result \n";
print "The result of division is = $div_result \n";
print "The result of power operation is = $pow_result \n";
print "The result of remainder operation is = $remainder \n";

Add a comment
Know the answer?
Add Answer to:
Rewrite calcs.pl to use subroutines. The program should prompt the user for 2 numbers, take those...
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
  • Instructions Write a program called stocks.cpp. The program should prompt the user for the number of...

    Instructions Write a program called stocks.cpp. The program should prompt the user for the number of shares purchased, purchase price per share, the commission for the purchase, sale commission paid, and the price the shares were sold at. The program should validate each of these values to ensure they are acceptable (none of these values can be less than zero) and should then pass them to a function called calculateProfit. The function should use the following formula to determine the...

  • Your program must prompt the user to enter a string. The program must then test the...

    Your program must prompt the user to enter a string. The program must then test the string entered by the user to determine whether it is a palindrome. A palindrome is a string that reads the same backwards and forwards, such as "radar", "racecar", and "able was I ere I saw elba". It is customary to ignore spaces, punctuation, and capitalization when looking for palindromes. For example, "A man, a plan, a canal. Panama!" is considered to be a palindrome....

  • Using Perl Program: Write a program with a subroutine to prompt the user for any message...

    Using Perl Program: Write a program with a subroutine to prompt the user for any message (like "what is your name?"). The sub will collect the user's answer and return it back to the call (in the main program above the subroutine). The sub does not accept any arguments. Print out the returned string (name) from the sub.

  • Subroutines in MIPS Determines the minimum of two integers Functions within the MIPS slides describe how...

    Subroutines in MIPS Determines the minimum of two integers Functions within the MIPS slides describe how one can use subroutines (also called procedures, functions, and methods) in MIPS. Because of the importance of subroutines in modern programming, most hardware designers include mechanisms to help programmers. In a high-level language like C or Java, most of the details of subroutine calling are hidden from the programmer. MIPS has special registers to send information to and from a subroutine. The registers $a0,...

  • You must write a C program that prompts the user for two numbers (no command line...

    You must write a C program that prompts the user for two numbers (no command line input) and multiplies them together using “a la russe” multiplication. The program must display your banner logo as part of a prompt to the user. The valid range of values is 0 to 6000. You may assume that the user will always enter numerical decimal format values. Your program should check this numerical range (including checking for negative numbers) and reprompt the user for...

  • In Java Main method Your main program will prompt the user for a test name and...

    In Java Main method Your main program will prompt the user for a test name and the number of scores the user will enter. After creating an arraylist to hold the scores, prompt the user for all the scores to hold in the arraylist. After all the scores are entered, then repeat back the score & letter grade. GradeBook Object Create an instance of a GradeBook object and pass the test name and arraylist to set the instance variables. At...

  • SUBROUTINES Example 2: May 2006 Question 2 The main program uses a subroutine located at address...

    SUBROUTINES Example 2: May 2006 Question 2 The main program uses a subroutine located at address 3000,- Read the program and answer the following questions. (a) What is the address of the instruction DCR E in the main program?[3 marks] (b) What instructions need to be written in the subroutine and give the address of the instructions? [3 marks] List the contents of the stack, the PC, and the stack pointer after (i) Execution of CALL instruction in the main...

  • 2. Write a program that reads N integer numbers of array from the user and then...

    2. Write a program that reads N integer numbers of array from the user and then displays the sum, max number and the numbers of the array use four subroutines ( Read_Array(), Calculate_Sum(), Find_Max() and Display_Array()). Here is sample output Please enter number of elements: 4 Please enter the required numbers: 50 100 150 20 The entered numbers are: 50 100 150 20 Sum is : 320 Max is 150 by Mpsi ,sum and max to call subroutine and retrieve...

  • USE C++ 2.) Write a program that will prompt the use to enter 10 numbers in...

    USE C++ 2.) Write a program that will prompt the use to enter 10 numbers in an array, then display: the 1st, the 5th and the last number. Example, if the user entered 1 2 3 4 5 6 7 8 9 10, the output shall be: 1st is 1 5th is 5 last is 10

  • Program C: Start by printing out your name, then your code should prompt the user to...

    Program C: Start by printing out your name, then your code should prompt the user to enter integers using the following prompt: Enter some integers, end with a 0: Your code should: Level 1 Task: count the number of integers entered (not including the 0) Level 2 Task: produce the sum of all of the integers Level 3 Task: produce the sum of the even integers These calculated values need to be displayed as follows: There were ___ numbers read...

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