Professional Documents
Culture Documents
3 7 6 37*106 = 37M
a. Write a function COLOR that takes as a parameter a string representing a
color and returns its value when found, and -1 otherwise.
int COLOR (char *C)
{
char COL[10][10]={"Black", "Brown", "Red", "Orange", "Yellow", "Green",
"Blue", "Violet", "Grey", "White"};
int i; COL
for (i = 0; i < 10; i++) B l a c k \0
B r o w n \0
if ( !strcmpi(C, COL[i]) ) // ( strcmpi(C, COL[i]) == 0 )
R e d \0
return i; O r a n g e \0
return -1; Y e l l o w \0
G r e e n \0
}
B l u e \0
V i o l e t \0
G r e y \0
W h i t e \0
b. Write a C program that reads from the user the three colors of a resistor
and displays its value.
#include <stdio.h> switch (P)
#include <string.h> {
#include <math.h> case 0:
part a. case 1:
void main() case 2: S = '\0';
{ P = pow(10, P);
char C[3][10], S; break;
int i, V = 0, P; case 3:
for (i=0; i<3; i++) case 4:
{ case 5: S = 'K';
do P = pow(10, P-3);
{ break;
printf ("Color %d:", i+1); default: S = 'M';
gets(C[i]); P = pow(10, P-6);
} while ( COLOR(C[i]) < 0 ); }
} printf ("R = %d %cOhms\n", V*P, S);
V = COLOR( C[0] ) * 10 + COLOR( C[1] ); }
P = COLOR( C[2] );
A student is defined by the following information:
ID: an integer value,
Name: a string of 30 characters maximum,
5 grades: each a real value,
An average: a real value.
a. Write the proposed data structure for one student,
b. Write a function AVERAGE, that takes 5 real values as parameters, and returns their average,
c. Write a function READ to read the information for one student, this function must use the AVERAGE function to
determine the student’s average,
d. Write a function FILL, that uses READ, to fill an array of 50 students,
e. Write a function SWAP that swaps two lines in an array of 50 students passed all as parameters,
f. Write a function SORT that takes as a parameter an array of 50 students, and sorts it by decreasing order of average,
g. Write a function DISPLAY that displays on the screen the ID, Name and Average of one student, passed as a parameter,
h. Write a function PRINT that uses DISPLAY to print a given array of 50 students on the screen,
i. Write a C program that uses the previous functions to:
• Read the information of 50 students,
• Calculate the average for each student,
• Display the results in decreasing order of average.
a. Write the proposed data structure for one student.
typedef struct Student
{
int ID;
Student
char Name[30+1];
Av
float Grades[5], Average; ID Name Grades
rg
} Student;
0 … 30 0 … 4
b. Write a function AVERAGE, that takes 5 real values as parameters, and
returns their average.
float Average(float X[5])
{
float A = 0;
int i;
for (i=0; i<5; i++)
A += X[i];
return A/5;
}
c.i Write a function READ to read the information for one student, this function must use
the AVERAGE function to determine the student’s average.
Student READ()
{
Student S;
int i; S
printf("ID: ");
scanf("%d", &S.ID);
fflush(stdin); Av
ID Name Grades
printf("Name: "); rg
gets(S.Name);
for (i=0; i<5; i++)
{ 0 … 30 0 … 4
printf("Grade %d", i+1);
scanf("%f", &S.Grades[i]);
}
S.Average = Average(S.Grades);
return S;
}
c.ii
void READ_By_Address(Student *S)
{
int i;
printf("ID: "); *S
scanf("%d", &S->ID);
fflush(stdin); Av
ID Name Grades
printf("Name: "); rg
gets(S->Name);
for (i=0; i<5; i++)
{
0 … 30 0 … 4
printf("Grade %d", i+1);
scanf("%f", &S->Grades[i]);
}
S->Average = Average(S->Grades);
}
d. Write a function FILL, that uses READ, to fill an array of 50 students,
void FILL(Student X[50])
{
int i;
for (i=0; i<25; i++)
X[i] = READ();
for (i=25; i<50; i++)
READ_By_Address (&X[i]);
}
e. Write a function SWAP that swaps two lines in an array of 50 students
passed all as parameters
void SWAP(Student X[50], int a, int b)
{
Student S;
S = X[a];
X[a] = X[b];
X[b] = S;
}
e'. Write a function SWAP_By_Address that swaps two students passed as
parameters
void SWAP_By_Address(Student *S1, Student *S2)
{
Student S;
S = *S1;
*S1 = *S2;
*S2 = S;
}
f. Write a function SORT that takes as a parameter an array of 50 students,
and sorts it by decreasing order of average
void SORT(Student X[50])
{
int i, j;
for (i=0; i<49; i++)
for (j=i+1; j<50; j++)
if ( X[j].Average > X[i].Average )
SWAP (X, i, j); // or SWAP_By_Address(&X[i], &X[j]);
}
g. Write a function DISPLAY that displays on the screen the ID, Name and
Average of one student, passed as a parameter
void DISPLAY (Student X)
{
printf ("%d\t%s\t%f\n", X.ID, X.Name, X.Average);
}
h. Write a function PRINT that uses DISPLAY to print a given array of 50
students on the screen
void PRINT (Student X[50])
{
int i;
for (i=0; i<50; i++)
DISPLAY (X[i]);
}
i. Write a C program that uses the previous functions to:
Read the information of 50 students,
Calculate the average for each student,
Display the results in decreasing order of average.
#include <stdio.h>
Part a.
Part b.
Part c.
Part d.
Part e.
Part f.
Part g.
Part h.
void main ()
{
Student S[50];
FILL (S);
SORT (S);
PRINT (S);
}
i' void DISPLAY (Student X) }
#include <stdio.h> { void FILL (Student X[50])
typedef struct Student printf ("%d\t%s\t%f\n", X.ID, {
{ X.Name, X.Average); int i;
int ID; } for (i=0; i<50; i++)
char Name[30+1]; void SORT (Student X[50]) X[i] = LIRE();
float Grades[5], Average; { }
} Student; int i, j; Student READ ()
void PRINT (Student X[]); for (i=0; i<49; i++) {
void DISPLAY (Student X); for (j=i+1; j<50; j++) Student S;
float AVERAGE (float []); if ( int i;
X[j].Average > X[i].Average ) printf("ID: ");
void SORT (Student X[]);
void SWAP (Student X[], int, int); SWAP (X, i, j); scanf("%d", &S.ID);
void FILL (Student X[]); fflush(stdin);
}
Student READ (); printf("Nom: ");
float AVERAGE (float X[5])
void main () gets(S.name);
{
{ float A = 0; for (i=0; i<5; i++)
Student S[50]; int i; {
FILL (S); for (i=0; i<5; i++) printf(“Note %d", i+1);
SORT (S); scanf("%f",
A += X[i]; &S.Grades[i]);
PRINT (S); return A/5;
} }
} S.Average = AVERAGE (S.Grades);
void PRINT (Student X[50]) void SWAP (Student X[50], int a, int b)
{ return S;
{ }
int i; Student S;
for (i=0; i<50; i++) S = X[a];
DISPLAY (X[i]); X[a] = X[b];
} X[b] = S;
END