CS1100 Introduction to Programmingmeghana/CS1100-slides/Week8.pdf · CS1100 { Introduction to...

Post on 25-Jul-2020

4 views 0 download

transcript

CS1100 – Introduction to Programming

Week 8: Modular ProgrammingSorting Integer Arrays

Goals for the week

1. Functions spread across files.• How to compile individual files.• Linking object files.•

2. Sorting arrays.• Selection sort.• Insertion sort.

Functions across files

3 students collaborate on an assignment on Matrix operations.

• S 1: Input Output – Read Matrix, Print Matrix.

• S 2: Operations – Matrix Multiply, Matrix Add.

• S 3: Interfacing and Testing – Main program, test cases.

• All 3 students agree on the signatures of the functions.

#include<stdio.h>

#define N 10

void readMatrix(int rows, int cols, int Mat[][N]);

void printMatrix(int rows, int cols, int Mat[][N]);

void addMat(int rows, int cols, int Mat1[][N],

int Mat2[][N], int output[][N]);

void multMat(int rows, int cols, int Mat1[][N],

int Mat2[][N], int output[][N]);

Functions across files

3 students collaborate on an assignment on Matrix operations.

• S 1: Input Output – Read Matrix, Print Matrix.

• S 2: Operations – Matrix Multiply, Matrix Add.

• S 3: Interfacing and Testing – Main program, test cases.

• All 3 students agree on the signatures of the functions.

#include<stdio.h>

#define N 10

void readMatrix(int rows, int cols, int Mat[][N]);

void printMatrix(int rows, int cols, int Mat[][N]);

void addMat(int rows, int cols, int Mat1[][N],

int Mat2[][N], int output[][N]);

void multMat(int rows, int cols, int Mat1[][N],

int Mat2[][N], int output[][N]);

Functions across files

3 students collaborate on an assignment on Matrix operations.

• S 1: Input Output – Read Matrix, Print Matrix.

• S 2: Operations – Matrix Multiply, Matrix Add.

• S 3: Interfacing and Testing – Main program, test cases.

• All 3 students agree on the signatures of the functions.

#include<stdio.h>

#define N 10

void readMatrix(int rows, int cols, int Mat[][N]);

void printMatrix(int rows, int cols, int Mat[][N]);

void addMat(int rows, int cols, int Mat1[][N],

int Mat2[][N], int output[][N]);

void multMat(int rows, int cols, int Mat1[][N],

int Mat2[][N], int output[][N]);

S 1 writes a file – ip.c

#include "header.h"

void readMatrix(int rows, int cols, int Mat[][N]) {

// code to read matrix.

}

void printMatrix(int rows, int cols, int Mat[][N]) {

// code to print matrix.

}

• Compiles the code using gcc -c ip.c

• Ensures that there are no syntax errors as far as ip.c isconcerned.

S 1 writes a file – ip.c

#include "header.h"

void readMatrix(int rows, int cols, int Mat[][N]) {

// code to read matrix.

}

void printMatrix(int rows, int cols, int Mat[][N]) {

// code to print matrix.

}

• Compiles the code using gcc -c ip.c

• Ensures that there are no syntax errors as far as ip.c isconcerned.

S 2 writes a file – ops.c

#include "header.h"

void addMat(int rows, int cols, int Mat1[][N],

int Mat2[][N], int output[][N]) {

// code to add two matrices.

}

void multMat(int rows, int cols, int Mat1[][N],

int Mat2[][N], int output[][N]) {

// code to add multiply matrices.

}

• Compiles the code using gcc -c ops.c

• Ensures that there are no syntax errors as far as ops.c isconcerned.

S 3 writes a file – main.c

#include "header.h"

void main() {

int mat1[N][N];

int mat2[N][N];

int mat3[N][N], mat4[N][N];

readMatrix(N, N, mat1);

readMatrix(N, N, mat2);

addMat(N, N, mat1, mat2, mat3);

multMat(N, N, mat1, mat2, mat4);

}

• Compiles the code using gcc -c main.c• Ensures that there are no syntax errors as far as main.c is

concerned.

Finally create an executable

• Finally compile the program asgcc main.c ip.c ops.c

• Alternativelygcc main.o ip.o ops.o

• How is this different including the files directly in main asfollows?#include “ip.c”#include “ops.c”

• Recommended: Single header file having all the declarations.

• Include header file in all your .c files.

Finally create an executable

• Finally compile the program asgcc main.c ip.c ops.c

• Alternativelygcc main.o ip.o ops.o

• How is this different including the files directly in main asfollows?#include “ip.c”#include “ops.c”

• Recommended: Single header file having all the declarations.

• Include header file in all your .c files.

Finally create an executable

• Finally compile the program asgcc main.c ip.c ops.c

• Alternativelygcc main.o ip.o ops.o

• How is this different including the files directly in main asfollows?#include “ip.c”#include “ops.c”

• Recommended: Single header file having all the declarations.

• Include header file in all your .c files.

Finally create an executable

• Finally compile the program asgcc main.c ip.c ops.c

• Alternativelygcc main.o ip.o ops.o

• How is this different including the files directly in main asfollows?#include “ip.c”#include “ops.c”

• Recommended: Single header file having all the declarations.

• Include header file in all your .c files.

Sorting Integer Arrays

Sort the array in decreasing order

15 8 3 12 30 7 9 17 32 19

One possible way:

• Find max, place it at first location.

• Sort the array from second location to end.

• What functions would be useful to implement sorting?• getMax element in the array starting from a particular location.• swap two values in given array locations.

Sort the array in decreasing order

15 8 3 12 30 7 9 17 32 19

One possible way:

• Find max, place it at first location.

• Sort the array from second location to end.

• What functions would be useful to implement sorting?• getMax element in the array starting from a particular location.• swap two values in given array locations.

Sort the array in decreasing order

15 8 3 12 30 7 9 17 32 19

One possible way:

• Find max, place it at first location.

• Sort the array from second location to end.

• What functions would be useful to implement sorting?

• getMax element in the array starting from a particular location.• swap two values in given array locations.

Sort the array in decreasing order

15 8 3 12 30 7 9 17 32 19

One possible way:

• Find max, place it at first location.

• Sort the array from second location to end.

• What functions would be useful to implement sorting?• getMax element in the array starting from a particular location.• swap two values in given array locations.

Selection sort

15 8 3 12 30 7 9 17 32 19

32 8 3 12 30 7 9 17 15 19

32 30 3 12 8 7 9 17 15 19...

......

......

......

......

...

32 30 19 17 15 12 9 8 7 3

Pseudo-code

• while (i ≤ n )• maxIndex = findMaxIndex(array, i, n);• swap(array, maxindex, i);

Selection sort

15 8 3 12 30 7 9 17 32 19

32 8 3 12 30 7 9 17 15 19

32 30 3 12 8 7 9 17 15 19...

......

......

......

......

...

32 30 19 17 15 12 9 8 7 3

Pseudo-code

• while (i ≤ n )• maxIndex = findMaxIndex(array, i, n);• swap(array, maxindex, i);

Selection sort

15 8 3 12 30 7 9 17 32 19

32 8 3 12 30 7 9 17 15 19

32 30 3 12 8 7 9 17 15 19

......

......

......

......

......

32 30 19 17 15 12 9 8 7 3

Pseudo-code

• while (i ≤ n )• maxIndex = findMaxIndex(array, i, n);• swap(array, maxindex, i);

Selection sort

15 8 3 12 30 7 9 17 32 19

32 8 3 12 30 7 9 17 15 19

32 30 3 12 8 7 9 17 15 19...

......

......

......

......

...

32 30 19 17 15 12 9 8 7 3

Pseudo-code

• while (i ≤ n )• maxIndex = findMaxIndex(array, i, n);• swap(array, maxindex, i);

Selection sort – main-program

1 #i n c l u d e ” so r t−header . h”2

3 main ( ) {4 i n t a r r a y [N] = {15 , 8 , 3 , 12 , 30 , 7 , 9 , 17 , 32 ,

19} ;5

6 p r i n tA r r a y ( a r r a y ) ;7 i n t i = 0 ;8 f o r ( i =0; i<N; i++) {9 i n t j = f indMax Index ( a r ray , i , N−1) ;

10 i f ( j != i ) {11 swap ( a r ray , i , j ) ;12 }13 }14

15 p r i n tA r r a y ( a r r a y ) ;16 }

Selection sort – helper functions

1 #i n c l u d e ” so r t−header . h”2 /∗∗∗∗∗ f u n c t i o n s f o r s e l e c t i o n s o r t ∗∗∗∗/3 i n t f i ndMax Index ( i n t a r r a y [N] , i n t s t a r t , i n t end ) {4 i n t max = a r r a y [ s t a r t ] ;5 i n t maxIndex = s t a r t ;6 i n t i = s t a r t ;7 wh i l e ( i <= end ) {8 i f ( a r r a y [ i ] > max) {9 max = a r r a y [ i ] ;

10 maxIndex = i ;11 }12 i ++;13 }14 r e t u r n maxIndex ;15 }

Selection sort – helper functions

1 #i n c l u d e ” so r t−header . h”2 /∗∗∗ common p r i n t f u n c t i o n ∗∗∗/3 vo i d p r i n tA r r a y ( i n t a r r a y [N] ) {4 i n t i ;5 f o r ( i =0; i < N; i++) {6 p r i n t f ( ”%d\ t ” , a r r a y [ i ] ) ;7

8 }9 p r i n t f ( ”\n” ) ;

10 }11

12 vo i d swap ( i n t a r r a y [N] , i n t index1 , i n t i ndex2 ) {13 i n t temp = a r r a y [ i ndex1 ] ;14 a r r a y [ i ndex1 ] = a r r a y [ i ndex2 ] ;15 a r r a y [ i ndex2 ] = temp ;16 }

Selection sort – header file

1 #inc l ud e<s t d i o . h>2

3 #de f i n e N 104 i n t f i ndMax Index ( i n t a r r a y [N] , i n t s t a r t , i n t end ) ;5 vo i d swap ( i n t a r r a y [N] , i n t index1 , i n t i ndex2 ) ;

Selection sort – number of comparisons

• Which input do we consider?

• Do number of comparisons depend on the particularpermutation of input?

• How does the method perform when the array is nearly sorted?

• Consider a “worst-case” input.

• Irrespective of whether the array is sorted or not, the methodalways needs n(n−1)

2 comparisons.

Insertion Sort

15 8 3 12 30 7 9 17 32 19

15 8 3 12 30 7 9 17 32 19

15 12 8 3 30 7 9 17 15 19...

......

......

......

......

...

32 30 19 17 15 12 9 8 7 3

• Although final result is the same, intermediate steps aredifferent from selection sort.

Insertion Sort

15 8 3 12 30 7 9 17 32 19

15 8 3 12 30 7 9 17 32 19

15 12 8 3 30 7 9 17 15 19...

......

......

......

......

...

32 30 19 17 15 12 9 8 7 3

• Although final result is the same, intermediate steps aredifferent from selection sort.

Insertion Sort

15 8 3 12 30 7 9 17 32 19

15 8 3 12 30 7 9 17 32 19

15 12 8 3 30 7 9 17 15 19

......

......

......

......

......

32 30 19 17 15 12 9 8 7 3

• Although final result is the same, intermediate steps aredifferent from selection sort.

Insertion Sort

15 8 3 12 30 7 9 17 32 19

15 8 3 12 30 7 9 17 32 19

15 12 8 3 30 7 9 17 15 19...

......

......

......

......

...

32 30 19 17 15 12 9 8 7 3

• Although final result is the same, intermediate steps aredifferent from selection sort.

Insertion Sort

15 8 3 12 30 7 9 17 32 19

15 8 3 12 30 7 9 17 32 19

15 12 8 3 30 7 9 17 15 19...

......

......

......

......

...

32 30 19 17 15 12 9 8 7 3

• Although final result is the same, intermediate steps aredifferent from selection sort.

Insertion sort – main program

1 #i n c l u d e ” so r t−header . h”2

3 vo i d i n s e r tMax ( i n t a r r a y [ ] , i n t i nd ex ) ;4 main ( ) {5 i n t a r r a y [N] = {15 , 8 , 3 , 12 , 30 , 7 , 9 , 17 , 32 ,

19} ;6

7 p r i n tA r r a y ( a r r a y ) ;8 i n t i ;9 f o r ( i =1; i<N; i++) {

10 i n s e r tMax ( a r ray , i ) ;11 }12

13 p r i n tA r r a y ( a r r a y ) ;14 }

Insertion sort – number of comparisons

1 #i n c l u d e ” so r t−header . h”2

3 vo i d i n s e r tMax ( i n t a r r a y [ ] , i n t i nd ex ) {4 i n t temp = a r r a y [ i nd ex ] ;5 i n t j = i ndex ;6 wh i l e ( j > 0 && a r r a y [ j −1] < temp ) {7 a r r a y [ j ] = a r r a y [ j −1] ;8 j−−;9 }

10 a r r a y [ j ] = temp ;11 }

• What happens if array[index-1] ≥ array[index]?

• InsertMax does a single comparison and returns to main.

• Thus if array is sorted, each time insertMax returns after asingle comparison.

• Number of comparisons ≈ n for best case scenario.

Merging two sorted arrays

Given two sorted arrays each of size N, create another array of size2N which contains the “merge” of the two arrays.

• void merge(int A[], int B[], int C[]);