CS1010 Discussion Group 11 Week 6 – One dimensional arrays.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Week 6.  Lab 1 and 2 results  Common mistakes in Style  Lab 1 common mistakes in Design  Lab 2 common mistakes in Design  Tips on PE preparation.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
CS1010: Programming Methodology
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)
CS1010 Discussion Group 11 Week 5 – Functions, Selection, Repetition.
CIS199 Test Review 2 REACH.
Some Assignments  Write a program which prints the following information about at least 5 persons: NAME MAIL-ID EMPLOYEE-CODE PHONE Eg. Umesh
Computer Organization and Design Pointers, Arrays and Strings in C
ECE Application Programming
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
CS1010 Discussion Group 11 Week 3 - Computational Thinking/Algorithms.
4. Java language basics: Function
User-Written Functions
EECS 183 Discussion #9: It’s time to “Git” Python! November 22nd, 2016
EGR 2261 Unit 10 Two-dimensional Arrays
Chapter 5: Control Structures II (Repetition)
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Computer Programming BCT 1113
Repetition (While-Loop) version]
CS1010 Discussion Group 11 Week 4 – Overview of C programming.
C Programming Tutorial – Part I
Tutorial 8 Pointers and Strings
CS1010 Discussion Group 11 Week 9 – Pointers.
Chapter 5: Control Structures II
CS1010 Programming Methodology
CS1010 Discussion Group 11 Week 7 – Two dimensional arrays.
Functions CIS 40 – Introduction to Programming in Python
Arrays in C.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Announcements Final Exam on August 17th Wednesday at 16:00.
C Passing arrays to a Function
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays & Functions Lesson xx
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
CS 2308 Exam I Review.
One-Dimensional Array Introduction Lesson xx
CS 2308 Exam I Review.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Number and String Operations
CS 2308 Exam I Review.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Chapter 6: Repetition Statements
Topics discussed in this section:
CS2011 Introduction to Programming I Arrays (I)
Pointers.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Computing Fundamentals
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Chapter 5: Control Structures II (Repetition)
Functions continued.
The structure of programming
SE1H421 Procedural Programming LECTURE 2 Variables (1)
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
EECE.2160 ECE Application Programming
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Presentation transcript:

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