19
CS1010E Programming Methodology Tutorial 9 Pointers in Arrays & Structures

CS1010E Programming Methodology Tutorial 9 Pointers in Arrays & Structures C14,A15,D11,C08,C11,A02

Embed Size (px)

Citation preview

Page 1: CS1010E Programming Methodology Tutorial 9 Pointers in Arrays & Structures C14,A15,D11,C08,C11,A02

CS1010E Programming MethodologyTutorial 9

Pointers in Arrays & Structures

Page 2: CS1010E Programming Methodology Tutorial 9 Pointers in Arrays & Structures C14,A15,D11,C08,C11,A02

Question 1

Practicing on Matrix Sorting.

We assume we know how to sort a 1-D array we have bubble sort, insertion sort, selection sort ..

How to sort a matrix ?// Function to sort the one-dimensional array list of n elements. void sort(int list[], int n) {

int i, j, minIndex, temp; for (i = 0; i < n-1; i++) {

minIndex = i; for (j = i+1; j < n; j++) if (list[j] < list[minIndex])

minIndex = j; temp = list[i]; list[i] = list[minIndex];

list[minIndex] = temp; }return;

}Selection sort

Page 3: CS1010E Programming Methodology Tutorial 9 Pointers in Arrays & Structures C14,A15,D11,C08,C11,A02

Question 1 (a)

rowSort Sort each row in ascending order

void rowSort(int table[][MAX], int rows, int cols) { int i; for (i = 0; i < rows; i++)

sort(table[i],cols); return;

}

Page 4: CS1010E Programming Methodology Tutorial 9 Pointers in Arrays & Structures C14,A15,D11,C08,C11,A02

Question 1(b)

colSort :Need to sort each column Analysis:

our sort function takes an array, but a column of a matrix is not an array (Why?) we can create a temp array, to store a column of a matrix

Algorithm:For each column a[][j] of array

Create a temp array tmp[j]Copy column a[][j] to tmpSort tmpCopy tmp back to a[][j]

Page 5: CS1010E Programming Methodology Tutorial 9 Pointers in Arrays & Structures C14,A15,D11,C08,C11,A02

Question 1(b)

// Function to sort each column of table. void colSort(int table[][MAX], int rows, int cols) {

int i,j; int listPtr[MAX]; for (i = 0; i < cols; i++) {

for (j = 0; j < rows; j++) listPtr[j] = table[j][i];

sort(listPtr, rows); for (j = 0; j < rows; j++)

table[j][i] = listPtr[j]; } return;

}

Copy column to tmp

Copy tmp back

Page 6: CS1010E Programming Methodology Tutorial 9 Pointers in Arrays & Structures C14,A15,D11,C08,C11,A02

Question 1(c)

sortRowMajorOrder: sort every element in a matrix

Note that the memory for matrix is consecutive

2 -17 11

-1 11 -7

0 3 -2

Matrix

2 -17 11 -1 11 -7 0 3 -2

Memory layout for matrix// Function to sort table in row-major order. void sortRowMajorOrder(int table[][MAX], int rows, int cols) { // pass the address of the first element (which is the address of an integer) sort(&table[0][0], rows * cols); return; }

Why &table[0][0] ?Is the following correct:• sort(table, row*col)• sort(table[0], row * col)• sort(&table[0], row *col)

Page 7: CS1010E Programming Methodology Tutorial 9 Pointers in Arrays & Structures C14,A15,D11,C08,C11,A02

Question 2

Design a module management system ! This question exams you several key points in C and shows you are already capable of writing applications !!

Module File Console

Module Manager(your main function)

Read/Write module information

Input/output(User Interaction)

Module Adder(your function)

Module Remover(your function)

Module Printer(your function)

Timetable Printer(your function)

Application Structure

Page 8: CS1010E Programming Methodology Tutorial 9 Pointers in Arrays & Structures C14,A15,D11,C08,C11,A02

Question 2

Data Model

We have two data types to deal with Schedule

Day, Start time, End time

Module Module code, Lec1, Lec2, Tutorial, MCs

typedef struct{ int day; int startTime; int endTime; }Schedule;

typedef struct{ char moduleCode[10]; Schedule lecture1; Schedule lecture2; Schedule tutorial; int numberOfMCs; }Module;

Page 9: CS1010E Programming Methodology Tutorial 9 Pointers in Arrays & Structures C14,A15,D11,C08,C11,A02

Question 2

Data Structures we needed: every student has a limit of 6 modules:

Module* myModuleList[6] = {NULL};

a total module set Module list[1000];

every student needs a time table, 11 time slot, 5 day Module* timeTable[11][5];

Page 10: CS1010E Programming Methodology Tutorial 9 Pointers in Arrays & Structures C14,A15,D11,C08,C11,A02

Question 2

Main function: read in module list response to user input

Page 11: CS1010E Programming Methodology Tutorial 9 Pointers in Arrays & Structures C14,A15,D11,C08,C11,A02

Question 2 Notice the pattern of handling user input

Page 12: CS1010E Programming Methodology Tutorial 9 Pointers in Arrays & Structures C14,A15,D11,C08,C11,A02

Question 2

Read in module information from File

Page 13: CS1010E Programming Methodology Tutorial 9 Pointers in Arrays & Structures C14,A15,D11,C08,C11,A02

Question 2

Module Adder Add a module to the module list based on module code

Algorithm: Fetch module from module list with given module code Check if there is a time conflict with existing schedule Check whether the student has take more than 6 modules If the module has no confliction, update the student’s module taken list and time table

Page 14: CS1010E Programming Methodology Tutorial 9 Pointers in Arrays & Structures C14,A15,D11,C08,C11,A02

Question 2

Add Module

Fetch Module

Check Validity

Code for module adder

Page 15: CS1010E Programming Methodology Tutorial 9 Pointers in Arrays & Structures C14,A15,D11,C08,C11,A02

Question 2

Fetch the module from module set

Page 16: CS1010E Programming Methodology Tutorial 9 Pointers in Arrays & Structures C14,A15,D11,C08,C11,A02

Question 2

Module Remover Remove a module based on the module code

Algorithm: Fetch module from module list Check whether a student has taken this module or not Remove module from module taken list , update time table

Page 17: CS1010E Programming Methodology Tutorial 9 Pointers in Arrays & Structures C14,A15,D11,C08,C11,A02

Code for module removal

Fetch List

Check validity

Removal

Update

Page 18: CS1010E Programming Methodology Tutorial 9 Pointers in Arrays & Structures C14,A15,D11,C08,C11,A02

Question 2

Print Module:Print out module information for a student

void printModules(Module* myModuleList[]) { int i; int MCs = 0; for(i=0;i<6;i++) if(myModuleList[i] != NULL) {

printf("%s\n", myModuleList[i]->moduleCode); printf("lecture "); printDay(myModuleList[i]->lecture1.day); printf("%d - %d\n", myModuleList[i]->lecture1.startTime,

myModuleList[i]->lecture1.endTime); …

MCs += myModuleList[i]->numberOfMCs; };

printf("Total number of MCs taken: %d\n", MCs); }

Counting MCs

Page 19: CS1010E Programming Methodology Tutorial 9 Pointers in Arrays & Structures C14,A15,D11,C08,C11,A02

Thank you !Next week is for review!