/* *******************************************************
* Filename : structure_example.c
* Author : Burak Kaleci
* Date : 12.04.2017
* Description : An example program to use structures with functions
* ******************************************************/
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 5
// structure definition
struct student{
int idNumber;
char firstName[100];
char lastName[100];
char gender;
int age;
double GPA;
};
// function prototypes
void printStudent(struct student p);
double calculateAvgGPA(double s1,double s2);
void determineGender(char g, char* n);
void printStudentArray(struct student *pArray);
int countMaleStudent(struct student *pArray);
/* function main begins program execution each program must include main function */
int main(void)
{
//define student type variable and initialize it by using list
struct student aStudent={2037,"Burak","Kaleci",'M',32,3.55};
//define student type variable and initialize it by using dot operator
struct student aStudent2;
aStudent2.idNumber=1000;
//aStudent2.firstName="Ayse"; ERROR
//aStudent2.lastName="Yilmaz"; ERROR
strcpy(aStudent2.firstName,"Ayse");
strcpy(aStudent2.lastName,"Yilmaz");
aStudent2.gender='F';
aStudent2.age=18;
aStudent2.GPA=2.98;
// call function to print student structure type variable
// by passing an entire structure
printStudent(aStudent);
printStudent(aStudent2);
// you can use structure members individually
// call function to calculate average of two students by using GPA member
// by passing individual structure members
double GPAAvg=calculateAvgGPA(aStudent.GPA,aStudent2.GPA);
printf("Average of GPA of two student is %f\n\n",GPAAvg);
// call function to determine the gender of the student
// by passing individual structure members
determineGender(aStudent.gender,aStudent.firstName);
determineGender(aStudent2.gender,aStudent2.firstName);
// define student type array with 5 items
struct student studentArray[SIZE];
int i;
//assigning structure variables to structure variables of the same type is valid operat
studentArray[0]=aStudent;
studentArray[1]=aStudent2;
// initialize the array
for(i=2;i<SIZE;i++){
studentArray[i].idNumber=2000+i;
printf("Enter name and surname\n");
scanf("%s%s",studentArray[i].firstName,studentArray[i].lastName);
printf("Enter gender\n");
scanf("%s",&studentArray[i].gender);
studentArray[i].age=18+rand()%18;
printf("Enter GPA\n");
scanf("%lf",&studentArray[i].GPA);
}
// print the array
// by passing a pointer to a structure
printStudentArray(studentArray);
// call function to count number of male students
// by passing a pointer to a structure
printf("There are %d male student\n\n",countMaleStudent(studentArray));
// sort the array according to the member
// 1 for idnumber, 2 for gender, 3 for age, 4 for GPA
sortArray(studentArray,2);
// print the array
// by passing a pointer to a structure
printStudentArray(studentArray);
// sort the array according to the member
// 1 for idnumber, 2 for gender, 3 for age, 4 for GPA
sortArray(studentArray,4);
// print the array
// by passing a pointer to a structure
printStudentArray(studentArray);
return 0; // indicates successful termination
}
void printStudent(struct student p)
{
printf("%d\t",p.idNumber);
printf("%s\t",p.firstName);
printf("%s\t",p.lastName);
printf("%c\t",p.gender);
printf("%d\t",p.age);
printf("%lf\n\n",p.GPA);
double calculateAvgGPA(double s1,double s2)
{
return (s1+s2)/2;
}
void determineGender(char g, char* n)
{
if (g=='M')
printf("The student %s is a male\n",n);
else
printf("The student %s is a female\n",n);
}
void printStudentArray(struct student *pArray)
{
int i;
for(i=0;i<SIZE;i++){
printf("%d\t",pArray[i].idNumber);
printf("%s\t",pArray[i].firstName);
printf("%s\t",pArray[i].lastName);
printf("%c\t",pArray[i].gender);
printf("%d\t",pArray[i].age);
printf("%lf\n\n",pArray[i].GPA);
}
printf("----------------------------------------------------------------\n\n");
}
int countMaleStudent(struct student *pArray)
{
int i,countMale=0;
for(i=0;i<SIZE;i++){
if (pArray[i].gender=='M')
countMale++;
}
return countMale;
}
void sortArray(struct student *studentArray, int param)
{
int i,pass;
switch (param){
case 1:
for(pass=0;pass<SIZE-1;pass++){
for(i=0;i<SIZE-1;i++){
if(studentArray[i].idNumber>studentArray[i+1].idNumber) {
struct student hold = studentArray[i];
studentArray[i] = studentArray[i+1];
studentArray[i+1] = hold;
}
}
}
break;
case 2:
for(pass=0;pass<SIZE-1;pass++){
for(i=0;i<SIZE-1;i++){
if(studentArray[i].gender>studentArray[i+1].gender) {
struct student hold = studentArray[i];
studentArray[i] = studentArray[i+1];
studentArray[i+1] = hold;
}
}
}
break;
case 3:
for(pass=0;pass<SIZE-1;pass++){
for(i=0;i<SIZE-1;i++){
if(studentArray[i].age>studentArray[i+1].age) {
struct student hold = studentArray[i];
studentArray[i] = studentArray[i+1];
studentArray[i+1] = hold;
}
}
}
break;
case 4:
for(pass=0;pass<SIZE-1;pass++){
for(i=0;i<SIZE-1;i++){
if(studentArray[i].GPA>studentArray[i+1].GPA) {
struct student hold = studentArray[i];
studentArray[i] = studentArray[i+1];
studentArray[i+1] = hold;
}
}
}
break;
}
}