1 More on Arrays. 2 Review: Passing Arrays to Functions Passing an array: what goes on in the memory? Why isn’t it the same as when passing primitives?

Slides:



Advertisements
Similar presentations
Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
Advertisements

The debugger Some programs may compile correctly, yet not produce the desirable results. These programs are valid and correct C programs, yet not the programs.
COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Maths for Computer Graphics
Lecture Roger Sutton CO331 Visual programming 15: Debugging 1.
Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.
1 ICS103 Programming in C Lecture 16: 2-Dimensional Arrays.
1 Gentle Introduction to Programming Session 4: Arrays, Sorting, Efficiency.
 Monday, 9/9/02, Slide #1 CS106 Introduction to CS1 Monday, 9/9/02  QUESTIONS?? (Exercises, Lab #1, etc.)  Today:  More on float objects  C++ concept:
The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
Guidelines for working with Microsoft Visual Studio.Net.
1 Lab Session-VII CSIT-121 Fall Revising Previous Lab and performing ASCII chart printing experiment (DEMO) 4 Visual Studio Features 4 The While.
1 Gentle Introduction to Programming Session 3: Recursion.
Guidelines for working with Microsoft Visual Studio 6.
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
1 Gentle Introduction to Programming Session 4: Arrays.
Programming Arrays. Question Write a program that reads 3 numbers from the user and print them in ascending order. How many variables do we need to store.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
Programming Arrays. Example 1 Write a program that reads 3 numbers from the user and print them in reverse order. How many variables do we need to store.
Debugging Projects Using C++.NET Click with the mouse button to control the flow of the presentation.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Computer programming Lecture 5. Lecture 5: Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character arrays.
Our Environment We will exercise on Microsoft Visual C++ v.6 We will exercise on Microsoft Visual C++ v.6 because that is what we have in the univ. because.
Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
Cosc175/testing1 Testing Software errors are costly GIGO Preventing Errors –Echo checking –Range and limit checking –Defensive programming.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Multi-Dimensional Arrays Arrays that have more than one index: Example of differences between basic data types and arrays using integers: Basic integer:
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
1 C Programming Week 2 Variables, flow control and the Debugger.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
1 Debugging and Syntax Errors in C++. 2 Debugging – a process of finding and fixing bugs (errors or mistakes) in a computer program.
WEEK 6 Class Activities Lecturer’s slides.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Chapter 8: Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 3 – Inventory Application: Introducing Variables,
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Georgia Institute of Technology Creating Classes part 2 Barb Ericson Georgia Institute of Technology June 2006.
Computer programming Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character.
Structured Programming Approach Module VIII - Additional C Data Types Arrays Prof: Muhammed Salman Shamsi.
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
Example 7 Multiplying with Technology Chapter 7.3 Use technology to compute BA and AB if and.  2009 PBLPathways.
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
Data Structure and Algorithm: CIT231 Lecture 3: Arrays and ADT DeSiaMore DeSiaMorewww.desiamore.com/ifm1.
8. ARRAYS. Aggregate variables Scalar variables –Single value Aggregate variables –Collection of values –Arrays: elements have the same type.
Exercise 1: Maximum element for the following code a- what is the basic operation? b- what is C(n)? d-what is the running time?
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
1 Two-Dimensional Arrays. 2 Terminology Two-dimensional arrays represent matrices A matrix contains a number of values of the same data type The values.
Arrays in C. What is Array? The variables we have used so far can store a single value. Array is a new type of variable capable of storing many values.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Programming application CC213
Testing and Debugging.
Computer Programming I
Week 8 - Programming II Today – more features: Loop control
Lecture 10 Arrays.
Week 2 Variables, flow control and the Debugger
Multidimensional array
Lets Play with arrays Singh Tripty
EECE.2160 ECE Application Programming
Applied Discrete Mathematics Week 4: Functions
Presentation transcript:

1 More on Arrays

2 Review: Passing Arrays to Functions Passing an array: what goes on in the memory? Why isn’t it the same as when passing primitives? What about passing an element from an array (f(arr[i]) is the same as f(x)!2)

3 Efficient Factorial Write a program that repeatedly receives a natural number as its input and returns its factorial The program should be efficient: Do not calculate 1*2*…*n for every n from the beginning on every input Do not calculate values that were not requested

4 Solution: Main Idea We can keep in an array the results When given a new n If calculated before – return it Otherwise, get a better start

5 Solution (efficient_factorial.c) #include #define MAX_N 10 int factorial (int array[], int pos, int n); int main() { int n, position = 0; int fact[MAX_N] = {0}; fact[0] = 1; while(1) { printf("Enter n\n"); scanf("%d",&n); if (n < 0) {break;} if (n >= MAX_N) {printf("too large!"); continue;} printf("fact(%d) = %d\n",n,factorial(fact,position,n)); if (n > position) position = n; }

6 Solution (Cont.) int factorial (int fact[], int pos, int n) { int i; if (pos < n) { for(i = pos + 1; i <= n; ++i) { fact[i] = fact[i-1] *i; } return fact[n]; }

7 Exercise Implement a function that accepts two integer arrays and returns 1 if they are equal, 0 otherwise The arrays are of the same size Write a program that accepts two arrays of integers from the user and checks for equality

8 Solution (compare_arrays.c) int compare_arrays(int arr1[], int arr2[], int size) { int i = 0; /* compare the elements one at a time */ for (i = 0; i < size; ++i) { if (arr1[i] != arr2[i]) return 0; } /* if we got here, both arrays are identical */ return 1; }

9 Multi-dimensional arrays Array of arrays: int A[2][3] = { {1, 2, 3}, {4, 5, 6} }; Means an array of 2 integer arrays, each of length 3. Access: j-th element of the i-array is A[i][j]

10 Multi-dimensional arrays The size of the array can be determined by the compiler (not recommended): int B[][2] = {{1,2}, {2,3}, {3,4}}; Cannot skip this!!

11 Example: matrix addition #include #define SIZE 3 int main() { int A[SIZE][SIZE] = {{1,2,3}, {4,5,6}, {7,8,9}}; int B[SIZE][SIZE] = {{1,1,1}, {2,2,2}, {3,3,3}}; int C[SIZE][SIZE]; int i = 0, j = 0; for (i = 0; i < SIZE; ++i) for (j = 0; j < SIZE; ++j) C[i][j] = A[i][j] + B[i][j]; return 0; }

12 2D arrays as function arguments void print_matrix(int mat[3][3]) { int i, j; for (i = 0; i < 3; ++i) { for (j = 0; j < 3; ++j) printf("\t%d ", mat[i][j]); printf("\n"); } The second subscript must be specified and it must be constant

13 Exercise Implement a function that accepts two integer matrices and returns 1 if they are equal, 0 otherwise The matrices are of the same size Write a program that defines and initialize 2 matrices of size 3x3 with integers You can use compare_arrays as your starting point

14 Solution (compare_matrices.c) int compare_matrices(int mat1[][SIZE], int mat2[][SIZE], int rows) { int i,j; for(i=0; i<rows; i++) for (j=0; j < SIZE; j++) if (mat1[i][j]!=mat2[i][j]) return 0; /* If we got here, both matrices are identical */ return 1; }

15 home Write a program that defines 3 matrices A,B,C of size 3x3 with float elements; initialize the first two matrices (A and B) Compute the matrix multiplication of A and B and store it in C (i.e. C = A*B) Matrix Multiplication: Print all the matrices on the screen

16 Solution mat_mul.c

17 The debugger Some programs may compile correctly, yet not produce the desirable results. These programs are valid and correct C programs, yet not the programs we meant to write! The debugger can be used to follow the program step by step and may help detecting bugs in an already compiled program.

18 The debugger’s common features Setting breakpoints (a point where the execution stops): bring the cursor to desired line and press the palm icon or F9. A dark red dot appears near the line. Executing a debugged run: Build->start debug->go or F5. The program will run and stop at the first breakpoint.

19 The debugger’s common features (cont.) Stopping at a specific line: Bringing the cursor to the line and press ctrl+F10, or Build->start debug->go to cursor. The program will stop at that point. Stepping to the next line – F10. Entering a function – F11. Seeing variable values – quickwatch and/or debug window at the bottom. The yellow arrow indicates our whereabouts at any given moment.

20 Examples factorial_func.c getchar.c

21 Buggy example pi_bad.c

22 More time? Talk about the loops-related questions from 2 weeks ago