CS1010 Discussion Group 11 Week 6 – One dimensional arrays
Slides are at http://www.comp.nus.edu.sg/~yanhwa/ HELLO! Slides are at http://www.comp.nus.edu.sg/~yanhwa/
Lab 2 Tips Did not put repeated ordinal number lines of code in sum and average in a separate function print_ordinal Can use round() for averaging, besides +0.5 or x10 and checking last digit Remember to initialise variables before using it in while condition (eg. command_code) Indentation. Use gg=G. Note indentation of break; and ifelse Meaningful variable names. Num, sum, index, tmp, calc Comments, last_digit = index % 10. When does while loop end? What are your assumptions for input? Recursion, arrays not allowed Input to be entered is from 1 to 1000, need to take care of 111, 112, 113 too Complex logic – unnecessary cases, else if else if else if
Multiple ways to do lab 2
Moral of the story: Try not to mix I/O and calculation Lab 2 Tips For tasks 2 and 3, the I/O and the calculation may be mixed inside functions, which may be considered BAD function design in CS1010. But since currently we haven't taught arrays, this can be tolerated in lab2. Moral of the story: Try not to mix I/O and calculation
signal = parse_command(); } while (signal != -1); Lab 2 answer in main()…. int signal = 1; do { signal = parse_command(); } while (signal != -1); Thus if (command_code == COMMAND_EXIT) will eventually return -1 in parse_command(). Everything else will return 0 in parse_command(). Parse_command() uses a while(1) loop but the return statement will exit out of the entire function. Use do_while(command_code != COMMAND_EXIT), or use a similar while loop. Note that using a while loop, you need to initialize the value of command_code, but for do_while it is unnecessary.
Lab CodeCrunch Lab #3 Have you submitted?
PE on Friday! Lab Check your session. Session 2? This is an open-book test. You may bring in any printed or written material. The scope of PE1 includes everything that is covered up to and including Repetition Statements. You are not allowed to use recursion, arrays or string functions from string.h. See marking scheme, PC number, plab account, computer lab number
#Lab 2 Do Split similar code into functions so that you Don’t Repeat Yourself Initialise when necessary Have clear logic Add comments at important, not trivial areas of code Must do Double/triple check your indentation (gg=G in vim will auto-indent your program nicely!) Add function description, preconditions and postconditions (if any) Do not Add function description elsewhere other than on the top of the function Use arrays or recursion in the labs for now http://www.comp.nus.edu.sg/~cs1010/labs/2017s1/labguide.html Follow lab guidelines
THE ARRAY data structure Lecture Summary int c[10]; a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] A series of “containers” that stores data of the same type Has a name and is allocated memory. “name” points to address of first container Each container has an index (starting index is 0 or 1) Has a fixed size (?) Variable-length arrays not supported by ANSI C90. Can be initialised at time of declaration int cand[NUM_CANDIDATES] = { 0 }; #define M 5 #define N 10 double foo[M*N+8]; // size of foo is 58
Do you know how to… int sumArray(int arr[], int size) { ... } Lecture Summary Do you know how to… Declare and initialise array Access array elements using index Copy array data into another array Create a function with array as parameter arr[] Pass an array to a function as an argument When an array is passed to a function, a pointer to the address of first element of the array is copied into the function. NOT THE ARRAY DATA Write I/O functions for array int sumArray(int arr[], int size) { ... }
while ( condition ) { // loop body } Do you know how do_while works? What if n is negative? Lecture Summary // Precond: n > 0 int add_digits(int n) { int sum = 0; return sum; } while ( condition ) { // loop body } do { sum = sum + n%10; n = n/10; } while (n > 0);
Will this work? double one_seventh = 1.0/7.0; double f = 0.0; do { Lecture Summary double one_seventh = 1.0/7.0; double f = 0.0; while (f != 1.0) { printf("%f\n", f); f += one_seventh; } do { sum = sum + n%10; n = n/10; } while (n > 0);
Seed is a value used to start a sequence of random numbers Lecture Summary Seed is a value used to start a sequence of random numbers A seed always yields identical sequence of random numbers
Tutorial 4 Q3 Moral of the story
Tutorial 4 Q4 All non-negative = NOT(there exists one negative) There exists one negative = NOT(all non-negative)
Tutorial 4 Q6 Why time(NULL)? What is %(high-low+1)?
Tutorial 4 Q6
PE is soon, recess week is soon Summary Repetition Arrays Random numbers #lab2 PE is soon, recess week is soon