COMP 102 Programming Fundamentals I - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

COMP 102 Programming Fundamentals I

Description:

COMP 102 Lab 09. 1. COMP 102. Programming Fundamentals I. Presented by : Timture Choi ... for (int row=0; row total_row; row ) for (int col = 0; col total_col; col ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 10
Provided by: typey73
Category:

less

Transcript and Presenter's Notes

Title: COMP 102 Programming Fundamentals I


1
COMP 102Programming Fundamentals I
  • Presented by Timture Choi

2
2-D Array
  • E.g.
  • // declare a 2-D array of 30 uninitialized
    integers
  • int table310
  • 3 rows
  • 10 columns

3
2-D Array
  • Accessing 2-D array
  • Syntax
  • arrayrowcolumn
  • E.g.
  • table12 5
  • x table12

4
2-D Array Initialization
  • Two methods to initialize a 2-D array
  • int table36 1,2,3,4,5,6,7,8,9,10,11,12,13,1
    4,15,16,17,18
  • int table36
  • 1,2,3,4,5,6,
  • 7,8,9,10,11,12,
  • 13,14,15,16,17,18

5
2-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 row0
rowlttotal_row row) for (int col 0
collttotal_col col) // assign value to an
element of array tablerowcol
rowcol // assign value of an element
to a variable temp tablerowcol
6
Mathematical Functions
  • include ltmathgt
  • 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

7
Mathematical Functions
  • double ceil(double x)
  • Smallest integer not less than x
  • E.g.
  • ceil(1.1) gt 2
  • ceil(-1.9) gt -1
  • double floor(double x)
  • Largest integer not greater than x
  • E.g.
  • floor(1.9) gt 1
  • floor(-1.1) gt -2
  • ltmath.hgt will not provide the round-off function

double d int i int i (int) (dgt0 ? d0.5
d-0.5)
8
Passing Arrays as Parameters
  • 1-D
  • int func(int arraysize, int size)
  • int func(const int arraysize, int size)
  • 2-D
  • int func(int arrayrowcol, int row, int col)
  • int func(const int arrayrowcol, 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

9
SUMMARY
  • By the end of this lab, you should be able to
  • Declare and manipulate
  • 2-D arrays
  • Use mathematical functions provide by
  • ltmathgt
  • Pass an array to a function
  • Normally
  • With const keyword
Write a Comment
User Comments (0)
About PowerShow.com