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

Slides:



Advertisements
Similar presentations
1 Introduction to Computing: Lecture 16 Character Strings 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
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)
Guidelines for working with Microsoft Visual Studio.Net.
R-1 University of Washington Computer Programming I Lecture 17: Multidimensional Arrays © 2000 UW CSE.
1 CSE1301 Computer Programming Lecture 18 Arrays (Part 1)
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Computer Science 210 Computer Organization Introduction to C.
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.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1) Linda M c Iver.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify.
C Lecture Notes 1 Arrays Lecture 6. C Lecture Notes 2 6.1Introduction Arrays –Structures of related data items –Static entity – same size throughout program.
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
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:
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers, Arrays, Multidimensional Arrays Pointers versus arrays – Lots of similarities How to deal with.
Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 6 Array and String.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
FUNCTION Dong-Chul Kim BioMeCIS UTA 12/7/
Khalid Rasheed Shaikh Computer Programming Theory 1.
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.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Arrays and Strings Lecture 30. Summary of Previous Lecture In the previous lecture we have covered  Functions Prototypes Variable Scope  Pointers Introduction.
Computer programming Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
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.
1D Arrays and Random Numbers Artem A. Lenskiy, PhD May 26, 2014.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Arrays.
Computer Programming for Engineers
1. 2 Introduction Structure Definitions and Declarations Initializing Structures Operations on Structures Members Structures as Functions Parameters Array.
Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f"
Introduction to Computing Lecture 12 Pointers Dr. Bekir KARLIK Yasar University Department of Computer Engineering
8. ARRAYS. Aggregate variables Scalar variables –Single value Aggregate variables –Collection of values –Arrays: elements have the same type.
C++ Programming Lecture 16 Arrays – Part III The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr.
1 CSE1301 Computer Programming Lecture 18 Arrays (Part 2)
Lecture 3: Getting Started & Input / Output (I/O)
CSI 121 Structure Programming Language Lecture 18 Arrays (Part 2)
Functions Dr. Sajib Datta
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
Computer Programming Lecture 15 Text File I/O
CSI 121 Structure Programming Language Lecture 10: Iteration (Part 1)
Multidimensional Arrays
Lecture 10 Arrays.
Functions, Part 2 of 3 Topics Functions That Return a Value
Computer Programming Lecture 14 Arrays (Part 2)
Introduction to Computing Lecture 10 Arrays (Part 1)
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
Presentation transcript:

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

2 Topics Multi-dimensional arrays Passing multi-dimensional arrays to functions

3 Multi-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” (or “multi-subscripted”) because you need to specify more than one ordinate 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 Page 4 Page 3 Page 2 Example: 3-D Array Page 1

13 Example 1: 3-D Array #include #define NROWS 3 #define NCOLS 5 #define NPAGES 2 int main() { float ledger[NPAGES][NROWS][NCOLS]; int page, row, column; /* Fill in the multi-d array with values */ for (page=0; page < NPAGES; page++) { for (row=0; row < NROWS; row++) { for (column=0; column < NCOLS; column++) { ledger[page][row][column]) = page + (row / 10.0) + (column / 100.0); } /* Print out the values in the multi-d array */ for (page=0; page < NPAGES; page++) { for (row=0; row < NROWS; row++) { for (column=0; column < NCOLS; column++) { printf("%10.2f", ledger[page][row][column]); } printf("\n"); } printf("\n"); } return 0; } multid1.c

14 Example 1 (cont): 3-D Array-1 #include #define NROWS 3 #define NCOLS 5 #define NPAGES 2 int main() { float ledger[NPAGES][NROWS][NCOLS]; int page, row, column; /* Fill in the multi-d array with values */ for (page=0; page < NPAGES; page++) { for (row=0; row < NROWS; row++) { for (column=0; column < NCOLS; column++) { ledger[page][row][column]) = page + (row / 10.0) + (column / 100.0); } multid1.c

15 /* Print out the values in the multi-d array */ for (page=0; page < NPAGES; page++) { for (row=0; row < NROWS; row++) { for (column=0; column < NCOLS; column++) { printf("%10.2f", ledger[page][row][column]); } printf("\n"); } printf("\n"); } return 0; } multid1.c Example 1 (cont): 3-D Array-2

16 Passing Multi-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

17 #include #define NROWS 3 #define NCOLS 5 #define NPAGES 2 void inputEntry(float ledger[][NROWS][NCOLS]); void printTables(float ledger[NPAGES][NROWS][NCOLS]); int main() { float ledger[NPAGES][NROWS][NCOLS] = {{{0}}}; printTables(ledger); while (1) { inputEntry(ledger); printTables(ledger); } return 0; } multid2.c Example 2: 3-D Array-1

18 #include #define NROWS 3 #define NCOLS 5 #define NPAGES 2 void inputEntry(float ledger[][NROWS][NCOLS]); void printTables(float ledger[NPAGES][NROWS][NCOLS]); int main() { float ledger[NPAGES][NROWS][NCOLS] = {{{0}}}; printTables(ledger); while (1) { inputEntry(ledger); printTables(ledger); } return 0; } multid2.c Example 2 (cont): 3-D Array-1

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

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

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

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

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

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

25 Compiler Problems with Floats Problem: You may encounter the following run-time error message with Borland C/C++ and Turbo C/C++: scanf : floating point formats not linked Abnormal program termination

26 Compiler Problems with Floats (cont) "floating point formats not linked" is a Borland run-time error (Borland C or C++, Turbo C or C++). Borland's compilers try to be smart and not link in the floating-point (f-p) library unless you need it. Alas, they all get the decision wrong. One common case is where you don't call any f-p functions, but you have %f or other f-p formats in scanf() or printf() calls. The cure is to call an f-p function, or at least force one to be present in the link. Explanation: From Section Q:03.04 in

27 Compiler Problems with Floats (cont) Solution: Define the following function somewhere in your source file, but do not call it: void dummyFunction ( void ) { float dummyFloat; scanf("%f", &dummyFloat); printf("%f", dummyFloat); }