Simple GPA calculator program

Discussion in 'Perl Misc' started by rmr531@gmail.com, Jun 7, 2006.

  1. Guest

    I have an assignment for a class that has been driving me crazy for the
    past few days. Basically I need to create a simple GPA calculator that
    takes in several letter grades (ex: A) and then calculates the GPA
    based off that A=4 B=3 C=2 D=1 F=0. Here is what I have, sorry it in
    inefficient sloppy code but I am still new to this and just want it to
    work.

    print "Enter the letter grade (A, B, C, D, F) for each test taken in a
    class, enter Z to quit.\n";

    ****************************************
    $score=0;
    $A="A"; $B="B"; $C="C"; $D="D"; $Z="Z";
    for($classes=0, $grade != $Z, $classes++)
    {
    $grade;
    print "Enter a grade: ";
    chomp($grade = <STDIN> );

    if($grade == $A)
    {$score + 4;}
    if($grade == $B)
    {$score + 3;}
    if($grade == $C)
    {$score + 2;}
    if($grade == $D)
    {$score + 1;}
    else {$score + 0;}
    }

    $gpa=$score/$classes;
    print "Your GPA is: $gpa.\n";
    *******************************************************
    Any help on this one is greatly appreciated, I know many of you out
    there are PERL experts but if you could make the solutions very basic
    that would be great, remember this is a beginner class :) I know it is
    something stupid that I did but I cannot figure it out, I think that my
    logic is sound. Thanks all.
     
    , Jun 7, 2006
    #1
    1. Advertising

  2. John Bokma Guest

    wrote:

    > I have an assignment for a class


    I would probably give you a D for trying.

    (With your program an F is not possible).

    One tip: == is for numbers, eq is for strings.

    --
    John Bokma Freelance software developer
    &
    Experienced Perl programmer: http://castleamber.com/
     
    John Bokma, Jun 7, 2006
    #2
    1. Advertising

  3. <> wrote:

    > I have an assignment for a class



    If we do your assignment for you, then you won't learn what
    you are supposed to learn.

    We would hate to see you short-changed like that.

    You pay good money in tuition to learn stuff, so you should *learn* stuff.


    > but I am still new to this and just want it to
    > work.



    You will fail in programming with such an attitude.

    Consider changing majors.

    Someone destined to become a programmer would want to
    _understand_ how it works, while you appear to be looking
    for cargo-cult programming...


    > $A="A"; $B="B"; $C="C"; $D="D"; $Z="Z";



    You can compare to strings just as you would compare to
    a variable.

    There is no need to copy the string into a variable before
    doing a comparison.


    > for($classes=0, $grade != $Z, $classes++)

    ^^^^^^^^^^^^
    ^^^^^^^^^^^^ $grade ne 'Z'

    != is for comparing numbers, ne is for comparing strings.

    > {
    > $grade;



    Why do you include that line of code?

    That is, what do you think that that line does for you?

    (this is not a rhetorical question.)


    > if($grade == $A)



    == is for comparing numbers, eq is for comparing strings.


    > {$score + 4;}



    Q: Where do you store this newly computed value?

    A: Nowhere!


    > Any help on this one is greatly appreciated,



    Uh huh.


    > I know many of you out
    > there are PERL experts



    *none* of us are PERL experts.

    Many of us are Perl experts though.


    > but if you could make the solutions



    Sure does sound like you want us to do your homework for you.

    We are too ethical for that (and you should be too).


    > I think that my
    > logic is sound.



    You are not thinking correctly then.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 7, 2006
    #3
  4. John Bokma Guest

    John Bokma <> wrote:

    > wrote:
    >
    >> I have an assignment for a class

    >
    > I would probably give you a D for trying.
    >
    > (With your program an F is not possible).


    Ok, that was stupid :-D

    > One tip: == is for numbers, eq is for strings.



    --
    John Bokma Freelance software developer
    &
    Experienced Perl programmer: http://castleamber.com/
     
    John Bokma, Jun 7, 2006
    #4
  5. John Bokma Guest

    Tad McClellan <> wrote:

    > <> wrote:


    [..]

    >> but I am still new to this and just want it to
    >> work.

    >
    > You will fail in programming with such an attitude.


    No, no, the attitude is good. Wanting the program to work is good. Better
    then make it appear like it works :-D.

    >> I know many of you out
    >> there are PERL experts

    >
    >
    > *none* of us are PERL experts.
    >
    > Many of us are Perl experts though.


    And some are perl experts :-D

    > Sure does sound like you want us to do your homework for you.
    >
    > We are too ethical for that (and you should be too).


    I have made in the past homework in exchange for money and books (Perl
    related ;-) ). I included also documentation. I don't think that's
    unethical. It's up to the student to reread my code, and learn from it,
    and ask smart questions. So far they have done so. Maybe that them paying
    for the help stimulated them.

    BTW, most of the times the students had already done quite some work, but
    just got stuck. In one case it was not even Perl related at all.

    --
    John Bokma Freelance software developer
    &
    Experienced Perl programmer: http://castleamber.com/
     
    John Bokma, Jun 7, 2006
    #5
  6. wrote:
    > I have an assignment for a class that has been driving me crazy for the
    > past few days. Basically I need to create a simple GPA calculator that
    > takes in several letter grades (ex: A) and then calculates the GPA
    > based off that A=4 B=3 C=2 D=1 F=0. Here is what I have, sorry it in
    > inefficient sloppy code but I am still new to this and just want it to
    > work.
    >
    > print "Enter the letter grade (A, B, C, D, F) for each test taken in a
    > class, enter Z to quit.\n";
    >
    > ****************************************
    > $score=0;
    > $A="A"; $B="B"; $C="C"; $D="D"; $Z="Z";
    > for($classes=0, $grade != $Z, $classes++)
    > {
    > $grade;
    > print "Enter a grade: ";
    > chomp($grade = <STDIN> );
    >
    > if($grade == $A)
    > {$score + 4;}
    > if($grade == $B)
    > {$score + 3;}
    > if($grade == $C)
    > {$score + 2;}
    > if($grade == $D)
    > {$score + 1;}
    > else {$score + 0;}
    > }
    >
    > $gpa=$score/$classes;
    > print "Your GPA is: $gpa.\n";
    > *******************************************************
    > Any help on this one is greatly appreciated, I know many of you out
    > there are PERL experts but if you could make the solutions very basic
    > that would be great, remember this is a beginner class :) I know it is
    > something stupid that I did but I cannot figure it out, I think that my
    > logic is sound. Thanks all.



    my %lookup = (
    A => sub { 4 },
    B => sub { 3 },
    C => sub { 2 },
    D => sub { 1 },
    Z => sub { print 'Your GPA is: ', $_[ 0 ] / $_[ 1 ], ".\n" and exit }
    );

    my ( $score, $classes );
    { print 'Enter a grade: ';
    chomp( my $grade = uc <STDIN> );

    $score += $lookup{ $grade }( $score, $classes++ ) and redo
    }



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Jun 7, 2006
    #6
  7. Guest

    Thanks all for the help. Tad, you are mean :) no I'm just kidding, I
    am sure that your advice has been taken advantage of before, but
    seriously take it easy on us newbies many of us are trying our best, I
    am just more of a Java guy, this Perl almost makes it to simple
    sometimes, putting strings and int and char all into one variable type
    is just chaos on my little mind. But your advice was very helpful, and
    I do appreciate it.

    A couple of you seemed to think that I was asking you to do the entire
    thing for me, I honestly was not, if I had I would not have done as
    much as I did. Maybe I just worded the post wrong but I was only
    looking for people to point our my mistakes (which there were plenty
    of, but I was on the right track, you have to admit), and the only
    reason that I just wanted to get it working was because it is due
    today. Maybe I should have said that I just want to figure out why it
    is not working, because that is what I meant.

    Basically all that I needed was the "ne" and the and "eq", not to make
    excuses but it is VERY hard to find that kind of stuff in the book that
    I have. Also I needed to change all those ifs to elsif. But I got it
    working, just need to tweak it to make it look pretty now. And
    speaking of ethics I am not going to post the full working program just
    because someone might use it to get out of an assignment, plus I am
    sure that you all know what it looks like.

    Thanks both John's for the help, John K. your program worked well and I
    kind of understand it but unfortunately this is a really really
    beginner class and even though it seems pretty simple it would have
    stood out like a sore thumb to the instructor, plus I could not have
    explained exactly how it worked (see I really am ethical).
     
    , Jun 7, 2006
    #7
  8. DJ Stunks Guest

    John W. Krahn wrote:
    > wrote:
    > > print "Enter the letter grade (A, B, C, D, F) for each test taken in a
    > > class, enter Z to quit.\n";

    >
    > my %lookup = (
    > A => sub { 4 },
    > B => sub { 3 },
    > C => sub { 2 },
    > D => sub { 1 },
    > Z => sub { print 'Your GPA is: ', $_[ 0 ] / $_[ 1 ], ".\n" and exit }
    > );
    >
    > my ( $score, $classes );
    > { print 'Enter a grade: ';
    > chomp( my $grade = uc <STDIN> );
    >
    > $score += $lookup{ $grade }( $score, $classes++ ) and redo
    > }


    tight! :D

    -jp
     
    DJ Stunks, Jun 7, 2006
    #8
  9. <> wrote:

    > Basically all that I needed was the "ne" and the and "eq", not to make
    > excuses but it is VERY hard to find that kind of stuff in the book that
    > I have.



    You should not be using the book that you have as your primary reference.

    (What book _do_ you have?)

    You should use the documentation for the software that you are
    using for your primary reference.

    You need to find info on an "operator", so the place to look first is:

    perldoc perlop

    Scanning the section headers leads to the "Relational Operators"
    section without much fuss or bother.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 7, 2006
    #9
  10. Guest

    Thanks, that should help quite a bit, I wish they showed us that kind
    of stuff in class.

    The book that we have is "Perl by Example" by Ellie Quigley. I
    definately would not reccommend it, everyone in the class hates it,
    including the instructor, but what can you do, we have to use what the
    college wants us too even if they are wrong.
     
    , Jun 7, 2006
    #10
  11. <> wrote:

    > Thanks, that should help quite a bit,



    You're welcome.


    > I wish they showed us that kind
    > of stuff in class.



    Students in my classes get shown that kind of stuff. :)


    > The book that we have is "Perl by Example" by Ellie Quigley. I
    > definately would not reccommend it, everyone in the class hates it,



    Yeah, that book has a very poor reputation.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 7, 2006
    #11
  12. Ted Zlatanov Guest

    On 7 Jun 2006, wrote:

    > The book that we have is "Perl by Example" by Ellie Quigley. I
    > definately would not reccommend it, everyone in the class hates it,
    > including the instructor, but what can you do, we have to use what
    > the college wants us too even if they are wrong.


    It might be a fun exercise for the class to pick a few chapters and
    find errors in them, then send that list of errors to the department
    chair and the dean.

    You can learn a lot that way, both about Perl and about real-world
    software, programmers, and politics :)

    Ted
     
    Ted Zlatanov, Jun 8, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ron
    Replies:
    14
    Views:
    1,010
  2. Rafael

    Need help with my Calculator program.

    Rafael, Dec 2, 2003, in forum: C Programming
    Replies:
    6
    Views:
    411
    osmium
    Dec 4, 2003
  3. Paul

    coding a simple calculator

    Paul, Mar 7, 2004, in forum: C Programming
    Replies:
    3
    Views:
    8,936
    Malcolm
    Mar 7, 2004
  4. Vijaykumar Dave

    calculator program with memory function

    Vijaykumar Dave, Apr 26, 2007, in forum: C Programming
    Replies:
    10
    Views:
    656
    Vijaykumar Dave
    May 3, 2007
  5. SIMPLE Javascript Calculator

    , Jul 10, 2006, in forum: Javascript
    Replies:
    23
    Views:
    340
    Randy Webb
    Jul 13, 2006
Loading...

Share This Page