{"id":590,"date":"2020-08-24T11:52:40","date_gmt":"2020-08-24T16:52:40","guid":{"rendered":"https:\/\/www.brezeale.com\/?p=590"},"modified":"2020-08-24T11:57:02","modified_gmt":"2020-08-24T16:57:02","slug":"c-application-choosing-an-electrical-plan","status":"publish","type":"post","link":"https:\/\/www.brezeale.com\/?p=590","title":{"rendered":"C Application:  Choosing an Electrical Plan"},"content":{"rendered":"\n<p>People learning to program often struggle with how to decompose a problem into the steps necessary to write a program to solve that problem. This is one of a series of posts in which I take a problem and go through my decision-making process that leads to a program.<\/p>\n\n\n\n<p><strong>Background:<\/strong> In the spring of 2020, it was time to choose a new electrical plan and with my current provider I basically had two options.  My home has a smart meter, so I am able to download my electrical usage in increments of 15 minutes.  This allows me to apply each plan to actual data to see what the costs would have been.<\/p>\n\n\n\n<p>Also relevant is that prior to April, 2020 I was going to work during the day and therefore wasn&#8217;t using as much electricity during the daytime.<\/p>\n\n\n\n<p><strong>Problem:<\/strong> Write a program to choose the cheapest electrical plan based on historical data.  I assume that you understand statements, conditionals, loops, arrays, functions, strings, pointers, and file I\/O.<\/p>\n\n\n\n<p>You can find a video with more details at <a rel=\"noreferrer noopener\" href=\"https:\/\/www.youtube.com\/watch?v=rv0DwcdAgPM\" target=\"_blank\">https:\/\/www.youtube.com\/watch?v=rv0DwcdAgPM<\/a> .<\/p>\n\n\n\n<br>\n\n\n\n<p><strong>Design:<\/strong>  Let&#8217;s look at the plans.  This gives me an idea of what the relevant values are:<\/p>\n\n\n\n<p>Plan 1 charges different rates for daytime and nightime.  They are<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n      daytime:  8am &amp;lt;= time &amp;lt; 8pm    $0.088008\/kWh\n    nighttime:  8pm &amp;lt;= time &amp;lt; 8am    $0.078008\/kWh\ndelivery cost:  $3.42 + $0.035448\/kWh\n<\/pre><\/div>\n\n\n<p>Plan 2 charges the same rate each hour of the day.  It&#8217;s cost is calculated as<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n     any time:  $0.078341\/kWh\ndelivery cost:  $3.42 + $0.035448\/kWh\n<\/pre><\/div>\n\n\n<p>The delivery costs are the same for both plans, so I could leave them out since I simply want to know which cost is cheapest.  However, I will include them in case a comparison of future plans has them as different costs.<\/p>\n\n\n\n<p>I can see that both plans determine my costs from the the kWh (i.e., kilowatt-hour) used for a time period, but that Plan 1 uses a different rate depending on the time of day.  Therefore, I need to know the time of day and the kWh used.<\/p>\n\n\n\n<p>Next, I want to know what I am working with with regard to the data, so I downloaded historical electrical usage for the period of January 1, 2020 to May 31, 2020; the data is in CSV format.  Here are the first few lines (with my actual meter number changed): <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nESIID,USAGE_DATE,REVISION_DATE,USAGE_START_TIME,USAGE_END_TIME,USAGE_KWH,ESTIMATED_ACTUAL,CONSUMPTION_GENERATION\n123456,01\/01\/2020,01\/03\/2020 07:51:37,00:00,00:15,.043,A,Consumption\n123456,01\/01\/2020,01\/03\/2020 07:51:37,00:15,00:30,.042,A,Consumption\n123456,01\/01\/2020,01\/03\/2020 07:51:37,00:30,00:45,.064,A,Consumption\n123456,01\/01\/2020,01\/03\/2020 07:51:37,00:45,01:00,.043,A,Consumption\n123456,01\/01\/2020,01\/03\/2020 07:51:37,01:00,01:15,.043,A,Consumption\n123456,01\/01\/2020,01\/03\/2020 07:51:37,01:15,01:30,.043,A,Consumption\n123456,01\/01\/2020,01\/03\/2020 07:51:37,01:30,01:45,.042,A,Consumption\n123456,01\/01\/2020,01\/03\/2020 07:51:37,01:45,02:00,.052,A,Consumption\n<\/pre><\/div>\n\n\n<p>The first line is a header line that tells me what the other lines represent, which I will need to deal with when processing the file in my program.  I can see that the the columns relevant to my needs are<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>column 2: USAGE_DATE &#8212; this is the day of the year (necessary only if I want to restrict my comparison to a particular time of the year).<\/li><li>column 4: USAGE_START_TIME &#8212;  this is the start of a 15-minute period for a given date<\/li><li>column 6: USAGE_KWH &#8212; this is the amount of electricity used<\/li><\/ul>\n\n\n\n<p>Why do I care about USAGE_DATE?  I ended my employment on May 31, 2020 and am (at the time of this writing) at home during the daytime.  Since I am using historical data to predict future costs, I chose to use the data from the month of May since it also reflects me being home during the daytime.<\/p>\n\n\n\n<p>I think I know what to do now: for the month of May, 2020, get the electrical usage for each 15-minute time period and use it to calculate the cost if on Plan 1 and the cost if on Plan 2.  This leads to this pseudocode:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n\/*    open file\n      for each line of file\n        get date, time, kwh\n        if date in range\n          if time is day\n            update costs for plan 1 using day cost\n          else\n            update costs for plan 1 using night cost\n\n          update costs for plan 2\n      \n      close file *\/\n<\/pre><\/div>\n\n\n<br>\n\n\n\n<h3 class=\"wp-block-heading\">Final Program<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n#include &lt;stdlib.h&gt;\n\nint checkDate(char buffer&#x5B;], char* m, char t&#x5B;], double* kwh) ;\ndouble produceCostPlan1(char t&#x5B;], double kwh) ;\ndouble produceCostPlan2(double kwh) ;\n\nint main(void) {\n    FILE* fp;\n    char* filename = &quot;IntervalData.csv&quot;;\n    char buffer&#x5B;120];\n    char* month = &quot;05&quot;;  \/* use May *\/\n    char time&#x5B;6];\n    double kwh;\n    int dateMatch;\n    double costPlan1 = 3.42;  \/* base delivery charge *\/\n    double costPlan2 = 3.42;\n\n    if( (fp = fopen(filename, &quot;r&quot;)) == NULL ) {\n        printf(&quot;unable to open %s\\n&quot;, filename);\n        exit(1);\n    }\n\n    fgets(buffer, sizeof(buffer), fp);\n\n    while( fgets(buffer, sizeof(buffer), fp) != NULL ) {\n        dateMatch = checkDate(buffer, month, time, &amp;kwh);\n        if(dateMatch) {\n            costPlan1 += produceCostPlan1(time, kwh) ;\n            costPlan2 += produceCostPlan2(kwh) ;\n        }\n    }\n\n    fclose(fp);\n\n    printf(&quot;cost plan 1 = $%.2f\\n&quot;, costPlan1);\n    printf(&quot;cost plan 2 = $%.2f\\n&quot;, costPlan2);\n}\n\nint checkDate(char buffer&#x5B;], char* m, char t&#x5B;], double* kwh) {\n    \/*\n    ESIID,USAGE_DATE,REVISION_DATE,USAGE_START_TIME,USAGE_END_TIME,USAGE_KWH,ESTIMATED_ACTUAL,CONSUMPTION_GENERATION\n    123456,01\/01\/2020,01\/03\/2020 07:51:37,00:00,00:15,.043,A,Consumption\n    \n    ESIID                    : 123456\n    USAGE_DATE               : 01\/01\/2020\n    REVISION_DATE            : 01\/03\/2020 07:51:37\n    USAGE_START_TIME         : 00:00\n    USAGE_END_TIME           : 00:15\n    USAGE_KWH                : .043\n    ESTIMATED_ACTUAL         : A\n    CONSUMPTION_GENERATION   : Consumption\n    *\/\n\n    char *token, *del = &quot;,&quot;;\n    char dateField&#x5B;20];\n\n    token = strtok(buffer, del);    \/* meter ID, throw away *\/\n    token = strtok(NULL, del);      \/* date *\/\n    strcpy(dateField, token);\n    token = strtok(NULL, del);      \/* revision date, throw away *\/\n    token = strtok(NULL, del);      \/* start time *\/\n    strcpy(t, token);\n    token = strtok(NULL, del);      \/* end time, throw away *\/\n    *kwh = atof(strtok(NULL, del)); \/* kwh time *\/\n\n    \/* tokenize date field *\/\n    token = strtok(dateField, &quot;\/&quot;);\n\n    if( strcmp(token, m) == 0 )\n        return 1; \/* True, month matches *\/\n    else\n        return 0;\n}\n\n\ndouble produceCostPlan1(char t&#x5B;], double kwh){\n    \/*\n        plan 1: nights and weekends (&gt;= 8pm to &lt; 8am)\n    \n          base     $0\n          days     $0.088008 per kWh = 8.8008 cents per kWh\n          nights   $0.078008 per kWh = 7.8008 cents per kWh\n      \n          delivery $3.42 + $0.035448 per kWh\n    *\/\n\n    \/* check if daytime *\/\n    if( strcmp(&quot;08:00&quot;, t) &lt;= 0 &amp;&amp; strcmp(t, &quot;20:00&quot;) &lt; 0 ) \n        return kwh*0.088008 + kwh*0.035448;\n    else\n        return kwh*0.078008 + kwh*0.035448;\n}\n\n\ndouble produceCostPlan2(double kwh){\n    \/*\n        plan 2: Secure 36 plan\n    \n          base     $0\n          all day  $0.078341 per kWh = 7.8341 cents per kWh\n      \n          delivery $3.42 + $0.035448 per kWh\n    *\/\n\n    return kwh*0.078341 + kwh*0.035448;\n}\n\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>People learning to program often struggle with how to decompose a problem into the steps necessary to write a program to solve that problem. This is one of a series of posts in which I take a problem and go through my decision-making process that leads to a program. Background: In the spring of 2020, it was time to choose a new electrical plan and with my current provider I basically had two options. My home has a smart meter,&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.brezeale.com\/?p=590\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[6,9],"tags":[],"class_list":["post-590","post","type-post","status-publish","format-standard","hentry","category-c-programming","category-programming"],"_links":{"self":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts\/590","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=590"}],"version-history":[{"count":7,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts\/590\/revisions"}],"predecessor-version":[{"id":598,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts\/590\/revisions\/598"}],"wp:attachment":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}