Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Please Help! This is a class assignment given to me.

by vidyadithya (Initiate)
on Dec 06, 2019 at 18:12 UTC ( [id://11109766]=perlquestion: print w/replies, xml ) Need Help??

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

Write a PERL Program to find the profit of selling 10 pizzas at the price of 300 each when the cost price is 250 each. Also the discount givem is 10% for each pizza.

  • Comment on Please Help! This is a class assignment given to me.

Replies are listed 'Best First'.
Re: Please Help! This is a class assignment given to me.
by marto (Cardinal) on Dec 06, 2019 at 18:14 UTC

      I dont know where to start or how to start.. :-(

        With respect, you have the problem in English. Your assignment is to write a program in Perl to solve the problem. If you don't know where to begin perlintro or tutorials->Getting Started with Perl is the best place to begin.

        Thanks for admitting this is homework. It's a refreshing breath of honesty for such questions.

        I don't know where to start or how to start

        Start with the links that marto just gave you, combined with the textbook or class notes from the class you are taking.

        1. Figure out the math for how to solve that problem manually. It is a simple math problem, and if you are advanced enough in your education to be taking a class on Perl programming, you should be able to solve it. If you need help with the math, perlmonks isn't really the place, because that's a math question, not a Perl programming question.
        2. Translate that math into Perl syntax. Your textbook or class notes should have examples of how to do the simple math functions required.
Re: Please Help! This is a class assignment given to me.
by footpad (Abbot) on Dec 06, 2019 at 18:30 UTC

    Hello and welcome to the Monastery!

    To begin, let me point you to a response to a similar thread that looks relevant. (Go ahead, it's a quick read and it'll help. Come back when you're finished.)

    Since it's homework, you'll need to do the lion's share in figuring it out. If you get stuck, post specific details about what you tried (e.g. the code), what you expected, what you saw, and what confuses you.

    If this is your first programming class, think about the problems you need to solve, put them in order, and then try to solve each in sequence. This particular assignment looks like a traditional top-down approach would be helpful.

    We're generally happy to clarify things that don't make sense, but we do expect everyone to do their own homework (which includes basic research).

    Marto has given you some good links to help you get started. See where they take you and, if you need to, ask follow-up questions...preferably with code.

    --f

Re: Please Help! This is a class assignment given to me.
by AnomalousMonk (Archbishop) on Dec 06, 2019 at 22:18 UTC

    One way to begin attacking a problem is to just start writing down obvious (update: even rather trivial) elements of the problem and general relationships between those elements. Even if it doesn't seem immediately relevant, just write it down. As you begin to see more relationships, write those down (or at least keep them in mind). AnonyMonk has started the process here.

    What else springs to mind?

    • If each pizza has a cost and you make a certain number of them, what's the total cost?
          my $total_cost = $cost ... $sold ...;
    • If you're actually selling each pizza at a discounted price, what is that price?
          my $discounted_price = $price ... $discount ...;
    • If you sell a certain number of pizzas at the discounted price, what are your total sales?
          my $total_sales = $discounted_price ... $sold ...;
    • What's your profit?
          my $profit = $total_sales ... $total_cost ...;
    So you might end up with an outline of general relationships like this:
    #!/usr/bin/perl use strict; use warnings; my $sold = 10; my $cost = 250; my $price = 300; my $discount = 0.10; my $total_cost = $cost ... $sold ...; my $discounted_price = $price ... $discount ...; my $total_sales = $discounted_price ... $sold ...; my $profit = $total_sales ... $total_cost ...;
    Again, as long as they're true, don't worry if some of the statements don't seem immediately relevant or useful; it's easy to delete text. Work on refining relationships that seem useful: exactly what is the relationship of the total cost to the per-item cost and the total made or sold? Continue on to a solution!


    Give a man a fish:  <%-{-{-{-<

Re: Please Help! This is a class assignment given to me.
by GrandFather (Saint) on Dec 07, 2019 at 01:59 UTC

    If this is your very first experience with Perl you could take one step back and write a "Hello world" program. A hello World is a trivial program to print "Hello world." on your screen. Once you have achieved that you know how to enter the program and run it. In Perl the code is:

    use strict; use warnings; print "Hello world\n";

    If you can't get that to run you need to tell us what operating system you are using at least and how you have installed Perl.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Re: Please Help! This is a class assignment given to me.
by Anonymous Monk on Dec 06, 2019 at 21:13 UTC
    The teacher needs to know if you can think and solve problems. If you can think, the teacher also needs to know how you think. If you can't think we have jobs for you that don't involve computers.

    Start by translating the problem into Perl, then do the math:

    #!/usr/bin/perl use strict; use warnings; my $sold = 10; my $cost = 250; my $price = 300; my $discount = 0.10; # your code here
Re: Please Help! This is a class assignment given to me.
by karlgoethebier (Abbot) on Dec 07, 2019 at 00:31 UTC

    Please see Elementary arithmetic as well as Percentage for a point to start. If this is too much effort you may ask your local favorite pizza dealer for a practical example.

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: Please Help! This is a class assignment given to me.
by Fletch (Bishop) on Dec 06, 2019 at 18:16 UTC

    It's so simple a child could do it.

    1. Underpants Pizza
    2. ???
    3. Profit.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Please Help! This is a class assignment given to me.
by harangzsolt33 (Chaplain) on Dec 07, 2019 at 06:11 UTC
    I dont know where to start or how to start

    You should start with the math. Figure out what you want the program to calculate and how to calculate it. Once you get that straight, writing the perl script will be very easy.

    You want to find the profit. And you have 10 pizzas. Let's find the profit for one pizza first and then we multiply it by 10. Okay?

    The profit equals income - expense. Income is the price you get for the pizza, and expense is what you pay for the ingredients and cooking and labor, etc.

    In this case, figuring out the expense is easy, because we are told that the pizza price is 250. So, 250 includes all the expenses. The income is 300, because that's what you're selling it for. And the difference is 50. That's your profit. That's the question.

    But then what if you sell the pizza 10% cheaper? Now, instead of selling it for 300, you sell it for 270, because that's 10% less. Now, if you pay 250 for the pizza and sell it for 270, then you still have a profit of 20 on each pizza.

    So, the math is pretty simple... Yes, writing the perl code is also simple, but you have to figure it out yourself.

    Every perl program starts with the same three lines of code. And this is just to get you started:

    #!/usr/bin/perl -w

    use strict;
    use warnings;

    my $A = 250; # Pizza price
    my $B = 300; # Selling price
    my $C = $B - $A; # Difference
    print "A = $A \n B = $B \n C = $C \n";

    In programming languages, when you write A = 4, that's not a math equation. It's an instruction for the processor to store the value "4" in a memory location which we label as "A" If you want to do a multiplication, then you would do something like:

    A = 4 * 12;

    But in perl, every label or variable I should say is preceded by a $ sign. And you should declare the variable before you use it. That means the first time you assign a value to it, you should put the word "my" in front of it:

    my $A = 4;

    If you want to do multiplication in perl, you would do:

    my $A = 4 * 12;
    $A = $A * 2;

    When you multiply a value by something else, you can write it as :

    my $A = 2;
    $A *= 5;
    print $A; # Output: 10

    Hope that wasn't too confusing.

      Every perl program starts with the same three lines of code. And this is just to get you started:
      #!/usr/bin/perl -w use strict; use warnings;

      Both use warnings and -w isn't needed, see: What's wrong with -w and $^W

      Personally I prefer the shebang #!/usr/bin/env perl to say "use whatever perl is in my PATH" (see env), it'll also work better with e.g. perlbrew. Pointing to a specific perl binary makes sense if distributing a script to a system where you may not have control over the PATH (e.g. CGI scripts on hosting providers).

      Update: I agree with Tux's reply: In the case of the shebang, TIMTOWTDI.

        Pointing to a specific perl binary makes sense if distributing a script to a system where you may not have control over the PATH.

        Or if you want to make sure it uses a specific (non system) perl install that has modules installed that are not available in general distributions or have libraries linked that are specific to the script or …

        Personally I always use #!/pro/bin/perl, which is where I have my production perl installed. If I do not need anything specific, and system-perl will do (which is seldom the case), I make /pro/bin/perl a symlink to the required perl. TIMTOWTDI


        Enjoy, Have FUN! H.Merijn
      my $A = 250; # Pizza price my $B = 300; # Selling price my $C = $B - $A; # Difference print "A = $A \n B = $B \n C = $C \n";

      Good news - Perl actually allows variable names with more than one character in them. This means that you no longer have to remember what $B is supposed to represent. Instead you can give that variable a meaningful name.

      my $cost_price = 250; my $sale_price = 300; my $profit = $sale_price - $cost_price; print "Make them for $cost_price\nSell them for $sale_price\nMake $pro +fit on each\n";

      Addendum: There is, as with most things, a happy medium.

        You should consider to whom the names are 'meaningful'. I would consider the name '$selling_price' to be ambiguous (before or after the discount?), but the 'client' who wrote this spec clearly intends the before case. This name probably meets the 'happy medium' rule, but only if you mean exactly the same thing that your client does.
        Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11109766]
Approved by beech
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (None)
    As of 2024-04-25 01:31 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found