Download presentation
Presentation is loading. Please wait.
Published byBuck Carpenter Modified over 9 years ago
1
CS1010E Programming Methodology Tutorial 9 Pointers in Arrays & Structures C14,A15,D11,C08,C11,A02
2
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
3
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; }
4
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 tmp Sort tmp Copy tmp back to a[][j]
5
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
6
Question 1(c) sortRowMajorOrder: sort every element in a matrix Note that the memory for matrix is consecutive Matrix 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)
7
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) Module Manager (your main function) Read/Write module information Input/output (User Interaction) Module Adder (your function) Module Adder (your function) Module Remover (your function) Module Remover (your function) Module Printer (your function) Module Printer (your function) Timetable Printer (your function) Timetable Printer (your function) Application Structure
8
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;
9
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];
10
Question 2 Main function: read in module list response to user input
11
Question 2 Notice the pattern of handling user input
12
Question 2 Read in module information from File
13
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
14
Question 2 Add Module Fetch Module Check Validity Code for module adder
15
Question 2 Fetch the module from module set
16
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
17
Code for module removal Fetch List Check validity Removal Update
18
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.