You are on page 1of 26

Arrays

Write a C procedure FILL to fill a matrix X(5, 5) of integer numbers.


void FILL (int X[5][5])
{
int i, j;
for (i=0; i<5; i++)
for (j=0; j<5; j++)
{
printf("(%d, %d)", i+1, j+1);
scanf("%d", &X[i][j]);
}
}
Write a C procedure PRINT to display a matrix X(5, 5) of integer numbers.
void PRINT(int X[5][5])
{
int i, j;
for (i=0; i<5; i++)
{
for (j=0; j<5; j++)
printf("%d\t", X[i][j]);
printf("\n");
}
}
Write a C procedure SUM to calculate the sum of 2 matrices Z(5, 5) = X(5, 5)
+ Y(5, 5), where Xij, Yij are integer
void SUM (int X[5][5], Y[5][5],
int Y[5][5],
Z[5][5])
int Z[5][5])
{
int i, j;
for (i=0; i<5; i++)
for (j=0; j<5; j++)
Z[i][j] = X[i][j] + Y[i][j];
}
Write a C program that uses the previous three sub programs to read 2
matrices A(5, 5) and B(5, 5) each of integer numbers and to calculate the
matrix C = A + B, and finally to display the three A, B, and their sum C.
#include <stdio.h> FILL(B);
Ex. I SUM(A, B, C);
Ex. II PRINT(A);
Ex. III PRINT(B);
void main() PRINT(C);
{ }
int A[5][5], B[5][5], C[5][5];
FILL(A);
Write a function MIN that takes as a parameter an array 25 real numbers
and returns the minimum in this array.
float MIN (float X[25])
{
float m=X[0];
int i;
for (i=1; i<25; i++)
if (X[i] < m)
m = X[i];
return m;
}
Write a function MAX that takes as a parameter an array of 25 real numbers
and returns the maximum in this array.
float MAX (float X[25])
{
float m=X[0];
int i;
for (i=1; i<25; i++)
if (X[i] > m)
m = X[i];
return m;
}
Write a function MINMAX that takes as a parameter an array of 25 real
numbers, and uses the previous two functions MIN and MAX to find the
minimum and the maximum inside the array and return them.
void MINMAX (float X[25], float *mn, float *MX) void MINMAX (float X[25], float *mn, float *MX)
{ {
int i; *mn = MIN(X);
*mn = *MX = X[0]; *MX = MAX(X);
for (i=1; i<25; i++) }
{
if (X[i] < *mn)
*mn = X[i];
if (X[i] > *MX)
*MX = X[i];
}
}
Write a function CONVERT that takes as a parameter a string and converts its upper case
characters into small case characters and vice-versa. Any other character remains as it was.
void CONVERT (char *X)
{
int i=0;
while (X[i] != '\0')
{
if (X[i] >= 'a' && X[i] <= 'z')
X[i] = X[i] – 'a' + 'A';
else
if (X[i] >= 'A' && X[i] <= 'Z')
X[i] = X[i] – 'A' + 'a';
i++;
}
}
The value of an electrical resistor can be determined by its three colors,
where the first two colors indicate the value of the resistor (tens and units
digits), and the thirs indicates the power of 10 to multiply with. The
following tables shows the numeric value for each color:
Color Black Brown Red Orange Yellow Green Blue Violet Grey White
Value 0 1 2 3 4 5 6 7 8 9

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

You might also like