{"id":3786,"date":"2019-10-08T00:01:43","date_gmt":"2019-10-08T07:01:43","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3786"},"modified":"2019-10-05T08:33:07","modified_gmt":"2019-10-05T15:33:07","slug":"know-your-gpa-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3786","title":{"rendered":"Know Your GPA &#8211; Solution"},"content":{"rendered":"<p>It&#8217;s fun supplying your own scores for a semester, which really helps boost your GPA! Still, the challenge for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3768\">this Month&#8217;s Exercise<\/a> was to input &mdash; and validate &mdash; a series of grades to calculate your GPA for five courses.<br \/>\n<!--more--><br \/>\nI went through two drafts of my solution. The first had an input routine, <em>get_grade()<\/em>, obtain the input from 0 through 100, validate and return that value. The <em>main()<\/em> function translated the value returned, calculated the GPA value, stored it in an array, then generated the average. This solution worked, but it was clunky.<\/p>\n<p>My second draft has the <em>get_grade()<\/em> function accept and validate input, and return the GPA value directly, not the 0 to 100 grade. This way, the <em>main()<\/em> function can process everything, both collecting input and calculating the average, in a single loop. Here&#8217;s my solution:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\n#define SIZE 12\r\n#define CLASSES 5\r\n\r\nfloat get_grade(void)\r\n{\r\n    char input[SIZE];\r\n    float grade;\r\n    char *p;\r\n\r\n    <span class=\"comments\">\/* obtain input *\/<\/span>\r\n    fgets(input,SIZE,stdin);\r\n    <span class=\"comments\">\/* convert to float *\/<\/span>\r\n    grade = strtod(input,&amp;p);\r\n    <span class=\"comments\">\/* validate *\/<\/span>\r\n    if( p==input )\r\n    {\r\n        puts(\"Inavlid input\");\r\n        exit(1);\r\n    }\r\n    <span class=\"comments\">\/* confirm range *\/<\/span>\r\n    if( grade&lt;0 || grade&gt;100 )\r\n    {\r\n        puts(\"Input out of range\");\r\n        exit(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* return GPA value *\/<\/span>\r\n    if( grade &gt; 89 )\r\n        return(4.0);\r\n    if( grade &gt; 79 )\r\n        return(3.0);\r\n    if( grade &gt; 69 )\r\n        return(2.0);\r\n    if( grade &gt; 59 )\r\n        return(1.0);\r\n\r\n    return(0.0);\r\n}\r\n\r\nint main()\r\n{\r\n    float gpa,average;\r\n    int x;\r\n\r\n    <span class=\"comments\">\/* obtain input *\/<\/span>\r\n    gpa = 0.0;\r\n    for( x=0; x&lt;CLASSES; x++)\r\n    {\r\n        printf(\"Enter grade (0 to 100) for class #%d: \",x+1);\r\n        gpa += get_grade();\r\n    }\r\n    average = gpa\/(float)CLASSES;\r\n\r\n    <span class=\"comments\">\/* output results *\/<\/span>\r\n    printf(\"Your semester GPA is %.1f\\n\",average);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>(<a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2019_10-Exercise.c\" rel=\"noopener noreferrer\" target=\"_blank\">GitHub<\/a>)<\/p>\n<p>The <em>get_grade()<\/em> function uses <em>fgets()<\/em> to obtain input. The <em>strtod()<\/em> function converts input to a decimal value, with the <em>if<\/em> statement (at Line 18) checking for invalid input.<\/p>\n<p>At Line 24, the <em>if<\/em> statement checks the input range: <code>if( grade&lt;0 || grade&gt;100 )<\/code> The program exits on any invalid input. One improvement you could make would be to have it prompt again, which you can code on your own.<\/p>\n<p>Starting at Line 31, a series of <em>if<\/em> tests return the proper GPA value, with the final <em>return<\/em> statement tossing back zero for values below 60.<\/p>\n<p>In the <em>main()<\/em> function, a <em>for<\/em> loop both gathers input and totals the GPA values returned in the <code>gpa<\/code> variable. Line 55 calculates the average, dividing <code>gpa<\/code> by the number of <code>CLASSES<\/code>, which must be typecast to a <em>float<\/em>: <code>average = gpa\/(float)CLASSES;<\/code><\/p>\n<p>Line 58 outputs the result.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>Enter grade (0 to 100) for class #1: 99<br \/>\nEnter grade (0 to 100) for class #2: 98<br \/>\nEnter grade (0 to 100) for class #3: 89<br \/>\nEnter grade (0 to 100) for class #4: 75<br \/>\nEnter grade (0 to 100) for class #5: 100<br \/>\nYour semester GPA is 3.4<\/code><\/p>\n<p>And you see that my GPA has improved from when I ran the code in the original Exercise post!<\/p>\n<p>I hope you devised a solution that&#8217;s clever and tidy and generates accurate output. The key is to use the <em>strtod()<\/em> functione to evaluate input. Refer to <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3671\">the Lesson on <em>strtol()<\/em><\/a> for details on how the function validates input. Otherwise, if whatever crazy method you devised to calculate GPA works, congratulations!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s fun supplying your own scores for a semester, which really helps boost your GPA! Still, the challenge for this Month&#8217;s Exercise was to input &mdash; and validate &mdash; a series of grades to calculate your GPA for five courses.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-3786","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3786","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3786"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3786\/revisions"}],"predecessor-version":[{"id":3798,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3786\/revisions\/3798"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}