0% found this document useful (0 votes)
20 views13 pages

Chapter 6 Structures Enumerations

Cours ingénieur informatique batna

Uploaded by

Edu Ca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views13 pages

Chapter 6 Structures Enumerations

Cours ingénieur informatique batna

Uploaded by

Edu Ca
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Chapter 6 : Structures and Enumerations

Chapter 6 : Structures and 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), ...

1.1. Declaration a structure


A structure is defined by indicating its name along with the names and types of its fields.

Algorithmic Syntax C Syntax


structure name_structure struct name_structure{
name_filed_1 : type_1; type_1 name_filed_1 ;
name_filed_2 : type_2; type_2 name_filed_2 ;
… …
name_filed_k : type_k; type_k name_filed_k ;
endstructure ; };

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.

1.2. Declaration of structure variable


The declaration of a structure does not reserve memory space. The reservation is made when we
define variables corresponding to this structure model. This occurs after the structure declaration.

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];

1.3. Access to the fields of a structure


Accessing a field of a structure is done by using the structure variable's name, followed by the "dot"
access operator and the field's name.
Syntax :
variable_name.filed_name

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.

1.4. Initializing a structure


When declaring a structure variable, you can initialize its fields with a notation similar to that used for
arrays by indicating the list of respective values between braces.

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

var struct Person p1;


p1 : Person ;
int main(){
Begin p1 = {"Ali", 25,1.75} ;
p1  {"Ali",25, 1.75} ; }
End.

1.5. Nesting of structures


Structures can consist of fields of various known types, such as base types, pointers, arrays, or
other structures. When one of the fields within a structure is of the type structure, it is referred to as
nested.
In the previous Person structure, we are replacing the « age » field with the « birth_date » field,
which is of the type structure called « Date ». The latter consists of three fields : day, month, and year.

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; };

structure Person struct Person {


name : string ; char name[20] ;
birth_date : Date ; struct Date birth_date ;
height : real ; float height ;
endstructure; };

var p1,p2 : Person ; struct Person p1,p2 ;

• 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} ;

// declaration of a variables A and D of types // declaration of a variables A and D of types


// Animal and Day. // Animal and Day.
Var A : Animal ; enum Animal A;
D : Day ; enum Day D;
Begin
int main() {
End. }

3. Example of using structures and enumerations


Let's consider a problem involving the management of a group of students in the computer science
department. Each student is represented by a structure containing information such as name, first name,
date of birth, identification number with 12 digits (2 digits for the year of the baccalaureate, 2 digits for the
year of enrolment and 8 digits for the baccalaureate code), year of study (L1, L2, L3, M1, M2, I1, I2, I3, I4
or I5 ; respectively for 1st, 2nd, 3rd year Bachelor, 1st, 2nd year Master, 1st, 2nd, 3rd, 4th or 5th year
Engineer), the number of modules with their coefficients, and the final exam marks for all modules. The
aim is to develop an algorithm to perform the following tasks:

1. Read the number of students, which must not exceed 1350.


2. For each student, read and store all of their information.
Page | 4
Chapter 6 : Structures and Enumerations

3. For each student, calculate and display its average.


4. Display the (name, first_name, date of birth, year of study, average) of each admitted student
(average>=10) and display the number of admitted students.
5. Display information of the best student in 1st year engineer.
6. Sort list of students in descending order of their average.
7. Transfer the first-year engineer students into a new array.
8. Remove from the table all students whose average is equal to zero.

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 ;

Begin { /* ********** Main Algorithm ********** */

/* **** 1. Reading of the number of students not exceeding 1350. **** */


repeat
printf("Enter the number of students : ");
read(n);
Until ((n>0) and (n<=1350));

/* **** 2. Reading and storing each student’s information. ***** */


for(i  1 to n) do
write("Enter the name of the student : ",i);
read(A[i].name);
write("Enter the first name of the student : ",i);
read(A[i].first_name);

Page | 5
Chapter 6 : Structures and Enumerations

write("Enter the birth date (day,month,year) of the student : ",i);


repeat
write("Enter the year of birth (between 1960 and 2007): ");
read(A[i].birth_date.year);
Until((A[i].birth_date.year <= 2007)and(A[i].birth_date.year >= 1960));
repeat
write("Enter the month of birth : ");
read(A[i].birth_date.month);
if ((A[i].birth_date.month < 1)or(A[i].birth_date.month>12))
write("Error date !!! Re-enter the month ");
endif
Until((A[i].birth_date.month <=12) and (A[i].birth_date.month >=1));
repeat
write("Enter the day of birth : ");
read(d);
According_to (A[i].birth_date.month){
1 : if ((d<1)and(d>31)) then d  -1; endif;
3 : if ((d<1)and(d>31)) then d  -1; endif;
5 : if ((d<1)and(d>31)) then d  -1; endif;
7 : if ((d<1)and(d>31)) then d  -1; endif;
8 : if ((d<1)and(d>31)) then d  -1; endif;
10 : if ((d<1)and(d>31)) then d  -1; endif;
12 : if ((d<1)and(d>31)) then d  -1; endif;
4 : if ((d<1)and(d>30)) then d  -1; endif;
6 : if ((d<1)and(d>30)) then d  -1; endif;
9 : if ((d<1)and(d>30)) then d  -1; endif;
11 : if ((d<1)and(d>30)) then d  -1; endif;
2 : if (((d<1)and(d>28))&&(!((d==29)&&(A[i].birth_date.year%4==0)&&
((A[i].birth_date.year%100!=0)and(A[i].birth_date.year%400==0)))))then
write("Wrong date : Re-enter the day ");
d  -1;
endif;
}
Until (d≠(-1));
A[i].birth_date.day  d;
repeat
write("Enter the study year : ");
write("* (1 or L1) for 1st year licence ");
write("* (2 or L2) for 2nd year licence ");
write("* (3 or L3) for 3rd year licence ");
write("* (4 or M1) for 1st year Master ");
write("* (5 or M2) for 2ed year Master ");
write("* (6 or I1) for 1st year Engineer ");
write("* (7 or I2) for 2nd year Engineer ");
write("* (8 or I3) for 3rd year Engineer ");
write("* (9 or I4) for 4th year Engineer ");
write("* (10 or I5) for 5th year Engineer ");
read(sy);
Until ((sy<=10)and(sy>=1));
A[i].s_y  sy;
write("Enter the identification number (year_BAC,Year_Enrollment,code_BAC): ");
repeat
write("Enter the year of BAC (between 2000 and 2023): ");
read(A[i].id_nber.year_BAC);
if ((A[i].id_nber.year_BAC<2000)or(A[i].id_nber.year_BAC>2023))then
write (" Wrong date : Re-enter the year of BAC ");
endif;
Until ((A[i].id_nber.year_BAC<=2023)and(A[i].id_nber.year_BAC>=2000));

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));

write("Enter the number of modules : ");


read(A[i].modules_nber);

write("Enter the grades of the student : ",i);


for (j1 to A[i].modules_nber) do
write("enter the grade of the module : ",j);
read(A[i].grades[j]);
endfor
endfor

/* ****** 3. Calculate and display the average of each student ***** */

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

/*** 4. Display the (name,surname,date of birth, year of study, average) of ***/


/*** each admitted student (average>=10) and the number of admitted students ***/

write("The Admitted students are : ");


nber_admitted  0;
for (i  1 to n) do
if (A[i].average>=10) then
write("The student ",i," is admitted with : ");
write("name : ",A[i].name," first_name : ",A[i].first_name);
write("birth date : ");
write(A[i].birth_date.day,"/",A[i].birth_date.month,"/",A[i].birth_date.year);

write("year of study : ");


According_to(A[i].s_y) do
1 : write("L1");
2 : write("L2");
3 : write("L3");
4 : write("M1");
5 : write("M2");
6 : write("I1");
7 : write("I2");
8 : write("I3");
9 : write("I4");
10 : write("I5");
endAcoording;

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("The best student in first year engineer is : ");


i_max  1;
for (i  2 to n) do
if ((A[i].s_y==I5)and((A[i].average)>(A[i_max].average))) then
i_max  i;
endif
endfor

write("name : ",A[i_max].name," first_name ",A[i_max].first_name);

write("birth date : ");


write(A[i_max].birth_date.day,"/",A[i_max].birth_date.month,"/",A[i_max].birth_date.year);

write("year of study : ");


According_to(A[i_max].s_y) do
1 : write("L1");
2 : write("L2");
3 : write("L3");
4 : write("M1");
5 : write("M2");
6 : write("I1");
7 : write("I2");
8 : write("I3");
9 : write("I4");
10 : write("I5");
endAccording;

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

write("The average of students after the sorting : ");


for (i  1 to n) do
write("student ",i," with average = ",A[i].average);
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

write("the new array of I1 ");


for (i 1 to nber_I1) {
write("student (1 year engineer) I1 : ",i," its average = ",A_I1[i].average);
}

/**** 8. Remove all students who have an average is zero ****/

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

/* **** 2. Reading and storing each student’s information **** */


for(i=0;i<n;i++){
printf("\nEnter the name of the student %d : ",i+1);
scanf("%s",A[i].name);
printf("Enter the first name of the student %d : ",i+1);
scanf("%s",A[i].first_name);
printf("\nEnter the birth date (day,month,year) of the student %d :\n",i+1);

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

printf("Enter the identification number (year_BAC,Year_Enrollment,code_BAC): \n");


do{
printf("Enter the year of BAC (between 2000 and 2023): ");
scanf("%d",&A[i].id_nber.year_BAC);

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

printf("\tAverage : %f \n" ,A[i].average);


printf("\tIdentification number :%02d%02d%d\n\n",A[i].id_nber.year_BAC%100,
A[i].id_nber.year_Enroll%100,A[i].id_nber.code_BAC);
nber_admitted++;
}
}
printf("Number of admitted students is %d students\n",nber_admitted);

/* *** 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

printf("the new array of I1 \n\n");


for (i=0;i<nber_I1;i++) {
printf("student (1 year engineer) I1 : %d its average = %f\n",i+1, A_I1[i].average);
}
/**** 8. Remove all students who have an average is zero ****/
for (i=0;i<n;i++) {
if (A[i].average==0){
A[i]= A[n-1];
n--;
i--;
}
}
printf("\n new array after the delete of the students with zero average \n");
for (i=0;i<n;i++) {
printf("Average student %d = %f\n",i+1,A[i].average);
}
return 0;
} /* ********** Main Program ********** */

Page | 13

You might also like