1 CSE1301 Computer Programming Lecture 18 Arrays (Part 2)

Slides:



Advertisements
Similar presentations
1 Introduction to Computing Lecture 16 Arrays (Part 2) Dr. Bekir KARLIK Yasar University Department of Computer Engineering
Advertisements

1 Introduction to Computing Lecture 15 Arrays (Part 1) Dr. Bekir KARLIK Yasar University Department of Computer Engineering
CSE1301 Computer Programming Lecture 4: C Primitives I.
CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)
1 Array Knowledge Understand the execute technique of array Skill Can write application program using one and two dimensional array.
1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
1 CSE1301 Computer Programming Lecture 19 Arrays (Part 2)
R-1 University of Washington Computer Programming I Lecture 17: Multidimensional Arrays © 2000 UW CSE.
1 CSE1301 Computer Programming Lecture 27 Recursion (Part 1)
1 CSE1301 Computer Programming Lecture 18 Arrays (Part 1)
Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Introduction to Computing Lecture 07: Repetition and Loop Statements (Part.
Crypto Project Sample Encrypted Data: –Turing: CS Public\CryptoProjectData Crypto_Short_Keys_P_U.out Crypto_Long_Keys_O_R.out Bonus +10 points on.
Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.
Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify –Array name –Position number Format: arrayname.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Lecture 16: Working with Complex Data Arrays. Double-Subscripted Arrays Commonly used to represent tables of values consisting of information arranged.
Multi-Dimensional Arrays Arrays that have more than one index: Example of differences between basic data types and arrays using integers: Basic integer:
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers, Arrays, Multidimensional Arrays Pointers versus arrays – Lots of similarities How to deal with.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 3.
FUNCTION Dong-Chul Kim BioMeCIS UTA 12/7/
1 CSE1301 Computer Programming Lecture 13 Functions (Part 1)
Computer Programming Lecture 8 Arrays. 2 switch-statement Example (3) If use press left arrowIf use press right arrow If use press up arrow If use press.
WEEK 6 Class Activities Lecturer’s slides.
How to design and code functions Chapter 4 (ctd).
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Arrays and Strings Lecture 30. Summary of Previous Lecture In the previous lecture we have covered  Functions Prototypes Variable Scope  Pointers Introduction.
C Lecture Notes 1 Arrays (...cont.). C Lecture Notes 2 6.6Sorting Arrays Sorting data –Important computing application –Virtually every organization must.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A3 – Basic Data Structures (Stacks)
Two-Dimensional Arrays
ECE Application Programming
ECE Application Programming
CSI 121 Structure Programming Language Lecture 18 Arrays (Part 2)
CSI 121 Structure Programming Language Lecture 17 Arrays (Part 1)
Functions in C Mrs. Chitra M. Gaikwad.
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
CSI 121 Structure Programming Language Lecture 10: Iteration (Part 1)
Lecture 9 : Array Acknowledgment : Lecture notes from Ohio Supercomputing Center.
CSI 121 Structured Programming Language Lecture 7: Input/Output
Multidimensional Arrays
Lecture 10 Arrays.
CSI 121 Structured Programming Language Lecture 13 Functions (Part 1)
C++ Programming Lecture 16 Arrays – Part III
Functions, Part 2 of 3 Topics Functions That Return a Value
Multidimensional Arrays
Multidimensional array
Computer Programming Lecture 14 Arrays (Part 2)
Introduction to Computing Lecture 10 Arrays (Part 1)
CHAPTER 2 Arrays and Vectors.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
CHAPTER 2 Arrays and Vectors.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Dale Roberts, Lecturer IUPUI
EECE.2160 ECE Application Programming
Functions, Part 2 of 3 Topics Functions That Return a Value
EECE.2160 ECE Application Programming
Functions, Part 2 of 3 Topics Functions That Return a Value
EECE.2160 ECE Application Programming
Chapter 6 - Arrays Outline Multiple-Subscripted Arrays.
Visit for more Learning Resources
Presentation transcript:

1 CSE1301 Computer Programming Lecture 18 Arrays (Part 2)

2 Topics Two-dimensional arrays Passing two-dimensional arrays to functions

3 Two-dimensional Arrays Each element of an array is like a single item of a particular type But an array itself is an item of a particular type So, an array element could be another array An “array-of-arrays” is called “multi- dimensional” because you need to specify several ordinates to locate an actual element

4 Example: YearlyRainfall Problem: using the Yearly Rainfall table input month and year output mean rainfall for that month and year year month Average Yearly Rainfall (in mm)

5 #define NYEARS 5 #define NMONTHS 12 int lookup(int year, int month) { int table[NYEARS][NMONTHS] = { {30,40,75,95,130,220,210,185,135,80,40,45}, {25,25,80,75,115,270,200,165, 85, 5,10, 0}, {35,45,90,80,100,205,135,140,170,75,60,95}, {30,40,70,70, 90,180,180,210,145,35,85,80}, {30,35,30,90,150,230,305,295, 60,95,80,30} }; if ((0 <= year) && (year < NYEARS) && (0 <= month) && (month < NMONTHS)) { return table[year][month]; } else { return -1; } yearly1.c Example (cont): YearlyRainfall-1

6 int main() { int year; int month; int rainfall; printf("Enter year and month: "); scanf("%d %d", &year, &month); rainfall = lookup(year - 1, month - 1); if (rainfall < 0) { printf("Year must be between 1 and %d,\n", NYEARS); printf("and month must be between 1 and %d.\n", NMONTHS); } else { printf("Rainfall for year %d, month %d is %d mm.\n", year, month, rainfall); } return 0; } yearly1.c Example (cont): YearlyRainfall-2

7 #define NYEARS 5 #define NMONTHS 12 int lookup(int year, int month) { int table[NYEARS][NMONTHS] = { {30,40,75,95,130,220,210,185,135,80,40,45}, {25,25,80,75,115,270,200,165, 85, 5,10, 0}, {35,45,90,80,100,205,135,140,170,75,60,95}, {30,40,70,70, 90,180,180,210,145,35,85,80}, {30,35,30,90,150,230,305,295, 60,95,80,30} }; if ((0 <= year) && (year < NYEARS) && (0 <= month) && (month < NMONTHS)) { return table[year][month]; } else { return -1; } yearly1.c Example (cont): YearlyRainfall-1

8 #define NYEARS 5 #define NMONTHS 12 int lookup(int year, int month) { int table[NYEARS][NMONTHS] = { {30,40,75,95,130,220,210,185,135,80,40,45}, {25,25,80,75,115,270,200,165, 85, 5,10, 0}, {35,45,90,80,100,205,135,140,170,75,60,95}, {30,40,70,70, 90,180,180,210,145,35,85,80}, {30,35,30,90,150,230,305,295, 60,95,80,30} }; if ((0 <= year) && (year < NYEARS) && (0 <= month) && (month < NMONTHS)) { return table[year][month]; } else { return -1; } yearly1.c Example (cont) : YearlyRainfall-1

9 int main() { int year; int month; int rainfall; printf("Enter year and month: "); scanf("%d %d", &year, &month); rainfall = lookup(year - 1, month - 1); if (rainfall < 0) { printf("Year must be between 1 and %d,\n", NYEARS); printf("and month must be between 1 and %d.\n", NMONTHS); } else { printf("Rainfall for year %d, month %d is %d mm.\n", year, month, rainfall); } return 0; } yearly1.c Example (cont): YearlyRainfall-2

10 int main() { int year; int month; int rainfall; printf("Enter year and month: "); scanf("%d %d", &year, &month); rainfall = lookup(year - 1, month - 1); if (rainfall < 0) { printf("Year must be between 1 and %d,\n", NYEARS); printf("and month must be between 1 and %d.\n", NMONTHS); } else { printf("Rainfall for year %d, month %d is %d mm.\n", year, month, rainfall); } return 0; } yearly1.c Example (cont): YearlyRainfall-2

11 int main() { int year; int month; int rainfall; printf("Enter year and month: "); scanf("%d %d", &year, &month); rainfall = lookup(year - 1, month - 1); if (rainfall < 0) { printf("Year must be between 1 and %d,\n", NYEARS); printf("and month must be between 1 and %d.\n", NMONTHS); } else { printf("Rainfall for year %d, month %d is %d mm.\n", year, month, rainfall); } return 0; } yearly1.c Example (cont): YearlyRainfall-2

12 Passing Two-Dimensional Arrays to Functions In the function definition, the size of the array needs to be specified Any changes to array elements within the function affect the ``original’’ array elements

13 #include #define NROWS 3 #define NCOLS 5 void inputEntry(float table[][NCOLS]); void printTable(float table[NROWS][NCOLS]); int main() { float table[NROWS][NCOLS] = {{0}}; printTable(table); while (1) { inputEntry(table); printTable(table); } return 0; } twod2.c Example 2: 2-D Array-1

14 #include #define NROWS 3 #define NCOLS 5 void inputEntry(float table[][NCOLS]); void printTable(float table[NROWS][NCOLS]); int main() { float table[NROWS][NCOLS] = {{0}}; printTable(table); while (1) { inputEntry(table); printTable(table); } return 0; } twod2.c Example 2 (cont): 2-D Array-1

15 /* Reads in a location in the table and the value of one item to be put in the table */ void inputEntry ( float table[NROWS][NCOLS] ) { int row, column; printf("Enter row and column number: "); scanf("%d %d", &row, &column); if ((0 <= row && row < NROWS) && (0 <= column && column < NCOLS)) { printf("Enter value: "); scanf("%f", &table[row][column]); } else { printf("Invalid entry location. No change.\n"); } twod2.c Example 2 (cont): 2-D Array-2

16 /* Reads in a location in the table and the value of one item to be put in the table */ void inputEntry ( float table[][NCOLS] ) { int row, column; printf("Enter row and column number: "); scanf("%d %d", &row, &column); if ((0 <= row && row < NROWS) && (0 <= column && column < NCOLS)) { printf("Enter value: "); scanf("%f", &table[row][column]); } else { printf("Invalid entry location. No change.\n"); } twod2.c Example 2 (cont): 2-D Array-2

17 /* Reads in a location in the table and the value of one item to be put in the table */ void inputEntry ( float table[][NCOLS] ) { int row, column; printf("Enter row and column number: "); scanf("%d %d", &row, &column); if ((0 <= row && row < NROWS) && (0 <= column && column < NCOLS)) { printf("Enter value: "); scanf("%f", &table[row][column]); } else { printf("Invalid entry location. No change.\n"); } twod2.c Example 2 (cont): 2-D Array-2

18 /* Reads in a location in the table and the value of one item to be put in the table */ void inputEntry ( float table[][NCOLS] ) { int row, column; printf("Enter row and column number: "); scanf("%d %d", &row, &column); if ((0 <= row && row < NROWS) && (0 <= column && column < NCOLS)) { printf("Enter value: "); scanf("%f", &table[row][column]); } else { printf("Invalid entry location. No change.\n"); } twod2.c Example 2 (cont): 2-D Array-2

19 /* Prints the table page-by-page, and each page row-by-row */ void printTable ( float table[NROWS][NCOLS] ) { int row, column; for (row=0; row < NROWS; row++) { for (column=0; column < NCOLS; column++) { printf("%10.2f", table[row][column]); } printf("\n"); } twod2.c Example 2 (cont): 2-D Array-3

20 /* Prints the table page-by-page, and each page row-by-row */ void printTable ( float table[NROWS][NCOLS] ) { int row, column; for (row=0; row < NROWS; row++) { for (column=0; column < NCOLS; column++) { printf("%10.2f", table[row][column]); } printf("\n"); } twod2.c Example 2 (cont): 2-D Array-3

21 Reading King – Chapter 8, Chapter 12 ( ) Deitel and Deitel – Chapter 6 (6.5 to 6.9)