{"id":2401,"date":"2017-03-08T00:01:51","date_gmt":"2017-03-08T08:01:51","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2401"},"modified":"2017-03-04T09:29:44","modified_gmt":"2017-03-04T17:29:44","slug":"detailed-examination-of-a-csv-file-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2401","title":{"rendered":"Detailed Examination of a CSV File &#8211; Solution"},"content":{"rendered":"<p>The task for <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2384\">this month&#8217;s Exercise<\/a> is to read a CSV file, store the data, then manipulate and report. For my solution, I chose to create an array of structures in which to store the weather information.<br \/>\n<!--more--><br \/>\nHere is the nested structure I used in my solution:<\/p>\n<pre class=\"screen\">\r\nstruct calendar {\r\n    int year;\r\n    char *month;\r\n    int day;\r\n};\r\nstruct temperature_data {\r\n    struct calendar date;\r\n    float high;\r\n    float low;\r\n};<\/pre>\n<p>I could have put all the data into a single-level structure, but whenever I see a date as part of the data, I create a nested structure. Above, the <code>calendar<\/code> structure has <code>year<\/code>, <code>month<\/code>, and <code>day<\/code> members. The <code>month<\/code> member is a pointer, which references an array of month strings. It could have easily been a <em>char<\/em> array instead.<\/p>\n<p>The structure is defined externally, which allows all the program&#8217;s various functions to access the <code>temperature_data struct<\/code> variable. In the <em>main()<\/em> function, a structure array is declared:<\/p>\n<p><code>struct temperature_data winter[RECORDS];<\/code><\/p>\n<p>As with <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2355\">last month&#8217;s Solution<\/a>, the CSV file is read, but this time its values are stored in the <code>winter[]<\/code> array. The file is closed, and a series of <em>printf()<\/em> statements coupled with various functions generate the required output.<\/p>\n<p>Here&#8217;s sample output from my solution:<\/p>\n<pre><code>Date range from December 25, 2016 to January 24, 2017:\r\nThe average high was 29.16\r\nThe average low was 14.48\r\nThe highest high was 41.0 on January 20, 2017\r\nThe lowest high was 16.0 on January 3, 2017\r\nThe highest low was 32.0 on January 22, 2017\r\nThe lowest low was -3.0 on January 12, 2017<\/code><\/pre>\n<p>The first line simply pulls the date from the first and last elements of the <code>winter[]<\/code> array:<\/p>\n<pre class=\"screen\">\r\n    printf(\"Date range from %s %d, %d to %s %d, %d:\\n\",\r\n            winter[0].date.month,\r\n            winter[0].date.day,\r\n            winter[0].date.year,\r\n            winter[RECORDS-1].date.month,\r\n            winter[RECORDS-1].date.day,\r\n            winter[RECORDS-1].date.year\r\n            );<\/pre>\n<p>The <code>RECORDS<\/code> constant is set to 31, the number of records in the <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/02\/weather_data.csv\">weather_data.csv<\/a> file. This first date is found in element zero of the array; the last date is at the value <code>RECORDS<\/code> minus one.<\/p>\n<p>The next six lines of output rely upon six functions I wrote to manipulate the data:<\/p>\n<pre class=\"screen\">\r\nfloat average_high(struct temperature_data w[]);\r\nfloat average_low(struct temperature_data w[]);\r\nint highest_high(struct temperature_data w[]);\r\nint lowest_high(struct temperature_data w[]);\r\nint highest_low(struct temperature_data w[]);\r\nint lowest_low(struct temperature_data w[]);<\/pre>\n<p>The first two functions calculate the average temperatures for the <code>weather[].high<\/code> and <code>weather[].low<\/code> members of the structure array, represented as <code>w[]<\/code> inside the functions.<\/p>\n<p>Remember: You must declare the <code>temperature_data<\/code> structure <strong>before<\/strong> you prototype the functions!<\/p>\n<p>The remaining functions crunch the numbers to return the specific element number for the minimum and maximum values as required. The element number is used to generate the output that shows the temperature and date.<\/p>\n<p>The highest\/lowest functions are pretty basic, with subtle changes between each one to get the specific values. I wrote the first one, <em>highest_high()<\/em>, and then copied-and-pasted it to help create the others.<\/p>\n<p>To view my entire solution, <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/02\/03exercise.c\">click here<\/a>. It&#8217;s long: 233 lines, but again the final four functions are similar.<\/p>\n<p>Your solution doesn&#8217;t need to be identical to mine. If your output matches the results shown earlier in this post, you&#8217;re good.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The task for this month&#8217;s Exercise is to read a CSV file, store the data, then manipulate and report. For my solution, I chose to create an array of structures in which to store the weather information.<\/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-2401","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\/2401","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=2401"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2401\/revisions"}],"predecessor-version":[{"id":2417,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2401\/revisions\/2417"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}