Chapter 6 Structures Enumerations
Chapter 6 Structures Enumerations
1. Structures
A structure is a new type of data made up of a set of variables (fields), which can be heterogeneous
and of different types.
• The difference with the array type is that the fields in an array are all homogeneous and of the
same type.
• Structures can be used to represent real objects characterized by a variety of information,
Par exemple :
• A person characterized by name (string), age (integer), height (real), …
• A car characterized by its make (string), color (string), model year (integer), ...
Example :
In Algorithmic In C Language
structure Person struct Person {
name : string; char name[20];
age : integer; int age ;
height : real; float height ;
endstructure ; };
Notes :
• The structure’s name is not a variable name, it represents the type or model of the structure.
• The memory allocated for a structure variable is equal to the sum of the spaces allocated for
its fields.
• In C, the definition of a structure type ends with a semicolon after the closing brace.
Page | 1
Chapter 6 : Structures and Enumerations
Example :
In Algorithmic In C Language
structure Person struct Person {
name : string; char name[20];
age : integer; int age ;
height : real; float height ;
endstructure ; };
var
p1 : Person ; struct Person p1 ;
A : array[10] of Person ; struct Person A[10] ;
Where :
• p1 is a variable of type Person,
• A is an array variable with 10 elements of type Person.
In C language, a variable of type Person can also be declared at the same time as its definition, as
shown below.
In C Language
struct Person {
char name[20];
int age ;
float height ;
}; p1, A[10];
Example :
• p1.age represents the « age » filed of the structure variable p1.
• p1.height represents the « height » filed of the structure variable p1.
• A[5].nom represents the « name » field in element 5 of the « A » Person array.
• read(p1.name) ; instruction to read the « name » filed of the person p1.
• write(A[10].height) ; instruction to display the « height » of element 10 (Person) in the « A » array.
In Algorithmic In C language
Algorithm Initialisation_structure ; #include <stdio.h>
structure Person struct Person {
name : string ; char name[20];
age : integer ; int age ;
height : real ; float height;
endstructure; };
Page | 2
Chapter 6 : Structures and Enumerations
Here is the « Date » structure and the new form of the « Person » structure :
In Algorithmic In C language
structure Date struct Date {
day : integer ; int day ;
month : integer ; int month ;
year : integer ; int year ;
endstructure; };
• To access the « year » field of the birth date of person « p1 », we can use the notation
p1.birth_date.year.
• To access the « month » field of the birth date of person « p2 », we can use the notation
p2.birth_date.month.
• Typically, structures are manipulated field by field.
Note:
In C language, it's permissible to assign one structure to another structure of the same type.
For instance:
The statement p2 = p1; is valid, copying all fields of «p1» into the corresponding fields of «p2».
2. Enumerations
An enumeration defines a data type where a variable can take one value from a finite set of possible
values. These values are identifiers such as names or symbols with each one corresponding to an
integer constant. The enumeration type is considered to be a simple and homogeneous type.
Page | 3
Chapter 6 : Structures and Enumerations
2.1.Declaration of an enumeration
In Algorithmic In C language
enumeration name_enum enum name_enum {val1, val2, …, valn};
val1, val2, …, valn
endenumeration;
Or
enumeration name_enum {val1, val2, …, valn} ;
Notes:
- The memory space reserved for variable of enumeration type is typically equivalent to the
space required to store an integer.
- The enumeration type is defined at the beginning of the declaration part of the algorithm.
- In C language, the definition of an enumeration type ends with a semicolon after the closing
brace.
2.2. Example of an enumeration
Here are the definitions of the enumeration types « Day » and « Animal », representing, respectively,
the days of the week and animals.
The declaration of a variable of type « Day » allowing to take one of the values: Saturday, Sunday,
Monday, Tuesday, Wednesday, Thursday or Friday.
The declaration of an « Animal » type variable making it possible to take one of the values: lion,
elephant, cat or giraffe.
In Algorithmic In C language
Algorithm type_enumeration; #include <stdio.h>
// definition of Day and Animal enumerations // definition of Day and Animal enumerations
enumeration Day {Saturday, Sunday, Monday, enum Day {Saturday, Sunday, Monday, Tuesday,
Tuesday, Wednesday, Thursday, Friday} ; Wednesday, Thursday, Friday} ;
enumeration Animal {lion,elephant,cat,giraffe} ; enum Animal {lion,elephant,cat,giraffe} ;
Solution
In Algorithmic
Algorithm Example_Structures_Enumerations;
enumeration study_year {L1=1, L2, L3, M1, M2, I1, I2, I3, I4, I5};
structure Date
day : integer;
month : integer;
year : integer;
endstructure;
structure identification_number{
year_BAC : integer;
year_Enroll : integer;
code_BAC : integer;
endstructure;
structure Student{
name : string ;
first_name : string ;
birth_date : Date ;
s_y : study_year ;
id_nber : identification_number ;
modules_nber : integer ;
grades : array[12] of integer ;
average : real ;
endstructure;
Var
x : Student;
A : array[1350] of Student;
A_I1 : array[1350] of Student;
n,i,j,sy,d,nber_admitted,nber_I1,i_max : integer ;
s : real ;
Page | 5
Chapter 6 : Structures and Enumerations
Page | 6
Chapter 6 : Structures and Enumerations
repeat
write("Enter the year of Enrollment : ");
read(A[i].id_nber.year_Enroll);
if(((A[i].id_nber.year_Enroll<2000)or(A[i].id_nber.year_Enroll>2023))or
(A[i].id_nber.year_Enroll<A[i].id_nber.year_BAC))then
write(" Wrong date: Re-enter the year of BAC ");
endif;
Until(((A[i].id_nber.year_Enroll<=2023)and(A[i].id_nber.year_Enroll>=2000))and
(A[i].id_nber.year_Enroll>=A[i].id_nber.year_BAC));
repeat
write("Enter the identification number of BAC : ");
read(A[i].id_nber.code_BAC);
if((A[i].id_nber.code_BAC<10000000)or(A[i].id_nber.code_BAC>99999999))then
write("Wrong identification number: Re-enter the identification number");
endif;
Until((A[i].id_nber.code_BAC<=99999999)and(A[i].id_nber.code_BAC>=10000000));
for (i 1 to n) do
s 0;
for (j 1 to A[i].modules_nber) do
s s + A[i].grades[j];
endfor
A[i].average s/A[i].modules_nber;
write("The Average of the student ",i," is ",A[i].average);
endfor
Page | 7
Chapter 6 : Structures and Enumerations
write("Average : ",A[i].average);
write("Identification number");
write(A[i].id_nber.year_BAC%100,A[i].id_nber.year_Enroll%100,A[i].id_nber.code_BAC);
nber_admitted nber_admitted + 1;
endif
endfor
write("Number of admitted students is ",nber_admitted," students");
/* *** 5. Display information of the best student in 1st year engineer *** */
write("Identification number");
write(A[i_max].id_nber.year_BAC%100,A[i_max].id_nber.year_Enroll%100,A[i_max].id_nber.code_BAC);
write("Average : ",A[i_max].average);
/*** 6. Sort the list of students in descending order of their averages ***/
for (i 1 to n-1) do
for (j 1 to n-i) do
if ((A[j].average)<(A[j+1].average)) then
x A[j];
A[j] A[j+1];
A[j+1] x;
endif
endfor
endfor
/**** 7. Copy first year engineer students into a new array ****/
Page | 8
Chapter 6 : Structures and Enumerations
nber_I1 0;
for (i 1 to n) do
if (A[i].s_y=I1) then
nber_I1 nber_I1 + 1;
A_I1[nber_I1] A[i];
endif
endfor
for (i 1 to n) do
if (A[i].average=0)then
A[i] A[n-1];
n n-1;
i i-1;
endif
endfor
write(" new array after the delete of the students with zero average ");
for (i 1 to n) {
write("Average student ",i," = ",A[i].average);
endfor
End. /* ********** Main Algorithm ********** */
In C language
#include <stdio.h>
#include <string.h>
enum study_year {L1=1, L2, L3, M1, M2, I1, I2, I3, I4, I5};
struct Date {
int day;
int month;
int year;
};
struct identification_number{
int year_BAC;
int year_Enroll;
int code_BAC;
};
struct Student{
char name[20];
char first_name[20];
struct Date birth_date;
enum study_year s_y;//L1, L2, L3, M1, M2, I1, I2, I3, I4, I5
struct identification_number id_nber;
int modules_nber;
int grades[12];
float average;
};
int main() { /* ********** Main Program ********** */
struct Student x, A[1350], A_I1[1350];
int n,i,j,sy,d,nber_admitted;
/* **** 1. Reading of the number of students not exceeding 1350 **** */
do{
printf("Enter the number of students : ");
scanf("%d",&n);
}while ((n<=0)||(n>1350));
Page | 9
Chapter 6 : Structures and Enumerations
do{
printf("Enter the year of birth (between 1960 and 2007): ");
scanf("%d",&A[i].birth_date.year);
}while (((A[i].birth_date.year)<=1960)||((A[i].birth_date.year)>=2007));
do{
printf("Enter the month of birth : ");
scanf("%d",&A[i].birth_date.month);
if (((A[i].birth_date.month)<1)||((A[i].birth_date.month)>12))
printf("\tError date !!! Re-enter the month \n");
}while (((A[i].birth_date.month)<1)||((A[i].birth_date.month)>12));
do{
printf("Enter the day of birth : ");
scanf("%d",&d);
switch (A[i].birth_date.month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10 :
case 12 : if ((d<1)||(d>31)) d=-1; break;
case 4 :
case 6 :
case 9 :
case 11 : if ((d<1)||(d>30)) d=-1; break;
case 2 : if (((d<1)||(d>28))&&(!((d==29)&&(A[i].birth_date.year%4==0)&&
((A[i].birth_date.year%100!=0)||(A[i].birth_date.year%400==0))))){
printf("\tWrong date : Re-enter the day \n");
d=-1;
}
}
}while (d==-1);
A[i].birth_date.day=d;
do{
printf("Enter the study year : \n");
printf("\t* (1 or L1) for 1st year licence \n");
printf("\t* (2 or L2) for 2nd year licence \n");
printf("\t* (3 or L3) for 3rd year licence \n");
printf("\t* (4 or M1) for 1st year Master \n");
printf("\t* (5 or M2) for 2ed year Master \n");
printf("\t* (6 or I1) for 1st year Engineer \n");
printf("\t* (7 or I2) for 2nd year Engineer \n");
printf("\t* (8 or I3) for 3rd year Engineer \n");
printf("\t* (9 or I4) for 4th year Engineer \n");
printf("\t* (10 or I5) for 5th year Engineer \n");
scanf("%d",&sy);
}while ((sy<1)||(sy>10));
A[i].s_y=sy;
Page | 10
Chapter 6 : Structures and Enumerations
if ((A[i].id_nber.year_BAC<2000)||(A[i].id_nber.year_BAC>2023))
printf ("\n\t Wrong date : Re-enter the year of BAC \n");
}while ((A[i].id_nber.year_BAC<2000)||(A[i].id_nber.year_BAC>2023));
do{
printf("Enter the year of Enrollment : ");
scanf("%d",&A[i].id_nber.year_Enroll);
if(((A[i].id_nber.year_Enroll<2000)||(A[i].id_nber.year_Enroll>2023))||
(A[i].id_nber.year_Enroll<A[i].id_nber.year_BAC))
printf("\n\t Wrong date: Re-enter the year of BAC \n");
}while(((A[i].id_nber.year_Enroll<2000)||(A[i].id_nber.year_Enroll>2023))||
(A[i].id_nber.year_Enroll<A[i].id_nber.year_BAC));
do{
printf("Enter the identification number of BAC : ");
scanf("%d",&A[i].id_nber.code_BAC);
}while((A[i].id_nber.code_BAC<10000000)||(A[i].id_nber.code_BAC>99999999));
printf("Enter the number of modules : ");
scanf("%d",&A[i].modules_nber);
printf("\n\nEnter the grades of the student %d : \n",i+1);
for (int j=0; j<A[i].modules_nber;j++){
printf("enter the grade of the module %d : ",j+1);
scanf("%d",&A[i].grades[j]);
}
}
/* ****** 3. Calculate and display the average of each student ***** */
for (i=0;i<n;i++) {
float s=0;
for (int j=0;j<A[i].modules_nber;j++) {
s+= A[i].grades[j];
}
A[i].average=s/A[i].modules_nber;
printf("The Average of the student %d is : %f\n",i+1,A[i].average);
}
/*** 4. Display the (name,surname,date of birth, year of study, average) of ***/
/*** each admitted student (average>=10) and the number of admitted students ***/
printf("The Admitted students are : \n");
nber_admitted=0;
for (i=0;i<n;i++) {
if (A[i].average>=10){
printf("The student %d is admitted : with \n",i+1);
printf("\tname : %s \n\tfirst_name : %s \n" ,A[i].name,A[i].first_name);
printf("\tbirth date : %02d/",A[i].birth_date.day);
printf("%02d/%d\n",A[i].birth_date.month,A[i].birth_date.year);
printf("\tyear of study : ");
switch(A[i].s_y){
case 1 : printf("L1\n");break;
case 2 : printf("L2\n");break;
case 3 : printf("L3\n");break;
case 4 : printf("M1\n");break;
case 5 : printf("M2\n");break;
case 6 : printf("I1\n");break;
case 7 : printf("I2\n");break;
case 8 : printf("I3\n");break;
case 9 : printf("I4\n");break;
case 10 : printf("I5\n");break;
}
Page | 11
Chapter 6 : Structures and Enumerations
/* *** 5. Display information of the best student in 1st year engineer *** */
printf("\n\nThe best student is : \n");
int i_max=0;
for (i=1;i<n;i++) {
if ((A[i].s_y==I5)&&((A[i].average)> (A[i_max].average)))
i_max = i;
}
printf("\tname : %s \n\tfirst_name : %s \n" ,A[i_max].name,A[i_max].first_name);
printf("\tbirth date : %02d/",A[i_max].birth_date.day);
printf("%02d/%d \n",A[i_max].birth_date.month,A[i_max].birth_date.year);
printf("\tyear of study : ");
switch(A[i_max].s_y){
case 1 : printf("L1\n");break;
case 2 : printf("L2\n");break;
case 3 : printf("L3\n");break;
case 4 : printf("M1\n");break;
case 5 : printf("M2\n");break;
case 6 : printf("I1\n");break;
case 7 : printf("I2\n");break;
case 8 : printf("I3\n");break;
case 9 : printf("I4\n");break;
case 10 : printf("I5\n");break;
}
printf("\tIdentification number is : %02d",A[i_max].id_nber.year_BAC%100);
printf("%02d%d \n",A[i_max].id_nber.year_Enroll%100,A[i_max].id_nber.code_BAC);
printf("\tAverage : %f \n" ,A[i_max].average);
/*** 6. Sort the list of students in descending order of their averages ***/
for (i=0;i<n-1;i++) {
for (j=0;j<n-i-1;j++) {
if ((A[j].average)<(A[j+1].average)){
x=A[j];
A[j]=A[j+1];
A[j+1]=x;
}
}
}
printf("The average of students after the sorting : \n");
for (i=0;i<n;i++) {
printf("student %d with average = %f\n",i+1, A[i].average);
}
/**** 7. Copy first year engineer students into a new array ****/
int nber_I1=0;
for (i=0;i<n;i++) {
if (A[i].s_y==I1){
A_I1[nber_I1]= A[i];
nber_I1++;
}
}
Page | 12
Chapter 6 : Structures and Enumerations
Page | 13