Writing C Program(s) Using Dynamic Data Structures
| // C Primer Plus |
| // Chapter 17 Exercise 1: |
| // Modify Listing 17.2 so that it displays the movie list both in the original |
| // order and in reverse order. One approach is to modify the linked-list |
| // definition so that the list can be traversed in both directions. Another |
// 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] != '\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. ");
else
printf ("Here is the movie list:\n");
for (j = 0; j < i; j++)
printf("Movie: %s Rating: %d\n", 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 newline
if (find) // if the address is not NULL,
*find = '\0'; // place a null character there
else
while (getchar() != '\n')
continue; // dispose of rest of line
}
return ret_val;
}
9 years ago 10
Purchase the answer to view it
- exercise.zip
- Develop an implementation plan (1,500-2,000 words) using the "Topic 3: Checklist" resource. The elements that should be included in your plan are listed below:
- MGT 420 Week 1 Individual Assignment Role of Stakeholder Paper
- how about this
- HCA 250 Final Psychology and Health Issues
- COM 530 WEEK 3 ORGANIZATIONAL COMMITMENT AND COMMUNICATION PAPER
- BUS 521 Assignment4
- Strategic plan for chosen Organization
- A researcher predicts that watching a film on institutionalization will change studentsâ attitudes about chronically mentally ill patients. The researcher...
- COM 285 Individual Business Communication Trends
- CJS 230 Week 8 Assignment 4 Rehabilitation