Day11.C
Arrays (2)Function Calls
Young W. Lim
December 9, 2017
This work is licensed under a Creative Com-mons “Attribution-NonCommercial-ShareAlike 3.0Unported” license.
Day11.C Young W. Lim
0.1 Find max, min, sum, avg values
::::::::::::::
find.c
::::::::::::::
#include <stdio.h>
int find_sum(int a[], int len) {
int i, S=0;;
for (i=0; i<len; ++i) S += a[i];
return S;
}
int find_max(int a[], int len) {
int i, cmax=(1L<<31); // the least negative int
printf("%x %+15d\n", cmax, cmax);
for (i=0; i<len; ++i)
if (cmax < a[i]) cmax = a[i];
return cmax;
}
int find_min(int a[], int len) {
int i, cmin=(1L<<31)-1; // the greatest positive int
printf("%x %+15d\n", cmin, cmin);
for (i=0; i<len; ++i)
if (cmin > a[i]) cmin = a[i];
return cmin;
}
double find_avg(int a[], int len) {
double avg;
avg = find_sum(a, len);
avg /= len;
return avg;
}
int main(void) {
int array[10] = {1, 9, 3, 5, 4, 2, 6, 8, 7, 10};
int sum_val, max_val, min_val, avg_val;
Page 1
Day11.C Young W. Lim
sum_val = find_sum(array, 10);
max_val = find_max(array, 10);
min_val = find_min(array, 10);
avg_val = find_avg(array, 10);
printf("sum_val= %d \n", sum_val);
printf("max_val= %d \n", max_val);
printf("min_val= %d \n", min_val);
printf("avg_val= %d \n", avg_val);
}
::::::::::::::
find.out
::::::::::::::
80000000 -2147483648
7fffffff +2147483647
sum_val= 55
max_val= 10
min_val= 1
avg_val= 5
cmax
• to find a maximum, cmax must set to the least possible negative number.
• for the type int has a 4-byte storage, 0x80000000 is such a number.
• this number can be obtained by shifting 1 to the left by 31 bit positions.
• to avoid warning messages (overflow), use the long int type
• (1L << 31) is an 8-byte integer, 0x0000000080000000 (positive)
• this 8-byte integer is assigned to the 4-byte integer variable cmax
• the value of cmax is now 0x80000000.
• the value of cmax is the least negative number for the int type integer
cmin
• to find a minimum, cmin must set to the greatest possible positive number.
• for the type int has a 4-byte storage, 0x7FFFFFFF is such a number.
• this number can be obtained
– by shifting 1 to the left by 31 bit positions,
Page 2
Day11.C Young W. Lim
– then subtracting by one,
– 0x7FFFFFFF is less than 0x80000000,
– considering them as positive numbers.
• to avoid warning messages (overflow), use the long int type
• (1L << 31) is an 8-byte integer, 0x0000000080000000 (positive)
• (1L << 31)-1 is an 8-byte integer, 0x000000007FFFFFFF (positive)
• this 8-byte integer is assigned to the 4-byte integer variable cmin
• the value of cmin is now 0x7FFFFFFF.
• the value of cmin is the greatest positive number for the int type integer
0.2 Linear Search
::::::::::::::
search.c
::::::::::::::
#include <stdio.h>
int find_max(int a[], int len) {
int i, cmax=(1L<<31);
for (i=0; i<len; ++i)
if (cmax < a[i]) cmax = a[i];
return cmax;
}
int find_min(int a[], int len) {
int i, cmin=(1L<<31)-1;
for (i=0; i<len; ++i)
if (cmin > a[i]) cmin = a[i];
return cmin;
}
int search(int a[], int key, int len) {
int i;
for (i=0; i<len; ++i)
if (key == a[i]) return i;
return -1;
}
Page 3
Day11.C Young W. Lim
int main(void) {
int array[10] = {1, 9, 3, 5, 4, 2, 6, 8, 7, 10};
int max_val, min_val, max_index, min_index;
max_val = find_max(array, 10);
min_val = find_min(array, 10);
max_index = search(array, max_val, 10);
min_index = search(array, min_val, 10);
printf("max_val= %d \n", max_val);
printf("max_index= %d \n", max_index);
printf("min_val= %d \n", min_val);
printf("min_index= %d \n", min_index);
}
::::::::::::::
search.out
::::::::::::::
max_val= 10
max_index= 9
min_val= 1
min_index= 0
searching the array
• the content of the array
index 0 1 2 3 4 5 6 7 8 9value 1 9 3 5 4 2 6 8 7 10
• max_val (=10) is stored in array[0] ( key=10, index=0 )
• min_val (=1) is stored in array[9] ( key=1, index=9 )
the search function
• in the for loop,
for (i=0; i<len; ++i)
if (key == a[i]) return i;
• whenever the input key is matched with any element of array
• the function immediately returns with the index i.
• however, when no element of the array matches with key,
• the next statement return -1 will be executed.
• normally, the index values are non-negative.
• the index value of -1 represents there is no matching key in array.
Page 4
Day11.C Young W. Lim
0.3 Passing individual elements of arrays
::::::::::::::
swap.c
::::::::::::::
#include <stdio.h>
void swap(int *a, int *b) {
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
void pr_element(int index, int val) {
printf("array[%d] = %d \n", index, val);
}
int main(void) {
int array[10] = {1, 9, 3, 5, 4, 2, 6, 8, 7, 10};
int i;
for (i=0; i<10; ++i)
printf("array[%d]= %d \n", i, array[i]);
swap(&array[0], &array[9]);
printf("------------------\n");
pr_element(0, array[0]);
pr_element(9, array[9]);
}
::::::::::::::
swap.out
::::::::::::::
array[0]= 1
array[1]= 9
array[2]= 3
array[3]= 5
array[4]= 4
array[5]= 2
Page 5
Day11.C Young W. Lim
array[6]= 6
array[7]= 8
array[8]= 7
array[9]= 10
------------------
array[0] = 10
array[9] = 1
passing the address of an array element
• swap(&array[0], &array[9]);
• swap(array, array+9);
passing the elements of an array
• pr_element(0, array[0]);
• pr_element(9, array[9]);
0.4 Find max, min, sum, avg values
::::::::::::::
find.c
::::::::::::::
#include <stdio.h>
int find_sum(int a[], int len) {
int i, S=0;;
for (i=0; i<len; ++i) S += a[i];
return S;
}
int find_max(int a[], int len) {
int i, cmax=(1L<<31); // the least negative int
printf("%x %+15d\n", cmax, cmax);
for (i=0; i<len; ++i)
if (cmax < a[i]) cmax = a[i];
return cmax;
}
int find_min(int a[], int len) {
int i, cmin=(1L<<31)-1; // the greatest positive int
printf("%x %+15d\n", cmin, cmin);
for (i=0; i<len; ++i)
if (cmin > a[i]) cmin = a[i];
Page 6
Day11.C Young W. Lim
return cmin;
}
double find_avg(int a[], int len) {
double avg;
avg = find_sum(a, len);
avg /= len;
return avg;
}
int main(void) {
int array[10] = {1, 9, 3, 5, 4, 2, 6, 8, 7, 10};
int sum_val, max_val, min_val, avg_val;
sum_val = find_sum(array, 10);
max_val = find_max(array, 10);
min_val = find_min(array, 10);
avg_val = find_avg(array, 10);
printf("sum_val= %d \n", sum_val);
printf("max_val= %d \n", max_val);
printf("min_val= %d \n", min_val);
printf("avg_val= %d \n", avg_val);
}
::::::::::::::
find.out
::::::::::::::
80000000 -2147483648
7fffffff +2147483647
sum_val= 55
max_val= 10
min_val= 1
avg_val= 5
cmax
• to find a maximum, cmax must set to the least possible negative number.
• for the type int has a 4-byte storage, 0x80000000 is such a number.
Page 7
Day11.C Young W. Lim
• this number can be obtained by shifting 1 to the left by 31 bit positions.
• to avoid warning messages (overflow), use the long int type
• (1L << 31) is an 8-byte integer, 0x0000000080000000 (positive)
• this 8-byte integer is assigned to the 4-byte integer variable cmax
• the value of cmax is now 0x80000000.
• the value of cmax is the least negative number for the int type integer
cmin
• to find a minimum, cmin must set to the greatest possible positive number.
• for the type int has a 4-byte storage, 0x7FFFFFFF is such a number.
• this number can be obtained
– by shifting 1 to the left by 31 bit positions,
– then subtracting by one,
– 0x7FFFFFFF is less than 0x80000000,
– considering them as positive numbers.
• to avoid warning messages (overflow), use the long int type
• (1L << 31) is an 8-byte integer, 0x0000000080000000 (positive)
• (1L << 31)-1 is an 8-byte integer, 0x000000007FFFFFFF (positive)
• this 8-byte integer is assigned to the 4-byte integer variable cmin
• the value of cmin is now 0x7FFFFFFF.
• the value of cmin is the greatest positive number for the int type integer
0.5 Linear Search
::::::::::::::
search.c
::::::::::::::
#include <stdio.h>
int find_max(int a[], int len) {
int i, cmax=(1L<<31);
for (i=0; i<len; ++i)
if (cmax < a[i]) cmax = a[i];
return cmax;
}
int find_min(int a[], int len) {
Page 8
Day11.C Young W. Lim
int i, cmin=(1L<<31)-1;
for (i=0; i<len; ++i)
if (cmin > a[i]) cmin = a[i];
return cmin;
}
int search(int a[], int key, int len) {
int i;
for (i=0; i<len; ++i)
if (key == a[i]) return i;
return -1;
}
int main(void) {
int array[10] = {1, 9, 3, 5, 4, 2, 6, 8, 7, 10};
int max_val, min_val, max_index, min_index;
max_val = find_max(array, 10);
min_val = find_min(array, 10);
max_index = search(array, max_val, 10);
min_index = search(array, min_val, 10);
printf("max_val= %d \n", max_val);
printf("max_index= %d \n", max_index);
printf("min_val= %d \n", min_val);
printf("min_index= %d \n", min_index);
}
::::::::::::::
search.out
::::::::::::::
max_val= 10
max_index= 9
min_val= 1
min_index= 0
searching the array
• the content of the array
index 0 1 2 3 4 5 6 7 8 9value 1 9 3 5 4 2 6 8 7 10
Page 9
Day11.C Young W. Lim
• max_val (=10) is stored in array[0] ( key=10, index=0 )
• min_val (=1) is stored in array[9] ( key=1, index=9 )
the search function
• in the for loop,
for (i=0; i<len; ++i)
if (key == a[i]) return i;
• whenever the input key is matched with any element of array
• the function immediately returns with the index i.
• however, when no element of the array matches with key,
• the next statement return -1 will be executed.
• normally, the index values are non-negative.
• the index value of -1 represents there is no matching key in array.
0.6 Passing individual elements of arrays
::::::::::::::
swap.c
::::::::::::::
#include <stdio.h>
void swap(int *a, int *b) {
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
void pr_element(int index, int val) {
printf("array[%d] = %d \n", index, val);
}
int main(void) {
int array[10] = {1, 9, 3, 5, 4, 2, 6, 8, 7, 10};
int i;
for (i=0; i<10; ++i)
printf("array[%d]= %d \n", i, array[i]);
Page 10
Day11.C Young W. Lim
swap(&array[0], &array[9]);
printf("------------------\n");
pr_element(0, array[0]);
pr_element(9, array[9]);
}
::::::::::::::
swap.out
::::::::::::::
array[0]= 1
array[1]= 9
array[2]= 3
array[3]= 5
array[4]= 4
array[5]= 2
array[6]= 6
array[7]= 8
array[8]= 7
array[9]= 10
------------------
array[0] = 10
array[9] = 1
passing the address of an array element
• swap(&array[0], &array[9]);
• swap(array, array+9);
passing the elements of an array
• pr_element(0, array[0]);
• pr_element(9, array[9]);
0.7 Historgram
::::::::::::::
h2.c
::::::::::::::
#include <stdio.h>
#include <stdlib.h>
#define SIZE 20
Page 11
Day11.C Young W. Lim
int main(void) {
int A[SIZE];
int H[10];
int i, j;
// H[0]= number of students 0 <= score < 10 score/10 = 0
// H[1]= number of students 10 <= score < 20 score/10 = 1
// H[2]= number of students 20 <= score < 30 score/10 = 2
// H[3]= number of students 30 <= score < 40 score/10 = 3
// H[4]= number of students 40 <= score < 50 score/10 = 4
// H[5]= number of students 50 <= score < 60 score/10 = 5
// H[6]= number of students 60 <= score < 70 score/10 = 6
// H[7]= number of students 70 <= score < 80 score/10 = 7
// H[8]= number of students 80 <= score < 90 score/10 = 8
// H[9]= number of students 90 <= score <=100 score/10 = 9,10
srand(1);
for (i=0; i<SIZE; ++i) A[i] = rand() % 101;
for (i=0; i<SIZE; ++i) printf("A[%d]= %d \n", i, A[i]);
for (j=0; j<10; ++j) H[j]= 0;
for (i=0; i<SIZE; ++i) {
j =A[i] / 10; // integer division = quotient
if (j==10) j=9;
H[j]++;
}
for (j=0; j<10; ++j)
printf("H[%d]= %d students\n", j, H[j]);
}
::::::::::::::
h2.out
::::::::::::::
A[0]= 32
A[1]= 32
A[2]= 54
A[3]= 12
A[4]= 52
A[5]= 56
A[6]= 8
A[7]= 30
A[8]= 44
A[9]= 94
A[10]= 44
A[11]= 39
A[12]= 65
Page 12
Day11.C Young W. Lim
A[13]= 19
A[14]= 51
A[15]= 91
A[16]= 1
A[17]= 5
A[18]= 89
A[19]= 34
H[0]= 3 students
H[1]= 2 students
H[2]= 0 students
H[3]= 5 students
H[4]= 2 students
H[5]= 4 students
H[6]= 1 students
H[7]= 0 students
H[8]= 1 students
H[9]= 2 students
Histogram with 10 bins
• there are 10 bins
• all the scores have the range of [0, 100]
• score / 10 : this integer division gives a bin index with a range of [0, 9]
• if score is equal to 100, the bin index 9 is assigned
• H[bin_size] is intialized with zero
• H[j] is incremented whenever a student’s score / 10 is equal to j
Histogram with 5 bins
#include <stdio.h>
#include <stdlib.h>
#define SIZE 20
#define BINSZ 5
//--------------------------------
// return 0 0 <= score < 20
// return 1 20 <= score < 40
// return 2 40 <= score < 60
// return 3 60 <= score < 80
// return 4 80 <= score < 50
//--------------------------------
int bin(int A[], int i) {
int j;
int score = A[i];
Page 13
Day11.C Young W. Lim
if (score < 20) j= 0;
else if (score < 40) j= 1;
else if (score < 60) j= 2;
else if (score < 80) j= 3;
else j= 4;
return j;
}
//--------------------------------
// bubble sort
//--------------------------------
void bubbleSort(int a[], int size)
{
int p, j;
int tmp;
for (p=1; p< size; ++p) {
for (j=0; j< size-1; ++j) {
if ( a[j] < a[j+1] ) {
tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
}
}
}
}
int main(void) {
int A[SIZE];
int H[BINSZ];
int i, j;
// H[0]= number of students 0 <= score < 20
// H[1]= number of students 20 <= score < 40
// H[2]= number of students 40 <= score < 60
// H[3]= number of students 60 <= score < 80
// H[4]= number of students 80 <= score < 50
srand(1);
for (i=0; i<SIZE; ++i) A[i] = rand() % 101;
for (i=0; i<SIZE; ++i) printf("A[%d]= %d \n", i, A[i]);
for (j=0; j<10; ++j) H[j]= 0;
for (i=0; i<SIZE; ++i) {
Page 14
Day11.C Young W. Lim
j = bin(A, i);
H[j]++;
}
printf("\nSorting in decreasing order \n\n");
bubbleSort(A, SIZE);
for (i=0; i<SIZE; ++i) printf("A[%d]= %d \n", i, A[i]);
printf("\n");
for (j=0; j<BINSZ; ++j)
printf("H[%d]= %d students\n", j, H[j]);
}
0.8 A spreadsheet example using 1-d arrays
[?]
• int I[SIZE]; student ID (identification) number
• int K[SIZE]; Korean subject score
• int E[SIZE]; English subject score
• int M[SIZE]; Mathematics subject score
• double A[SIZE]; average score of three subjects
Page 15
Day11.C Young W. Lim
0.9 avg3() function
//--------------------------------------------------
// Calculating the average of three numbers
//--------------------------------------------------
double avg3(int x, int y, int z)
{
return (x+y+z) / 3.;
}
• takes three integers
• returns their arithmetic average value (a double type)
Page 16
Day11.C Young W. Lim
0.10 init arrays() function
//--------------------------------------------------
// Initialize K[], E[], M[] arrays
// by assigning random number grade
//--------------------------------------------------
void init_arrays
(int I[], int K[], int E[], int M[], double A[])
{
int i;
// srand(7) makes rand() generate
// the same random sequence
// --> easy to debug a program
srand(7);
for (i=0; i<SIZE; ++i) {
I[i] = i+1 + 201600;
K[i] = rand() % 101;
E[i] = rand() % 101;
M[i] = rand() % 101;
A[i] = avg3(K[i], E[i], M[i]);
}
}
• takes 5 1-d array names
– the int type arrays : (I, K, E, M)
– the double type array : (A)
• returns nothing
• fill the K, E, and M arrays with random numbers
• A[i] is filled with the average value of K[i], E[i], and M[i]
• I[i] is filled with the expression i+1+201600
• all the 5 arrays (I, K, E, M, A) are modified
Page 17
Day11.C Young W. Lim
0.11 pr table() function
//--------------------------------------------------
// Print the original table
//-------------------------------------------------
void pr_table
(int I[], int K[], int E[], int M[], double A[])
{
int i;
printf("%10s %10s %10s %10s %10s \n", "StID",
"Korean", "English", "Math", "Average");
for (i=0; i<SIZE; ++i) {
printf("%10d %10d %10d %10d %10.2f \n",
I[i], K[i], E[i], M[i], A[i]);
}
}
• takes 5 1-d array names
– the int type arrays : (I, K, E, M)
– the double type array : (A)
• returns nothing
• print the student ID, test scores of Korean, English, and Mathematics,and their average score row by row
• header
%10s %10s %10s %10s %10s“StID” “Korean” “English” “Math” “Average”
• each row
%10d %10d %10d %10d %10.2fI[i] K[i] E[i] M[i] A[i]
• StID Korean English Math Average
201601 17 78 99 64.67
201602 65 87 61 71.00
201603 21 46 91 52.67
201604 100 7 31 46.00
201605 14 63 42 39.67
201606 72 78 68 72.67
201607 9 100 68 59.00
201608 84 20 2 35.33
201609 29 79 62 56.67
201610 69 53 94 72.00
• no array is modified.
Page 18
Day11.C Young W. Lim
0.12 Dbubblesort() function
//--------------------------------------------------
// Bubble Sort Double Array
//-------------------------------------------------
void DbubbleSort(double a[], int size)
{
int p, j;
double tmp;
for (p=1; p< size; ++p) {
for (j=0; j< size-1; ++j) {
if ( a[j] < a[j+1] ) {
tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
}
}
}
}
• takes the 1-d array name of the double type and the array size
• returns nothing
• there are size-1 passes : for (p=1; p<size; ++p) { ... }
• in each pass, perform the following basic operation
over size-1 pairs of adjacent elements : for (j=0; j<size-1; ++j) { ... }
– for a given j, compare the pair a[j] and a[j+1]
– if ( a[j] < a[j+1] ) then swap each other
– thus ensuring a[j] is greater than or equal to a[j+1]
– the next pair to be compared will be
∗ a[j+1] and a[j+2] in terms of the old value of j
∗ a[j] and a[j+1] in terms of the incremented value of j
• the current element is moved to the right (increasing index)
until the smaller element is found
• after p-1 passes, the array elements are in the increasing order.
• the given array is modified
Page 19
Day11.C Young W. Lim
0.13 pr sorted table() function
//--------------------------------------------------
// Print the Sorted Table
//-------------------------------------------------
void pr_sorted_table
(int I[], int K[], int E[], int M[], double A[])
{
int i, j;
double B[SIZE]; // Backup Array for Sorting
for (i=0; i<SIZE; ++i) B[i] = A[i];
//....................
DbubbleSort(B, SIZE);
//....................
printf("\n\nSorted on a student’s average\n\n");
printf("%10s %10s %10s %10s %10s \n", "StID",
"Korean", "English", "Math", "Average");
for (i=0; i<SIZE; ++i) {
for (j=0; j<SIZE; ++j) if (B[i] == A[j]) break;
printf("%10d %10d %10d %10d %10.2f \n",
I[j], K[j], E[j], M[j], A[j]);
}
}
• takes 5 1-d array names
– the int type arrays : (I, K, E, M)
– the double type array : (A)
• returns nothing
• initially, all the 1-d arrays are sorted by the student ID
• copy A array into B array
• sort B array : DbubbleSort(B, SIZE);
• only B array are in the increasing order of the average score
• for each B[i] : for (i=0; i<SIZE; ++i)
– find the index j such that A[j]=B[i] : for (j=0; j<SIZE; ++j)
– print the (j+1)-th row of the original table
–%10d %10d %10d %10d %10.2f
I[j] K[j] E[j] M[j] A[j]
• no array is modified
Page 20
Day11.C Young W. Lim
0.14 Avg() function
//--------------------------------------------------
// Average over Integer Array
//-------------------------------------------------
double Avg(int X[], int n) {
int i; double S=0.0;
for (i=0; i<n; ++i) S+= X[i];
return S/n;
}
• takes a 1-d int array name and its size
• returns its arithmetic average value (a double type)
• 1n
∑n−1i=0 X[i]
Page 21
Day11.C Young W. Lim
0.15 DAvg() function
//--------------------------------------------------
// Average over Double Array
//-------------------------------------------------
double DAvg(double Y[], int n) {
int i; double S=0.0;
for (i=0; i<n; ++i) S+= Y[i];
return S/n;
}
• takes a 1-d double array name and its size
• returns its arithmetic average value (a double type)
• 1n
∑n−1i=0 Y [i]
Page 22
Day11.C Young W. Lim
0.16 pr averages() function
//--------------------------------------------------
// Print the Averages
//-------------------------------------------------
void pr_averages(int K[], int E[], int M[], double A[])
{
double A1 = Avg(K, SIZE);
double A2 = Avg(E, SIZE);
double A3 = Avg(M, SIZE);
double A4 = DAvg(A, SIZE);
printf("%10s %10.2f %10.2f %10.2f %10.2f \n",
"Average", A1, A2, A3, A4);
}
• takes 5 1-d array names
– the int type arrays : (I, K, E, M)
– the double type array : (A)
• returns nothing
• A1 : the average score of the Korean subject
• A2 : the average score of the English subject
• A3 : the average score of the Mathematics subject
• A4 : the average score of each student’s average score
• %10s %10.2f %10.2f %10.2f %10.2f”Average” A1 A2 A3 A4
• no array is modified
Page 23
Day11.C Young W. Lim
0.17 main() function
int main(void) {
int I[SIZE]; // ID of a student
int K[SIZE]; // Grade of Korean Class
int E[SIZE]; // Grade of English Class
int M[SIZE]; // Grade of Math Class
double A[SIZE]; // Average of a student
init_arrays(I, K, E, M, A);
pr_table(I, K, E, M, A);
pr_sorted_table(I, K, E, M, A);
pr_averages(K, E, M, A);
}
• declare 5 1-d arrays
– int I[SIZE];
– int K[SIZE];
– int E[SIZE];
– int M[SIZE];
– double A[SIZE];
• call init_arrays() function
• call pr_table() function
• call pr_sorted_table() function
• call pr_averages() function
Page 24
Day11.C Young W. Lim
0.18 When students have the same average
test cases the previous codes do not work in such case.
/--------------------------------------------------
// Initialize K[], E[], M[] arrays
// by assigning random number grade
//--------------------------------------------------
void init_arrays
(int I[], int K[], int E[], int M[], double A[])
{
int i;
// srand(7) makes rand() generate
// the same random sequence
// --> easy to debug a program
srand(7);
for (i=0; i<SIZE; ++i) {
I[i] = i+1 + 201600;
K[i] = rand() % 101;
E[i] = rand() % 101;
M[i] = rand() % 101;
A[i] = avg3(K[i], E[i], M[i]);
}
K[3] = K[2];
E[3] = E[2];
M[3] = M[2];
A[3] = A[2];
K[5] = K[2];
E[5] = E[2];
M[5] = M[2];
A[5] = A[2];
K[7] = K[2];
E[7] = E[2];
M[7] = M[2];
A[7] = A[2];
}
Page 25
Day11.C Young W. Lim
• rows having index values of 2, 3, 5, 7 have the same score.
• only one student ID (201603) is repeated.
StID Korean English Math Average
201610 69 53 94 72.00
201602 65 87 61 71.00
201601 17 78 99 64.67
201607 9 100 68 59.00
201609 29 79 62 56.67
201603 21 46 91 52.67
201603 21 46 91 52.67
201603 21 46 91 52.67
201603 21 46 91 52.67
201605 14 63 42 39.67
Average 28.70 64.40 79.00 57.37
Page 26
Day11.C Young W. Lim
0.19 Handling the same average cases - Method 1
using extra memory
• using index array : int C[SIZE];
• swap both average score and its index : DbubbleSort()
//--------------------------------------------------
// Bubble Sort Double Array
//-------------------------------------------------
void DbubbleSort(double b[], int c[], int size)
{
int p, j, t;
double tmp;
for (p=1; p< size; ++p) {
for (j=0; j< size-1; ++j) {
if ( b[j] < b[j+1] ) {
tmp = b[j];
b[j] = b[j+1];
b[j+1] = tmp;
t = c[j];
c[j] = c[j+1];
c[j+1] = t;
}
}
}
}
Page 27
Day11.C Young W. Lim
//--------------------------------------------------
// Print the Sorted Table
//-------------------------------------------------
void pr_sorted_table
(int I[], int K[], int E[], int M[], double A[])
{
int i, j;
double B[SIZE]; // Backup Array for Sorting
int C[SIZE]; // index array
for (i=0; i<SIZE; ++i) {
B[i] = A[i];
C[i] = i;
}
//....................
DbubbleSort(B, C, SIZE);
//....................
printf("\n\nSorted on a student’s average\n\n");
printf("%10s %10s %10s %10s %10s \n", "StID",
"Korean", "English", "Math", "Average");
for (i=0; i<SIZE; ++i) {
j = C[i];
printf("%10d %10d %10d %10d %10.2f \n",
I[j], K[j], E[j], M[j], A[j]);
}
}
Page 28
Day11.C Young W. Lim
0.20 Handling the same average cases - Method 2
using extra computation
• counting the same average cases: int cnt;
• search all indices for the same average : j
//--------------------------------------------------
// Print the Sorted Table
//-------------------------------------------------
void pr_sorted_table
(int I[], int K[], int E[], int M[], double A[])
{
int i, j;
double B[SIZE]; // Backup Array for Sorting
int cnt;
for (i=0; i<SIZE; ++i) B[i] = A[i];
//....................
DbubbleSort(B, SIZE);
//....................
printf("\n\nSorted on a student’s average\n\n");
printf("%10s %10s %10s %10s %10s \n", "StID",
"Korean", "English", "Math", "Average");
for (i=0; i<SIZE; ++i) {
cnt=1;
while (B[i-cnt]==B[i]) cnt++;
j=0;
while (cnt>0) {
cnt--;
while (A[j]!=B[i]) j++;
if (cnt > 0) j++;
}
printf("%10d %10d %10d %10d %10.2f \n",
I[j], K[j], E[j], M[j], A[j]);
}
}
0.21 A spreadsheet example using 2-d arrays
[?]
• int X[4][SIZE];
– X[0][SIZE]; student ID (identification) number
Page 29
Day11.C Young W. Lim
– X[1][SIZE]; Korean subject score
– X[2][SIZE]; English subject score
– X[3][SIZE]; Mathematics subject score
• double A[SIZE]; average score of three subjects
Page 30
Day11.C Young W. Lim
0.22 avg3() function
//--------------------------------------------------
// Calculating the average of three numbers
//--------------------------------------------------
double avg3(int x, int y, int z)
{
return (x+y+z) / 3.;
}
• takes three integers
• returns their arithmetic average value (a double type)
Page 31
Day11.C Young W. Lim
0.23 init arrays() function
//--------------------------------------------------
// Initialize X[4][SIZE] arrays
// by assigning random number grade
//--------------------------------------------------
void init_arrays (int X[][SIZE], double A[])
{
int i;
// srand(7) makes rand() generate
// the same random sequence
// --> easy to debug a program
srand(7);
for (i=0; i<SIZE; ++i) {
X[0][i] = i+1 + 201600; // I
X[1][i] = rand() % 101; // K
X[2][i] = rand() % 101; // E
X[3][i] = rand() % 101; // M
A[i] = avg3(X[1][i], X[2][i], X[3][i]);
}
}
• takes a 2-d array name and a 1-d array name
– the int type 2-d arrays : (X)
– the double type 1-d array : (A)
• returns nothing
• fill the X[1], X[2], and X[3] 1-d subarrays with random numbers
• A[i] is filled with the average value of X[1][i], X[2][i], and X[3][i]
• X[0][i] is filled with the expression i+1+201600
• all the elements of X are modified
Page 32
Day11.C Young W. Lim
0.24 pr table() function
//--------------------------------------------------
// Print the original table
//-------------------------------------------------
void pr_table (int X[][SIZE], double A[])
{
int i;
printf("%10s %10s %10s %10s %10s \n", "StID",
"Korean", "Enlgish", "Math", "Average");
for (i=0; i<SIZE; ++i) {
printf("%10d %10d %10d %10d %10.2f \n",
X[0][i], X[1][i], X[2][i], X[3][i], A[i]);
}
}
• takes a 2-d array name and a 1-d array name
– the int type 2-d arrays : (X)
– the double type 1-d array : (A)
• returns nothing
• print the student ID, test scores of Korean, English, and Mathematics,and their average score row by row
• header
%10s %10s %10s %10s %10s“StID” “Korean” “English” “Math” “Average”
• each row
%10d %10d %10d %10d %10.2fX[0][i] X[1][i] X[2][i] X[3][i] A[i]
• StID Korean English Math Average
201601 17 78 99 64.67
201602 65 87 61 71.00
201603 21 46 91 52.67
201604 100 7 31 46.00
201605 14 63 42 39.67
201606 72 78 68 72.67
201607 9 100 68 59.00
201608 84 20 2 35.33
201609 29 79 62 56.67
201610 69 53 94 72.00
• no array is modified.
Page 33
Day11.C Young W. Lim
0.25 Dbubblesort() function
//--------------------------------------------------
// Bubble Sort Double Array
//-------------------------------------------------
void DbubbleSort(double a[], int size)
{
int p, j;
double tmp;
for (p=1; p< size; ++p) {
for (j=0; j< size-1; ++j) {
if ( a[j] < a[j+1] ) {
tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
}
}
}
}
• takes the 1-d array name of the double type and the array size
• returns nothing
• there are size-1 passes : for (p=1; p<size; ++p) { ... }
• in each pass, perform the following basic operation
over size-1 pairs of adjacent elements : for (j=0; j<size-1; ++j) { ... }
– for a given j, compare the pair a[j] and a[j+1]
– if ( a[j] < a[j+1] ) then swap each other
– thus ensuring a[j] is greater than or equal to a[j+1]
– the next pair to be compared will be
∗ a[j+1] and a[j+2] in terms of the old value of j
∗ a[j] and a[j+1] in terms of the incremented value of j
• the current element is moved to the right (increasing index)
until the smaller element is found
• after p-1 passes, the array elements are in the increasing order.
• the given array is modified
Page 34
Day11.C Young W. Lim
0.26 pr sorted table() function
//--------------------------------------------------
// Print the Sorted Table
//-------------------------------------------------
void pr_sorted_table (int X[][SIZE], double A[])
{
int i, j;
double B[SIZE]; // Backup Array for Sorting
for (i=0; i<SIZE; ++i) B[i] = A[i];
//....................
DbubbleSort(B, SIZE);
//....................
printf("\n\nSorted on a student’s average\n\n");
printf("%10s %10s %10s %10s %10s \n", "StID",
"Korean", "Enlgish", "Math", "Average");
for (i=0; i<SIZE; ++i) {
for (j=0; j<SIZE; ++j) if (B[i] == A[j]) break;
printf("%10d %10d %10d %10d %10.2f \n",
X[0][j], X[1][j], X[2][j], X[3][j], A[j]);
}
}
• takes a 2-d array name and a 1-d array name
– the int type 2-d arrays : (X)
– the double type 1-d array : (A)
• returns nothing
• initially, all the rows of X array and A array are sorted by the student ID
• copy A array into B array
• sort B array : DbubbleSort(B, SIZE);
• only B array are in the increasing order of the average score
• for each B[i] : for (i=0; i<SIZE; ++i)
– find the index j such that A[j]=B[i] : for (j=0; j<SIZE; ++j)
– print the (j+1)-th row of the original table
–%10d %10d %10d %10d %10.2fX[0][j] X[1][j] X[2][j] X[3][j] A[j]
• no array is modified
Page 35
Day11.C Young W. Lim
0.27 Avg() function
//--------------------------------------------------
// Average over Integer Array
//-------------------------------------------------
double Avg(int M[], int n) {
int i; double S=0.0;
for (i=0; i<n; ++i) S+= M[i];
return S/n;
}
• takes a 1-d int array name and its size
• returns its arithmetic average value (a double type)
• 1n
∑n−1i=0 X[i]
Page 36
Day11.C Young W. Lim
0.28 DAvg() function
//--------------------------------------------------
// Average over Doubl Array
//-------------------------------------------------
double DAvg(double N[], int n) {
int i; double S=0.0;
for (i=0; i<n; ++i) S+= N[i];
return S/n;
}
• takes a 1-d double array name and its size
• returns its arithmetic average value (a double type)
• 1n
∑n−1i=0 Y [i]
Page 37
Day11.C Young W. Lim
0.29 pr averages() function
//--------------------------------------------------
// Print the Averages
//-------------------------------------------------
void pr_averages(int X[][SIZE], double A[]) {
double A1 = Avg(X[1], SIZE);
double A2 = Avg(X[2], SIZE);
double A3 = Avg(X[3], SIZE);
double A4 = DAvg(A, SIZE);
printf("%10s %10.2f %10.2f %10.2f %10.2f \n",
"Average", A1, A2, A3, A4);
}
• takes a 2-d array name and a 1-d array name
– the int type 2-d arrays : (X)
– the double type 1-d array : (A)
• returns nothing
• A1 : the average score of the Korean subject
• A2 : the average score of the English subject
• A3 : the average score of the Mathematics subject
• A4 : the average score of each student’s average score
• %10s %10.2f %10.2f %10.2f %10.2f”Average” A1 A2 A3 A4
• no array is modified
Page 38
Day11.C Young W. Lim
0.30 main() function
//====================================================
// main
//====================================================
int main(void) {
// X[0][SIZE] --> I[SIZE]; // ID of a student
// X[1][SIZE] --> K[SIZE]; // Grade of Korean
// X[2][SIZE] --> E[SIZE]; // Grade of English
// X[3][SIZE] --> M[SIZE]; // Grade of Math
int X[4][SIZE];
double A[SIZE]; // Average of a student
init_arrays(X, A);
pr_table(X, A);
pr_sorted_table(X, A);
pr_averages(X, A);
}
• declare a 2-d array and a 1-d array
– int X[4][SIZE];
– double A[SIZE];
• call init_arrays() function
• call pr_table() function
• call pr_sorted_table() function
• call pr_averages() function
Page 39
Day11.C Young W. Lim
0.31 When students have the same average
test cases the previous codes do not work in such case.
/--------------------------------------------------
// Initialize K[], E[], M[] arrays
// by assigning random number grade
//--------------------------------------------------
void init_arrays
(int I[], int K[], int E[], int M[], double A[])
{
int i;
// srand(7) makes rand() generate
// the same random sequence
// --> easy to debug a program
srand(7);
for (i=0; i<SIZE; ++i) {
I[i] = i+1 + 201600;
K[i] = rand() % 101;
E[i] = rand() % 101;
M[i] = rand() % 101;
A[i] = avg3(K[i], E[i], M[i]);
}
X[1][3] = X[1][2];
X[2][3] = X[2][2];
X[3][3] = X[3][2];
A[3] = A[2];
X[1][5] = X[1][2];
X[2][5] = X[2][2];
X[3][5] = X[3][2];
A[5] = A[2];
X[1][7] = X[1][2];
X[2][7] = X[2][2];
X[3][7] = X[3][2];
A[7] = A[2];
}
Page 40
Day11.C Young W. Lim
• rows having index values of 2, 3, 5, 7 have the same score.
• only one student ID (201603) is repeated.
StID Korean English Math Average
201610 69 53 94 72.00
201602 65 87 61 71.00
201601 17 78 99 64.67
201607 9 100 68 59.00
201609 29 79 62 56.67
201603 21 46 91 52.67
201603 21 46 91 52.67
201603 21 46 91 52.67
201603 21 46 91 52.67
201605 14 63 42 39.67
Average 28.70 64.40 79.00 57.37
Page 41
Day11.C Young W. Lim
0.32 Handling the same average cases - Method 1
using extra memory
• using index array : int C[SIZE];
• swap both average score and its index : DbubbleSort()
//--------------------------------------------------
// Bubble Sort Double Array
//-------------------------------------------------
void DbubbleSort(double b[], int c[], int size)
{
int p, j, t;
double tmp;
for (p=1; p< size; ++p) {
for (j=0; j< size-1; ++j) {
if ( b[j] < b[j+1] ) {
tmp = b[j];
b[j] = b[j+1];
b[j+1] = tmp;
t = c[j];
c[j] = c[j+1];
c[j+1] = t;
}
}
}
}
Page 42
Day11.C Young W. Lim
//--------------------------------------------------
// Print the Sorted Table
//-------------------------------------------------
void pr_sorted_table (int X[][SIZE], double A[])
{
int i, j;
double B[SIZE]; // Backup Array for Sorting
int C[SIZE];
for (i=0; i<SIZE; ++i) {
B[i] = A[i];
C[i] = i;
}
//....................
DbubbleSort(B, C, SIZE);
//....................
printf("\n\nSorted on a student’s average\n\n");
printf("%10s %10s %10s %10s %10s \n", "StID",
"Korean", "Enlgish", "Math", "Average");
for (i=0; i<SIZE; ++i) {
j = C[i];
printf("%10d %10d %10d %10d %10.2f \n",
X[0][j], X[1][j], X[2][j], X[3][j], A[j]);
}
}
Page 43
Day11.C Young W. Lim
0.33 Handling the same average cases - Method 2
using extra computation
• counting the same average cases: int cnt;
• search all indices for the same average : j
//--------------------------------------------------
// Print the Sorted Table
//-------------------------------------------------
void pr_sorted_table (int X[][SIZE], double A[])
{
int i, j, cnt;
double B[SIZE]; // Backup Array for Sorting
for (i=0; i<SIZE; ++i) B[i] = A[i];
//....................
DbubbleSort(B, SIZE);
//....................
printf("\n\nSorted on a student’s average\n\n");
printf("%10s %10s %10s %10s %10s \n", "StID",
"Korean", "Enlgish", "Math", "Average");
for (i=0; i<SIZE; ++i) {
cnt=1;
while (B[i-cnt]==B[i]) cnt++;
j=0;
while (cnt>0) {
cnt--;
while (A[j]!=B[i]) j++;
if (cnt > 0) j++;
}
printf("%10d %10d %10d %10d %10.2f \n",
X[0][j], X[1][j], X[2][j], X[3][j], A[j]);
}
}
Page 44