Write a program that will read data about n movies (data about a movie is defined in structure).
Sort data about movies (criteria of sorting is by title of a movie and by surname and name of an actor).
Read data about one actor, and print data about all movies that contain read actor.

How to implement function for printing data about all movies that contain one read actor?

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 50

typedef struct
{
    char name[MAX];
    char surname[MAX];
}ACTOR;

typedef struct
{
    char title[MAX];
    int length,year;
    int numberOfActors;
    ACTOR *actors;
}MOVIE;

ACTOR readActor();
MOVIE* readMovies(int *n);
void sort(MOVIE *arr,int n);
void print(MOVIE *arr,int n, ACTOR *act);

ACTOR readActor()//read data about one actor
{
    ACTOR act;
    printf("Name: ");
    scanf("%s",act.name);
    printf("Surname: ");
    scanf("%s",act.surname);
    return act;
}

MOVIE* readMovies(int *n)//read data about movies with actors
{
    MOVIE *me;
    do
    {
        printf("n=");
        scanf("%d",n);
    }
    while(*n < 1);
    me=(MOVIE*)malloc(*n * sizeof(MOVIE));
    me->actors=(ACTOR*)malloc(me->numberOfActors * sizeof(ACTOR));
    int i,j;
    for(i = 0; i < *n; i++)
    {
        printf("%d. movie: \n",i+1);
        fflush(stdin);
        printf("Title: ");
        gets(me->title);
        printf("Year: ");
        scanf("%d",&me->year);
        printf("Length: ");
        scanf("%d",&me->length);
        printf("Number of actors: ");
        scanf("%d",&me->numberOfActors);
        for(j = 0; j < me->numberOfActors; j++)
        {
            printf("%d. actor: \n",j+1);
            me->actors[j]=readActor();
        }
    }
    return me;
}

void sort(MOVIE *arr,int n)//sort data by title, surname and name
{
    int i,j,u,v;
    for(i = 0; i < n-1; i++)
    {
        for(j = i+1; j < n; j++)
        {
           if(strcmp(arr[i].title,arr[j].title) > 0)//compare movie title
           {
               MOVIE temp1=arr[i];
               arr[i]=arr[j];
               arr[j]=temp1;
           }
         for(u = 0; u < arr->numberOfActors - 1; u++)
         {
             for(v = u+1; v < arr->numberOfActors; v++)
             {
                 if(strcmp(arr->actors[i].surname,arr->actors[j].surname) > 0)//if surname of actors are different
                 {
                    ACTOR temp2=arr->actors[i];
                    arr->actors[i]=arr->actors[j];
                    arr->actors[j]=temp2;
                 }
                 else if((strcmp(arr->actors[i].surname,arr->actors[j].surname) == 0)//if surnames are the same, sort by name
                         && (strcmp(arr->actors[i].name,arr->actors[j].name) > 0))
                 {
                    ACTOR temp2=arr->actors[i];
                    arr->actors[i]=arr->actors[j];
                    arr->actors[j]=temp2;
                 }
                 else if((strcmp(arr->actors[i].surname,arr->actors[j].surname) == 0)//break if name and surname are the same
                         && (strcmp(arr->actors[i].name,arr->actors[j].name) == 0))
                    break;
             }
         }
        }
    }
}

void print(MOVIE *arr,int n, ACTOR *act)//print movies with specific actor
{
    //...//
}

int main()
{
    MOVIE *me;
    int n;
    me=readMovies(&n);
    MOVIE *arr;
    arr=(MOVIE*)malloc(n * sizeof(MOVIE));
    sort(arr,n);
    return 0;
}

Line 41 is incorrect. It should be scanf("%d", *n);. Go from there, and review all your code for incorrect usage of pointers, etc. Another mistake is on line 44. You are allocating n number of movies. I don't think that was your intention (or was it?). Anyway, there are a LOT of this sort of mistake in your code. Fix it and post your corrections for further comment.

Edited 11 Months Ago by rubberman

For line 52 ... do NOT use unsafe 'gets' ... you can use fgets instead (or use my readLine to read in dynamic C strings of any length, if you wish to use dynamic C strings.)

http://developers-heaven.net/forum/index.php/topic,2580.msg2864.html#msg2864

@rubberman note wrt line 41:

/* OP is passing in address of n */
MOVIE* readMovies(int *n)//read data about movies with actors
{
    MOVIE *me;
    do
    {
        printf("n=");
        scanf("%d",n); /* line 41 ... NOTE 'n' is an address already */
    }

    /* ... */

How to implement function for printing data about all movies that contain one read actor?

Traverse the array of movies ... for each movie ... if the number of actors in that movie is 1, then print out that movie info.

Edited 10 Months Ago by David W

This article has been dead for over six months. Start a new discussion instead.