Jump to content

School Grade Calculations?


paulmo

Recommended Posts

I need to create an app which calculates numerical and letter grades, from input number which = total points for assignment. For example, if assignment is worth 70 points total*, 70 is entered in form field, then submit button returns values in table: A 67-70A- 64-66B+ 61-63B 58-60etc. This would be my first math app, so much thanks in advance for getting me started. *Grading cutoffs based on 100 points: A 93A- 90B+ 87B 83B- 80C+ 77C 73C- 70D+ 67D 63

Link to comment
Share on other sites

This would be my first math homework app, so much thanks in advance for getting me started.
this is very basic. you need to learn forms and how to get them to display on the page and accept user input, and then display that on the page. Determining the letter grade is just a simple if/else or switch control functions. You would probably want the form to submit to itself. Let's see what you got first and we'll take it from there.
Link to comment
Share on other sites

Since the grade scale is based on a 100-point system you can just use percentages. Though if that's the case then your original table is wrong because 93% of 70 is 65, not 67.You could create an array that has all the cutoff values in it:$arrCutoffs = array("A" => .93, "A-" => .90, ...);Then use a loop to multiply the input from the POST or GET (whichever you're using to submit the value) by each of the cutoff values and print them out:

foreach ($arrCutoffs as $grade => $cutoff) {   echo $grade.": ".($inputValue*$cutoff)."<br />";}

This will print out the minimum points for each grade level.

Link to comment
Share on other sites

Thanks Shadow! I'm getting a parse error on the $arrCutoffs line:

<form name="calc" method="post"> <input name="total_points" type="text" id="total_points">  <input id="submitbutton2" name="submitfield1" value="calc"> </form> <?php$arrCutoffs = array("A" => .93, "A-" => .90, "B+" => .87, "B" => .83, "B-" => .80, "C+" => .77, "C" => .73, "C-" .70, "D+" => .67, "D" => .63, "F" =>.0);foreach ($arrCutoffs as $grade => $cutoff) {   echo $grade.": ".($total_points*$cutoff)."<br/>";}?>

Link to comment
Share on other sites

Sorry for double post: I must have hit reply instead of edit. Along with parse error, $cutoff is not defined. What to do with that?

<form name="calc" method="post"> <input name="total_points" type="text" id="total_points">  <input id="submitbutton2" name="submitfield1" value="calc"> </form> <?php$arrCutoffs = array("A" => .93, "A-" => .90, "B+" => .87, "B" => .83, "B-" => .80, "C+" => .77, "C" => .73, "C-" .70, "D+" => .67, "D" => .63, "F" =>.0);foreach ($arrCutoffs as $grade => $cutoff) {   echo $grade.": ".($total_points*$cutoff)."<br/>";}?>

Link to comment
Share on other sites

Parse error: parse error, expecting `')'' in C:\EasyPHP5.3.0\www\grade.php on line 250

Also edited php:

<?php$total_points = mysql_real_escape_string($_POST['total_points']); $total_points = strip_tags($total_points); $arrCutoffs = array("A" => .93, "A-" => .90, "B+" => .87, "B" => .83, "B-" => .80, "C+" => .77, "C" => .73, "C-" .70, "D+" => .67, "D" => .63, "F" =>.0);foreach ($arrCutoffs as $grade => $cutoff) {   echo $grade.": ".($total_points*$cutoff)."<br/>";}?>

Link to comment
Share on other sites

Parse error: parse error, expecting `')'' in C:\EasyPHP5.3.0\www\grade.php on line 250

Also edited php; and what to do with undefined $cutoff? Thanks:

<?php$total_points = mysql_real_escape_string($_POST['total_points']); $total_points = strip_tags($total_points); $arrCutoffs = array("A" => .93, "A-" => .90, "B+" => .87, "B" => .83, "B-" => .80, "C+" => .77, "C" => .73, "C-" .70, "D+" => .67, "D" => .63, "F" =>.0);foreach ($arrCutoffs as $grade => $cutoff) {   echo $grade.": ".($total_points*$cutoff)."<br/>";}?>

Link to comment
Share on other sites

Hi JSG, I see $arrCutoffs is defined as array and $grade as $arrCutoffs, but I still don't see where $cutoff is defined as the cutoff values. I'm also curious why "a as b" in loops? Is it just for organization? Thanks.

Link to comment
Share on other sites

This line:foreach ($arrCutoffs as $grade => $cutoff)tells PHP to loop through the array $arrCutoffs, and for each element in the array set the value of $grade to be the key, and $cutoff to be the value. PHP implicitly creates those variables if they don't exist.

Link to comment
Share on other sites

Thanks, I see it now. So loops assign variables to the key => value in array.
The foreach loop does. It only assigns the key to a variable if you use the syntax I gave you, though. The foreach comes in two forms:foreach ($array as $value)foreach ($array as $key => $value)As you might be able to guess, the first one only assigns the value of an element to a variable and the key is not used.The second one assigns both the key and the value to variables.There is also the for loop, which looks like this:for ($var=0; $var < $end; $var++)This one creates a variable ($var) which you can initialize to anything you want, and loops until the condition (in this case, $var < $end; could also be >, <=, >=, etc.) becomes false, incrementing $var by the specified amount (in this case 1, which is what the ++ does). You could also decrement or have bigger intervals, ie:$var--$var+2References:PHP for loops - W3SchoolsPHP for loop - php.netPHP foreach loop - php.net
Link to comment
Share on other sites

OK, I get how foreach is specifically for arrays. I have some questions and observations (which might be wrong) about the other loops. Your example of for loop:

for ($var=0; $var < $end; $var++)

seems more like the basic while loop in manual: the for loop is iterated in manual:

for (expr1; expr2; expr3)	statementThe first expression (expr1) is evaluated (executed) once unconditionally at the beginning of the loop. In the beginning of each iteration, expr2 is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are executed. If it evaluates to FALSE, the execution of the loop ends. At the end of each iteration, expr3 is evaluated (executed).

So how is for different than elseif? They seem to do the same thing. And switch seems identical to if/elseif statements, unless I'm missing something. Justsomeguy created this clever switch statement for me a few years ago, where the cases are months. I imagine this could also be done with if /elseif statements?:

switch (intval(date('n'))){case 1: case 2: case 3:

Thanks.

Link to comment
Share on other sites

Your example of for loop:
for ($var=0; $var < $end; $var++)

seems more like the basic while loop in manual: the for loop is iterated in manual:

for (expr1; expr2; expr3)	statementThe first expression (expr1) is evaluated (executed) once unconditionally at the beginning of the loop. In the beginning of each iteration, expr2 is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are executed. If it evaluates to FALSE, the execution of the loop ends. At the end of each iteration, expr3 is evaluated (executed).

So how is for different than elseif? They seem to do the same thing.

For loops and while loops are indeed very similar. A for loop is not, however, similar to an if/else structure.An if/else performs a test (or series of tests) and executes a single block of code for the first expression that evaluates to true and is only ever executed once.A for loop assigns a value to a variable (expr1), tests for a condition using that variable (expr2), and then increments/decrements the variable. The for loop will keep iterating until the condition in expr2 evaluates to false. There may be other kinds of expressions that can be used but this is by far the most common type of for loop.A while loop does not create or increment any variables. It only evaluates an expression using variables that you create and define. If those variables never change within the loop, the expression will always evaluate to the same thing.Consider these snippets using the array:$arrTest = array(1, 2, 3, 4);
$x = 2; //Predefined value for $xif ($x>count($arrTest)) { //Tests if $x is greater than the number of elements in the array   echo "x is too big";} else {   echo "arrTest[x] = ".$arrTest[$x];}

Runs only once, testing if the value if $x is larger than the number of elements in the array

for ($x=0; $x<count($arrTest); $x++) { //Creates the $x variable and defines it as 0   //At the start of the loop expr2 is evaluated ($x < count($arrTest)) and if it's true execute the code, if not break the loop   echo "arrTest[x] = ".$arrTest[$x];   //At the end of the loop expr3 is evaluated ($x++)   //The cycle then starts over, except $x is not recreated/redefined again}

Runs as long as $x is less than the number of elements in the array. If $x equals 0 the loop will not run.

$x = 0; //Predefined value for $xwhile ($x < count($arrTest)) { //Evaluates the expression at the start of each iteration   //If the condition is true, execute the code, if not break the loop   echo "arrTest[x] = ".$arrTest[$x];   $x++; //Manually increment $x}

Runs as long as $x is less than the number of elements in the array. If $x equals 0 the loop will not run. The difference between this and the for loop is that $x needs to be created and manipulated manually.

And switch seems identical to if/elseif statements, unless I'm missing something. Justsomeguy created this clever switch statement for me a few years ago, where the cases are months. I imagine this could also be done with if /elseif statements?:
switch (intval(date('n'))){case 1: case 2: case 3:

Thanks.

Yes it could be done with an if/else, but a switch is generally faster and easier to read when you're testing the value of a single variable, or you need to do a long list of tests. (Like testing for months, in the example you provided)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...