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";
{

{\$score + 4;}
{\$score + 3;}
{\$score + 2;}
{\$score + 1;}
else {\$score + 0;}
}

\$gpa=\$score/\$classes;
*******************************************************
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

2. John BokmaGuest

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

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

^^^^^^^^^^^^

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

> {

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

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

--
Perl programming
Fort Worth, Texas

4. John BokmaGuest

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
5. John BokmaGuest

> <> 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
6. John W. KrahnGuest

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++)
> {
> print "Enter a grade: ";
>
> {\$score + 4;}
> {\$score + 3;}
> {\$score + 2;}
> {\$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
7. Guest

Thanks all for the help. Tad, you are mean no I'm just kidding, I
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
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
8. DJ StunksGuest

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!

-jp

DJ Stunks, Jun 7, 2006

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

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

perldoc perlop

section without much fuss or bother.

--
Perl programming
Fort Worth, Texas

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

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

--
Perl programming
Fort Worth, Texas

12. Ted ZlatanovGuest

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