Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have this program that needs to be C89 compliant that reads a file from command line (Ex: ./a.out filename.csv). Here is my code but

I have this program that needs to be C89 compliant that reads a file from command line (Ex: ./a.out filename.csv). Here is my code but it has 3 errors that I would also liked fixed. I need help with the program to give the expected output (attached).

Code: ************************************************

#include

#include

#include

struct node {

char startTime [10];

char endTime [10];

char days [15];

int classCount;

};

int read (struct node array[], char* filename);

char* getToken(char buffer[], int position);

char* getString(char buffer[], int position);

int findClass(struct node array[], char* start, char* end, char* days, int size);

void printArray(struct node array[], int size);

void sortArray (struct node array[], int size);

int main (int argc, char* argv[])

{

struct node array [100];

int count;

if (argc !=2){

printf("usage: executable filename ");

return -1;

}

count = read(array, argv[1]);

sortArray(array, count);

printArray(array, count);

return 0;

}

int read (struct node array[], char* filename)

{

char buffer [100];

char temp [100];

char *token;

char *start, *end, *days;

int count =0, index;

FILE* fp;

if((fp=fopen(filename, "r"))==NULL){

printf("Unable to open file. ");

return -1;

}

/*column heading*/

fgets(buffer, sizeof(buffer), fp);

while(fgets(buffer, sizeof(buffer), fp) != NULL){

strcpy(temp, buffer); /*clean copy*/

/*gets the start time*/

start = getToken(temp, 11); /*this maybe 10? */

printf("%s--", start);

/*gets the end time*/

strcpy(temp, buffer);

end = getToken(temp, 12);

printf("%s", end);

/*gets the weekday schedule*/

strcpy(temp, buffer);

days = getString(temp, 13);

if((index = findClass( array, start, end, days, count)) == -1)

{

/*unseen class time/days*/

array[count].startTime = start;

array[count].endTime = end;

array[count].days = days;

array[count].classCount = 1;

count++;

}

else{

array[index].classCount +=1;

}

} fclose(fp);

return count;

}

char* getToken(char buffer[], int pos){

int i, tracker;

char days;

char *del =", ";

char *token = strtok(buffer, del);

for(i = 1; i

token = strtok(NULL, del);

return token;

}

char* getString(char buffer[], int pos){

int i;

char *days;

char *del =", ";

char *token = strtok(buffer, del);

for(i=1; i

token = strtok(NULL, del);

}

if (strcmp(token,"Y")== 0){

days = "Mo";

}

token = strtok(NULL,del);

if (strcmp(token,"Y")== 0){

days = strcat(days, "Tu");

}

token = strtok(NULL,del);

if (strcmp(token,"Y")== 0){

days = strcat(days,"We");

}

token = strtok(NULL,del);

if (strcmp(token,"Y")== 0){

days = strcat(days, "Th");

}

token = strtok(NULL,del);

if (strcmp(token,"Y")== 0){

days = strcat(days,"Fr");

}

token = strtok(NULL,del);

if (strcmp(token,"Y")== 0){

days = strcat(days,"Sa");

}

token = strtok(NULL,del);

if (strcmp(token,"Y")== 0){

days = strcat(days,"Su");

}

token = strtok(NULL,del);

return days;

}

int findClass(struct node array[], char* start, char* end, char* days, int size){

int i;

for(i=0; i

if(array[i].startTime== start && array[i].endTime==end

&& array[i].days == days)

return i;

return -1;

}

void sortArray(struct node array[], int size){

int i, sort;

struct node temp;

do{

sort = 1;

for(i=0; i

if(array[i].classCount > array[i+1].classCount){

temp = array[i];

array[i] = array[i+1];

array[i+1] = temp;

sort = 0;

}

/*NEED TO DO STRCMP here because start time is a string*/

else if (array[i].classCount == array[i+1].classCount){

if(array[i].startTime

temp = array[i];

array[i] = array[i+1];

array[i+1] = temp;

sort = 0;

}

}

}while( !sort);

}

void printArray(struct node array[], int size){

int i;

int classCount =0;

for(i=0; i

if(array[i].classCount == 1)

printf("%s-- %s, %s, %d class ", array[i].startTime,

array[i].endTime, array[i].days, array[i].classCount);

else

printf("%s-- %s, %s, %d class ", array[i].startTime,

array[i].endTime, array[i].days, array[i].classCount);

}

}

Partial OUTPUT:

image text in transcribed

Partial File to read (Output and file are very long):

image text in transcribed

Also here are the errors I am getting that I would like help with too, please:

image text in transcribed

11:00 AM--12:20 PM, TuTh, 162 classes 9:30 AM--10:50 AM, TuTh, 157 classes 12:30 PM-_ 1:50 PM, TuTh, 140 classes 10:00 AM--10:50 AM, MoWeFr, 124 classes 11:00 AM--11:50 AM, MoWeFr, 120 classes 2:00 PM3:20 PM, TuTh, 113 classes 9:00 AM-- 9:50 AM, MoWeFr, 107 classes 3:30 PM4:50 PM, TuTh, 87 classes 5:30 PM6:50 PM, MoWe, 84 classes 5:30 PM6:50 PM, TuTh, 83 classes 8:00 AM-- 9:20 AM, TuTh, 77 classes 2:30 PM3:50 PM, MoWe, 73 classes 1:00 PM2:20 PM, MoWe, 71 classes 4:00 PM5:20 PM, MoWe, 61 classes 1:00 PM-_ 1:50 PM, MoWe Fr, 59 classes 7:00 PM- 9:50 PM, Tu, 42 classes 7:00 PM9:50 PM, Mo, 38 classes 8:00 AM-_ 8:50 AM, MOWeFr, 37 classes 7:00 PM- 9:50 PM, We, 7:00 PM8: 20 PM, MoWe, 33 classes 7:00 PM9:50 PM, Th, 33 classes 2:00 PM-4:50 PM, Tu, 25 classes 7:00 PM8:20 PM, TuTh, 23 classes 2:00 PM-2:50 PM, MoWeFr, 18 classes 5:30 PM8:20 PM, Tu, 17 classes 4:00 PM6:50 PM, Mo, 15 classes 2:00 PM-4:50 PM, Th, 13 classes 5:30 PM8: 20 PM, Th, 13 classes 4:00 PM6:50 PM, We, 12 classes 1:00 PM 1:50 PM, Mowe, 11 classes 3:00 PM-_ 5:50 PM, Mo, 11 classes 6:00 PM 8:50 PM, Tu, 10 classes 8:00 AM--10:50 AM, Tu, 9 classes 36 classes 11:00 AM-_ 1:50 PM, Th, 9 classes 1:00 PM-_ 3:50 PM, Mo, 9 classes 2:00 PM-_ 4:50 PM, Mo, 9 classes 1:00 PM-_ 3:50 PM, Fr, 8 classes 1:00 PM-3:50 PM, MoTuWeThFr, 8 classes 2:00 PM-_ 4:50 PM, We, 8 classes 5:30 PM-_ 8:20 PM, Mo, 8 classes 6:00 PM 8:50 PM, Mo, 8 classes 8:00 AM--10:50 AM, Th, 7 classes 4:00 PM-_ 6:50 PM, Fr, 7 classes 6:00 PM 8:50 PM, Th, 7 classes 8: 00 AM-_10: 50 AM, Mo, 6 classes 11:00 AM11:50 AM, Mowe, 6 classes

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Database Technology Edbt 88 International Conference On Extending Database Technology Venice Italy March 14 18 1988 Proceedings Lncs 303

Authors: Joachim W. Schmidt ,Stefano Ceri ,Michele Missikoff

1988th Edition

3540190740, 978-3540190745

More Books

Students also viewed these Databases questions

Question

what is a podcast

Answered: 1 week ago

Question

What is meant by decentralisation of authority ?

Answered: 1 week ago

Question

Briefly explain the qualities of an able supervisor

Answered: 1 week ago

Question

Define policy making?

Answered: 1 week ago

Question

Define co-ordination?

Answered: 1 week ago

Question

What are the role of supervisors ?

Answered: 1 week ago