COMP102 Lab 091 COMP 102 Programming Fundamentals I Presented by : Timture Choi.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
Topic 9C – Multiple Dimension Arrays. CISC105 – Topic 9C Multiple Dimension Arrays A multiple dimension array is an array that has two or more dimensions.
Arrays Programming COMP102 Prog. Fundamentals I: Arrays / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g.,
Math Functions & 2-D Arrays Programming. COMP102 Prog. Fundamentals I: Math Functions & 2D Arrays / Slide 2 Copyright © 2000 by Brooks/Cole Publishing.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Simple Arrays COMP104 Lecture 11 / Slide 2 Arrays * An array is a collection of data elements that are of the same type (e.g., a collection of integers,characters,
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
Introduction to Java Programming, 4E Y. Daniel Liang.
Multiple-Subscripted Array
Chapter 9: Arrays and Strings
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
1 Chapter 5 Methods. 2 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Introduction to Programming with C++ Fourth Edition
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
Topic 2A – Library Functions and Casting. CISC 105 – Topic 2A Functions A function is a piece of code which performs a specific task. When a function.
Simple Arrays Programming COMP104 Lecture 12 / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g., a collection.
1 Topic 04 Methods Programming II/A CMC2522 / CIM2561 Bavy Li.
COMP102 Lab 131 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Chapter 8 Arrays and Strings
1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.
Lecture 5 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Chapter 4 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
Multi-Dimensional Arrays Arrays that have more than one index: Example of differences between basic data types and arrays using integers: Basic integer:
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
COMP102 Lab 021 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
1 Java Library Lecture 9 by Dr. Norazah Yusof. 2 Java Library Java has pre-defined classes that consist of the basic language classes in Java (organized.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
TOPIC 6 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
Chapter 4: Methods Method Structure Method Structure Declaring Methods Declaring Methods Calling Methods Calling Methods Passing Parameters Passing Parameters.
Arrays.
Opening Input/Output Files ifstream infile; ofstream outfile; char inFileName[40]; char outFileName[40]; coutinFileName;
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Arrays Chapter 12. One-Dimensional Arrays If you wanted to read in 1000 ints and print them in reverse order, it would take a program that’s over 3000.
ARRAYS Multidimensional realities Image courtesy of
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
Chapter 5 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Arrays float Scores[9]; ? index: element // one dimensional array 2.
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Computer Programming BCT 1113
Chapter 4 Mathematical Functions, Characters, and Strings
Chapter 3 Methods.
Chapter 5 – Part 2 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 4: Mathematical Functions, Characters, and Strings
Using Free Functions Chapter 3 Computing Fundamentals with C++
The Language Package.
METHODS (FUNCTIONS) By: Lohani Adeeb khan.
Functions.
Chapter 5 Methods.
Chapter 5 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
C++ Array 1.
Chapter 4 Methods Introducing Methods Declaring Methods
Functions in C Math Library Functions Functions Function Definitions
Ch 5 : Mathematical Functions, Characters, and Strings
Presentation transcript:

COMP102 Lab 091 COMP 102 Programming Fundamentals I Presented by : Timture Choi

COMP102 Lab D Array E.g. // declare a 2-D array of 30 uninitialized integers int table[3][10];  3 rows  10 columns

COMP102 Lab D Array Accessing 2-D array Syntax array[row][column] E.g. table[1][2] = 5; x = table[1][2]; -- table

COMP102 Lab D Array Initialization Two methods to initialize a 2-D array int table[3][6] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16, 17,18}; int table[3][6] = {{1,2,3,4,5,6}, {7,8,9,10,11,12}, {13,14,15,16,17,18}}; table

COMP102 Lab D Array Inputting Values Use two for loops to access/process all the elements of a 2-D array E.g. int total_row = 3, total_col = 6; for (int row=0; row<total_row; row++) for (int col = 0; col<total_col; col++) { // assign value to an element of array table[row][col] = row*col; … // assign value of an element to a variable temp = table[row][col]; }

COMP102 Lab 096 Mathematical Functions #include double log(double x)  Natural logarithm double log10(double x)  Base 10 logarithm double exp(double x)  e to the power x double pow(double x, double y)  x to the power y double sqrt(double x)  Square root of x double sin(double x) double cos(double x) double tan(double x)  In radian argument

COMP102 Lab 097 Mathematical Functions double ceil(double x) Smallest integer not less than x E.g.  ceil(1.1) => 2  ceil(-1.9) => -1 double floor(double x) Largest integer not greater than x E.g.  floor(1.9) => 1  floor(-1.1) => -2 will not provide the round-off function double d; int i; … int i = (int) (d>0 ? d+0.5 : d-0.5);

COMP102 Lab 098 Passing Arrays as Parameters 1-D int func(int array[size], int size); int func(const int array[size], int size); 2-D int func(int array[row][col], int row, int col); int func(const int array[row][col], int row, int col); The “ [ ] ” in the formal parameter specification Indicates that the variable is an array It is a good practice to pass the dimension of the array as another parameter Arrays are always passed by reference If the function must not change any element of the array  const should be used

COMP102 Lab 099 SUMMARY By the end of this lab, you should be able to: Declare and manipulate  2-D arrays Use mathematical functions provide by  Pass an array to a function  Normally  With const keyword