Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reads a file consisting of restaurant ratings and the date

ID: 3913247 • Letter: W

Question

Write a program that reads a file consisting of restaurant ratings and the date the rating was submitted.

It should then determine the number of ratings in each of the following ranges : 1 - 2 (Unacceptable), 3 - 4 (Poor), 5 - 6 (Average), 7 -8 (above average), 9 -10 (very good).

The earliest and latest submission date of each rating range must also be shown. A sample output is :

                          TUSCAN GARDEN CUSTOMER RATING REPORT

Rating                      Total             Earliest Date        Latest Date

1 - 2 (Unacceptable)                2                  12/10/16                12/21/16

3 - 4 (Poor)                              5                  12/07/16                01/18/17

5 - 6 (Average)                      12                  11/30/16                02/21/17

7 - 8 (above average)            29                   12/11/16               03/30/17

9 -10 (very good).                  17                   01/20/17               03/31/17   

Notes :

The program must use at least 2 functions :

A function that takes as parameters an array and the number of elements (dates) loaded into it and returns the earliest date. (see #3 below)

A function that takes as parameters an array and the number of elements (dates) loaded into it and returns the latest date. (see #3 below)

Potential pseudocode solution :

declare 5 string arrays (one for each rating)

read first input record

if (inFile.eof())

    output message “The input file is empty”

else

    output heading lines

while (!inFile.eof())

use rating to load ‘date information’ into the appropriate array

(be sure to keep track of how many elements are loaded into each array)

for each rating, do the following :

call earliest date function

call latest date function

output detail line

declare functions

3. The logic for the 2 function is similar to the indexLargestElement function logic in Example 8-6 in the textbook on pages 534 – 535. (Edition 6, pages 520 -521) MAlik c++

Sample data file:

05 11/30/17
03 12/07/17
05 12/07/17
05 12/08/17
01 12/10/17
07 12/11/17
07 12/14/17
06 12/15/17
02 12/21/17
05 12/21/17
06 12/22/17
07 12/22/17
08 12/23/17
07 12/23/17
07 12/23/17
07 12/23/17
08 12/24/17
08 12/24/17
07 12/24/17
03 12/26/17
05 12/26/17
07 12/28/17
04 12/29/17
07 01/01/18
06 01/03/18
07 01/03/18
08 01/05/18
05 01/10/18
04 01/18/18
08 01/18/18
07 01/18/18
07 01/18/18
08 01/18/18
08 01/18/18
08 01/18/18
07 01/18/18
04 01/18/18
05 01/18/18
06 01/19/18
10 01/20/18
08 01/21/18
10 01/21/18
09 01/21/18
09 01/21/18
10 01/21/18
07 01/21/18
07 01/21/18
06 02/21/18
09 02/22/18
09 02/22/18
09 02/22/18
07 02/25/18
09 02/26/18
08 02/28/18
07 03/03/18
10 03/03/18
10 03/04/18
09 03/05/18
07 03/10/18
09 03/15/18
09 03/20/18
10 03/22/18
07 03/30/18
09 03/30/18
09 03/31/18

**please //comment as nearly as much as possible to help students learn step by step** Thank you and good luck, may the best person answer!

Explanation / Answer

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// internal date representation. easy to compare
struct intdate {
   int day;
   int month;
   int year;
};

// structure to keep ratings together
struct ratings {
   // keep count in each array; count of dates in 0th array is in count[0] and so on
   int count[5];
   struct intdate intdates[5][1024];
};

int load_file(char *name, struct ratings *ratings)
{
   FILE *in = fopen(name, "r+");
   char buf[256];
   char *rat, *date;
   int nrat, day, month, year;
   char *d;
   if (in == NULL) {
       perror("error opening input file");
       return -1;
   }

   // while not end of file read the records
   while(!feof(in)) {
       if (fgets(buf, 256, in) == NULL) {
           break;
       } else {
           // parse each line; use strtok to parse; first token is rating, second is date
           rat = strtok(buf, " ");
           date = strtok(NULL, " ");
           // convert ratings into integer
           nrat = atoi(rat);
           // tokenize the date to day, month and year values
           d = strtok(date, "/ ");
           month = atoi(d);
           d = strtok(NULL, "/ ");
           day = atoi(d);
           d = strtok(NULL, "/ ");
           year = atoi(d);
           if (nrat == 1 || nrat == 2) {
               // date goes into the first array
               ratings->intdates[0][ratings->count[0]].month = month;
               ratings->intdates[0][ratings->count[0]].day = day;
               ratings->intdates[0][ratings->count[0]++].year = year;
           } else if (nrat == 3 || nrat == 4) {
               // date goes into the second array
               ratings->intdates[1][ratings->count[1]].month = month;
               ratings->intdates[1][ratings->count[1]].day = day;
               ratings->intdates[1][ratings->count[1]++].year = year;
           } else if (nrat == 5 || nrat == 6) {
               // date goes into the third array
               ratings->intdates[2][ratings->count[2]].month = month;
               ratings->intdates[2][ratings->count[2]].day = day;
               ratings->intdates[2][ratings->count[2]++].year = year;
           } else if (nrat == 7 || nrat == 8) {
               // date goes into the fourth array
               ratings->intdates[3][ratings->count[3]].month = month;
               ratings->intdates[3][ratings->count[3]].day = day;
               ratings->intdates[3][ratings->count[3]++].year = year;
           } else if (nrat == 9 || nrat == 10){
               // date goes into the fifth array
               ratings->intdates[4][ratings->count[4]].month = month;
               ratings->intdates[4][ratings->count[4]].day = day;
               ratings->intdates[4][ratings->count[4]++].year = year;
           }
       }
   }
   return 0;

}

int compare_dates (struct intdate date1, struct intdate date2)
{
   if (date1.year > date2.year) {
       return 1;
   }
   if ((date1.year == date2.year) && (date1.month > date2.month)) {
       return 1;
   }
   if ((date1.year == date2.year) && (date1.month == date2.month) && (date1.day > date2.day)) {
       return 1;
   }
   if ((date1.year == date2.year) && (date1.month == date2.month) && (date1.day == date2.day)) {
       return 0;
   }
   return -1;
}

int earliest_date(struct intdate intdates[], int len)
{
   int i;
   int earliest = 0;
   struct intdate d;
   if (len == 0) {
       printf("earliest_date(): empty input array ");
       return -1;
   }
   earliest = 0;
   for (i=1; i < len; i++) {
       d = intdates[i];
       if (compare_dates(intdates[i], d) == -1) {
           earliest = i;
       }
   }
   return earliest;
}

int latest_date(struct intdate intdates[], int len)
{
   int i;
   int latest = 0;
   struct intdate d;
   if (len == 0) {
       printf("latest_date(): empty input array ");
       return -2;
   }
   for (i=1; i < len; i++) {
       d = intdates[i];
       if (compare_dates(intdates[i], d) >= 0) {
           latest = i;
       }
   }
   return latest;
}

int main()
{
   struct ratings ratings;
   int i, earliest, latest;
   for (i = 0; i < 5; i++) {
       ratings.count[i] = 0;
   }
   if (load_file("sample.dat", &ratings) != 0) {
       printf("error loading sample.dat");
   }

   printf(" TUSCAN GARDEN CUSTOMER RATING REPORT ");
   printf("Rating Total Earliest Date Latest Date ");

   earliest = earliest_date(ratings.intdates[0], ratings.count[0]);
   latest = latest_date(ratings.intdates[0], ratings.count[0]);
   printf("1-2(unacceptable) %d %02d/%02d/%02d %02d/%02d/%02d ",
           ratings.count[0],
           ratings.intdates[0][earliest].month,
           ratings.intdates[0][earliest].day,
           ratings.intdates[0][earliest].year,
           ratings.intdates[0][latest].month,
           ratings.intdates[0][latest].day,
           ratings.intdates[0][latest].year);

   earliest = earliest_date(ratings.intdates[1], ratings.count[1]);
   latest = latest_date(ratings.intdates[1], ratings.count[1]);
   printf("3-4(poor) %d %02d/%02d/%02d %02d/%02d/%02d ",
           ratings.count[1],
           ratings.intdates[1][earliest].month,
           ratings.intdates[1][earliest].day,
           ratings.intdates[1][earliest].year,
           ratings.intdates[1][latest].month,
           ratings.intdates[1][latest].day,
           ratings.intdates[1][latest].year);

   earliest = earliest_date(ratings.intdates[2], ratings.count[2]);
   latest = latest_date(ratings.intdates[2], ratings.count[2]);
   printf("5-6(average) %d %02d/%02d/%02d %02d/%02d/%02d ",
           ratings.count[2],
           ratings.intdates[2][earliest].month,
           ratings.intdates[2][earliest].day,
           ratings.intdates[2][earliest].year,
           ratings.intdates[2][latest].month,
           ratings.intdates[2][latest].day,
           ratings.intdates[2][latest].year);

   earliest = earliest_date(ratings.intdates[3], ratings.count[3]);
   latest = latest_date(ratings.intdates[3], ratings.count[3]);
   printf("7-8(above average) %d %02d/%02d/%02d %02d/%02d/%02d ",
           ratings.count[3],
           ratings.intdates[3][earliest].month,
           ratings.intdates[3][earliest].day,
           ratings.intdates[3][earliest].year,
           ratings.intdates[3][latest].month,
           ratings.intdates[3][latest].day,
           ratings.intdates[3][latest].year);

   earliest = earliest_date(ratings.intdates[4], ratings.count[4]);
   latest = latest_date(ratings.intdates[4], ratings.count[4]);
   printf("7-8(above average) %d %02d/%02d/%02d %02d/%02d/%02d ",
           ratings.count[4],
           ratings.intdates[4][earliest].month,
           ratings.intdates[4][earliest].day,
           ratings.intdates[4][earliest].year,
           ratings.intdates[4][latest].month,
           ratings.intdates[4][latest].day,
           ratings.intdates[4][latest].year);
}