Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help rounding numbers and assigning letter grade to the numerical

Status
Not open for further replies.

frasca282

Vendor
Feb 25, 2010
2
US
I need help rounding the grade average from the grade calculator. For instance from like 25.5 to 26 and from 25.4 to 25. Also, I need help printing a letter grade from your average.

Here is what i have so far:

#!/usr/bin/perl




print "Content-type:text/html\n\n";

use CGI ':standard';

$grade1=param('grade1');
$grade2=param('grade2');
$grade3=param('grade3');
$grade4=param('grade4');


@array=($grade1,$grade2,$grade3,$grade4);


foreach $grade(@array) {

print "$grade <br> ";




if(($grade !=Null) || ($grade eq 0)) {$count++}



$total=$grade+$total;
}

$average=$total/$count;

print "Your total grade is $total <br>
Grades input: $count <br>
Your average grade is: $average";



 
Have a look at perldoc -f int... it gives some advice on how to do rounding.

How are letter grades defined? I presume you'll just need some logic like "if it's greater than x, it's an A, else if it's greater than y it's a B, etc. ... else it's a fail."

Annihilannic.
 
The perldoc perlsyn page tells you how to write logic statements and gives examples.

Annihilannic.
 
I would consider building a hash with the grade as the key, and the lower/upper score boundaries as the value. Then only 1 condition statement has to be used and the grades/score boundaries can easily be edited from the hash table. I also round numbers using $number = int($number + 0.5);.

For example:

Code:
#! /usr/bin/perl
use strict;
use CGI ':standard';
#####
my $score1 = "2.2";
my $score2 = "4.8";
my $score3 = "6.1";
my $score4 = "6.4";
my @scores = ($score1,$score2,$score3,$score4);

my $totalscore;

foreach (@scores) {
	$_ = int($_ + 0.5);
	$totalscore += $_;
}
#####
my %grades = (
	'A' => [21,25],
	'B' => [16,20],
	'C' => [11,15],
	'D' => [6,10],
	'E' => [1,5],
	'F' => [0,0]
);

my $finalgrade;

foreach (keys %grades) {
	if (($totalscore >= ${$grades{$_}}[0]) && ($totalscore <= ${$grades{$_}}[1])) {
		$finalgrade = $_;
	}
}
#####
print "Content-type:text/html\n\n";
print "Score = $totalscore, Grade= $finalgrade";

Chris
 
From:
If you wish to round to a specific significant digit, you can use the printf function (or sprintf, depending upon the situation), which does proper rounding automatically. See the perlfunc man page for more information on the (s)printf function.

Version 5 includes a POSIX module which defines the standard C math library functions, including floor() and ceil(). floor($num) returns the largest integer not greater than $num, while ceil($num) returns the smallest integer not less than $num. For example:


#!/usr/local/bin/perl
use POSIX qw(ceil floor);

$num = 42.4; # The Answer to the Great Question (on a Pentium)!

print "Floor returns: ", floor($num), "\n";
print "Ceil returns: ", ceil($num), "\n";

Which prints:
Floor returns: 42
Ceil returns: 43
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top