Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1010 Discussion Group 11 Week 7 – Two dimensional arrays.

Similar presentations


Presentation on theme: "CS1010 Discussion Group 11 Week 7 – Two dimensional arrays."— Presentation transcript:

1 CS1010 Discussion Group 11 Week 7 – Two dimensional arrays

2 Slides are at http://www.comp.nus.edu.sg/~yanhwa/
HELLO! Slides are at

3 Practical Exam Tips Know how to use %c in printf, using %d will print the ascii value of a number (the number that you get when you convert a character to an integer) Use pow() for power, but remember that if there is a warning that complains “pow is undefined” then you did not import math.h or you did not compile with –lm flag Be familiar with error messages: floating-point exception is (usually) when you try to divide a float with zero.

4 To use “n” or avoid “n”? Just use num
Practical Exam Tips A function prototype or function interface is a declaration of a function that specifies the function's name and type signature (data types of parameters) and return type. Thus if you face warnings regarding prototypes, check all these. To use “n” or avoid “n”? Just use num Preconditions. Indentation. Comments. Description. Your program must be able to compile! -5 for compiler warning with –Wall. Only declare variables at the start of a function

5 Take note of overflow (which variables need to be long long int type?)
Practical Exam Tips Seesoo has to be done with two nested loops because the first loop is to calculate the “seesoo”. Have to repeat this calculation while seesoo > 9. Easiest to do teetoo if do it step by step. Remove the last group of three first, which is making life difficult. After that, it is easy to deal with groups of 4 (with one number that you ignore) Take note of overflow (which variables need to be long long int type?) Invalid operands to binary % (have ‘long long int’ and ‘double’) Only 10% %0 = infinity…

6 Practical Exam Tips Use two SSH Secure Shell Clients, one to program, one to compile. Don’t break the “flow” of programming Press “up arrow” to traverse through the command history Remember, you can printf everywhere to debug your program. Figure out which part causes the error/bug by following the “flow” of the program and mentally tracing it out, with the help of the printf statements. printf(“digit: %d”, digit); Comment out parts of program if unable to compile or there are warning messages and see if there are any change. Syntax errors vs run-time errors Check output whether it conforms with requirements!!! Don’t give up, we try to award as many marks as possible especially if there is only minor logic errors :’) Be familiar with char and int conversion

7 int arr[3] = {0} all elements initialized to zero
Practical Exam Tips Debug infinite loops using printf. Find the flow of the program, see what will be printed. Set variable counter = 0. break if counter == some number. Input/output stream redirection ./a.out < seesoo1.in ./a.out < seesoo1.in > ans.out saves the output diff ans.out seesoo1.out compares correct output with your output int arr[3] = {0} all elements initialized to zero Int arr[3] = {1} only first element initialized to 1

8 Be familiar with char and int.
PE Tips Be familiar with char and int. printf("%c\n", (char) ((int) 'C' + beeboo_val%10)); char[] string[i] = number + '0'; int value = c - '0';

9 PE Tips

10 Have you seen the past year papers? (and answers?)
Midterms Midterms on Friday! Have you seen the past year papers? (and answers?)

11 Lab 4 Exercise 2 worksheet

12 THE ARRAY data structure
Lecture Summary A function header with array parameter, int sum(int a[ ], int size) Why is it not necessary to indicate size of array in the brackets? Each integer is 4 bytes big. 4 bytes = 4x8 bits = 32 bits!

13 THE ARRAY data structure
Lecture Summary How to declare 2D array? // the first dimension can be unspecified int b[][5] = { {4, 2, 1, 0, 0}, {8, 3, 3, 1, 6}, {0, 0, 0, 0, 0} }; // initializer with implicit zero values int d[3][5] = { {4, 2, 1}, {8, 3, 3, 1, 6} }; Why must we specify the 2nd dimension?

14 2D arrays where memory is linear and row-major order
Lecture Summary 2D arrays where memory is linear and row-major order Address of A[i][j] = base_address + size * (i * num_cols + j)

15 Linear Search (Just searching…) O(n)
Lecture Summary Linear Search (Just searching…) O(n) Binary Search (Searching a phone book) Bubble Sort (Bubbling the max to the back) Selection Sort (Selecting the minimum to be placed at the front) Insertion Sort (Putting each element at its correct position backwards, like sorting a hand of cards)

16

17 Tutorial 1b Yes, all of the formats work. For numerical inputs, scanf() skips whitespaces to read the next value.

18 Tutorial 2 \n \n The newline character will be read into the element values[1][0]. Could correct it by using a dummy character to hold the newline. Alternatively, the program could be re-written by reading a string for each row of characters. (String will be covered in week 9 lecture.)

19 Tutorial 3 2 3 4 5 4 5 7 8 6

20 Product of two matrices

21 Diagonal and triangular
Send me your programs!

22 Find the most frequent value in a 2D array?
Extra Find the most frequent value in a 2D array? 1. If you know the max value of the array 2. if you don’t know the max value maximum-repeating-number-in-ok-time/

23 NIRC check digit, weights and alphabet letter
Extra NIRC check digit, weights and alphabet letter

24 Add to your cheatsheet!

25 Midterms PYP


Download ppt "CS1010 Discussion Group 11 Week 7 – Two dimensional arrays."

Similar presentations


Ads by Google