Home > Programming > Functions using the "C" programming language > example10.c

 

Previous

Next


/*
    In this example we demonstrate how to pass two struct parameters to a function.
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

struct STUDENT {
    char name[20];
    int grade;
};

/*
    Function prototype
    If the two students have the save grade, function compare_Students returns 0
*/
int compare_Students(struct STUDENT, struct STUDENT);

void main(void)
{
    struct STUDENT Tom;
    struct STUDENT Kate;
/* Seed the random number generator with current time */
    srand( (unsigned)time( NULL ));
    strcpy(Tom.name,"Tom");
    Tom.grade = (int)(((long)rand()*10)/(RAND_MAX+1));
    strcpy(Kate.name,"Kate");
    Kate.grade = (int)(((long)rand()*10)/(RAND_MAX+1));
    if (!compare_Students(Tom,Kate))
    {
        printf("%s and %s have equal grades : %d\n",Tom.name,Kate.name,Tom.grade);
    }
    else
    {
        printf("%s grade %d\n",Tom.name,Tom.grade);
        printf("%s grade %d\n",Kate.name,Kate.grade);
    }
}

/*
    Function compare_Students source
*/
int compare_Students(struct STUDENT s1, struct STUDENT s2)
{
    return s1.grade - s2.grade;
}


© 2004 Jim Valavanis

Previous

Next

1