due in a few hours, written in c if you look up \"movie structure file written i
ID: 3720204 • Letter: D
Question
due in a few hours, written in c if you look up "movie structure file written in c chegg" the top link is similar to how the code is supposed to be but the person's answer doesn't work/compile correctly
2 Name: Date: Movie Structure File write a program that will read in a CSV file with information regarding movies. The file will contain a movie name, MPAA coding (G, PG, PG-13, R), number of minutes, and ratings (1-10) from the three top critics. Your program will read in the data into an array of structures and send the array to a function that will professionally display all of the information, including the average rating of the critics. Show me this program working!Explanation / Answer
As per your requirement i have written c code which satifies all your requirement please follow it step by step.
Here below one is the C program and resultant output.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxstrlength 256
typedef struct
{
char name[50];
char codingElement[6];
int mintuesElement;
int rating[3];
}movie;
int read_file(FILE *infile, movie movies[]);
void display(movie movies[], int countElement);
int main()
{
FILE * infile;
movie movies[100]; /*max 100 movies in the array*/
int countElement ;
infile = fopen("movies.csv", "r");
if(!infile)
{
printf("Unable to open file ");
return 1;
}
countElement = read_file(infile, movies);
fclose(infile);
display(movies, countElement);
printf(" Done. ");
return 0;
}
int read_file(FILE *infile, movie movies[])
{
char buffer[maxstrlength];
char *token;
char separatorElement[2] = ",";
int countElement = 0;
int i;
while(fgets(buffer, maxstrlength - 1, infile))
{
//remove the from the end
if ((strlen(buffer)>0) && (buffer[strlen (buffer) - 1] == ' '))
buffer[strlen (buffer) - 1] = '';
token = strtok(buffer, separatorElement);
strcpy(movies[countElement].name, token);//copy the token to movie name
//extract next token i.e. codingElement
token = strtok(NULL, separatorElement);
strcpy(movies[countElement].codingElement, token);
//extract next token i.e. movie length
token = strtok(NULL, separatorElement);
movies[countElement].mintuesElement = atoi (token);
//extract the 3 ratings
for(i = 0; i < 3; i++)
{
token = strtok(NULL, separatorElement);
movies[countElement].rating[i] = atoi (token);
}
countElement++;
}
return countElement;
}
void display(movie movies[], int countElement)
{
int i;
double avg;
printf(" %-40s %-15s %15s %10s %10s %10s %20s", "Movie Name", "Coding", "Length(min)","Rating 1",
"Rating 2","Rating 3", "Average Rating");
printf(" %-40s %-15s %15s %10s %10s %10s %20s", "==========", "======", "===========","========",
"========","========", "============== ");
for( i = 0; i < countElement; i++)
{
avg = (movies[i].rating[0] + movies[i].rating[1] + movies[i].rating[2] ) / 3.0;
printf(" %-40s %-15s %15d %10d %10d %10d %20.2f", movies[i].name , movies[i].codingElement, movies[i].mintuesElement,
movies[i].rating[0], movies[i].rating[1], movies[i].rating[2], avg);
}
}
input file movies.csv
movie1,G,120,5,5,6
movie2,PG,120,9,9,8
movie3,PG-13,120,7,9,6
movie4,R,120,8,9,8
output
Movie Name Coding Length(min) Rating 1 Rating 2 Rating 3 Average Rating
========== ====== =========== ======== ======== ======== ==============
movie1 G 120 5 5 6 5.33
movie2 PG 120 9 9 8 8.67
movie3 PG-13 120 7 9 6 7.33
movie4 R 120 8 9 8 8.33
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.