Writing C Program(s) Using Dynamic Data Structures

// approach is to use recursion.

Listing 17.1 The films1.c Program/* films1.c — using an array of structures */#include <stdio.h>#include <string.h>#define TSIZE 45 /* size of array to hold title */#define FMAX 5 /* maximum number of film titles */struct film {char title[TSIZE];int rating;};char * s_gets(char * st, int n);int main(void){struct film movies[FMAX];int i = 0;int j;puts(“Enter first movie title:”);while (i < FMAX && s_gets(movies[i].title, TSIZE) != NULL &&movies[i].title[0] != ‘’){puts(“Enter your rating <0-10>:”);scanf(“%d”, &movies[i++].rating);while(getchar() != ‘n’)continue;puts(“Enter next movie title (empty line to stop):”);}if (i == 0)printf(“No data entered. “);elseprintf (“Here is the movie list:n”);for (j = 0; j < i; j++)printf(“Movie: %s Rating: %dn”, movies[j].title,movies[j].rating);

printf(“Bye!n”);return 0;}char * s_gets(char * st, int n){char * ret_val;char * find;ret_val = fgets(st, n, stdin);if (ret_val){find = strchr(st, ‘n’); // look for newlineif (find) // if the address is not NULL,*find = ‘’; // place a null character thereelsewhile (getchar() != ‘n’)continue; // dispose of rest of line}return ret_val;}

Leave a Comment

Your email address will not be published. Required fields are marked *

Writing C Program(s) Using Dynamic Data Structures

// approach is to use recursion.

Listing 17.1 The films1.c Program/* films1.c — using an array of structures */#include <stdio.h>#include <string.h>#define TSIZE 45 /* size of array to hold title */#define FMAX 5 /* maximum number of film titles */struct film {char title[TSIZE];int rating;};char * s_gets(char * st, int n);int main(void){struct film movies[FMAX];int i = 0;int j;puts(“Enter first movie title:”);while (i < FMAX && s_gets(movies[i].title, TSIZE) != NULL &&movies[i].title[0] != ‘’){puts(“Enter your rating <0-10>:”);scanf(“%d”, &movies[i++].rating);while(getchar() != ‘n’)continue;puts(“Enter next movie title (empty line to stop):”);}if (i == 0)printf(“No data entered. “);elseprintf (“Here is the movie list:n”);for (j = 0; j < i; j++)printf(“Movie: %s Rating: %dn”, movies[j].title,movies[j].rating);

printf(“Bye!n”);return 0;}char * s_gets(char * st, int n){char * ret_val;char * find;ret_val = fgets(st, n, stdin);if (ret_val){find = strchr(st, ‘n’); // look for newlineif (find) // if the address is not NULL,*find = ‘’; // place a null character thereelsewhile (getchar() != ‘n’)continue; // dispose of rest of line}return ret_val;}

Leave a Comment

Your email address will not be published. Required fields are marked *

Is this question part of your Assignment?

Get expert help

Girl in a jacket


We are a team of academic consultants with extensive experience in writing academic papers for college students in the US, Canada, UK, AU, and other parts of the world.

We help students with both technical and non-technical assignments across all majors & academic disciplines.

Unlike what our name suggests, we research and draft everything word for word. We do not use AI or any rewriting tool! We provide Turnitin reports for AI & Turnitin alongside every paper.

Need help? Send us your assignment now!

description here description here description here