CSI 121 Structure Programming Language Lecture 18 Arrays (Part 2)

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

1 Introduction to Computing Lecture 16 Arrays (Part 2) Dr. Bekir KARLIK Yasar University Department of Computer Engineering
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.
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)
1 CSE1301 Computer Programming: Lecture 9 Input/Output.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)
1 CSE1301 Computer Programming Lecture 18 Arrays (Part 1)
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1) Linda M c Iver.
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.
FUNCTION Dong-Chul Kim BioMeCIS UTA 12/7/
1 CSE1301 Computer Programming Lecture 13 Functions (Part 1)
Khalid Rasheed Shaikh Computer Programming Theory 1.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
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.
CSE1301 Computer Programming: Lecture 6 Input/Output.
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
1 CSE1301 Computer Programming Lecture 18 Arrays (Part 2)
Lecture 3: Getting Started & Input / Output (I/O)
Computer Science 210 Computer Organization
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Functions, Part 2 of 2 Topics Functions That Return a Value
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Functions Dr. Sajib Datta
CSI 121 Structure Programming Language Lecture 17 Arrays (Part 1)
Functions in C Mrs. Chitra M. Gaikwad.
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
CSI-121 Structured Programming Language Lecture 16 Pointers
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
CSI 121 Structure Programming Language Lecture 10: Iteration (Part 1)
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Lecture 9 : Array Acknowledgment : Lecture notes from Ohio Supercomputing Center.
CS1100 Computational Engineering
CSI 121 Structured Programming Language Lecture 7: Input/Output
Multidimensional Arrays
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
CSI 121 Structured Programming Language Lecture 5: C Primitives 2
Lecture 10 Arrays.
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
CSI 121 Structured Programming Language Lecture 13 Functions (Part 1)
EKT150 : Computer Programming
Arrays Outline Introduction Arrays Declaring Arrays
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Functions, Part 2 of 3 Topics Functions That Return a Value
The while Looping Structure
Introduction to Computer Organization & Systems
Computer Programming Lecture 14 Arrays (Part 2)
Introduction to Computing Lecture 10 Arrays (Part 1)
More Loops Topics Counter-Controlled (Definite) Repetition
Character Arrays char string1[] = “first”;
Introduction to C EECS May 2019.
EECE.2160 ECE Application Programming
More Loops Topics Counter-Controlled (Definite) Repetition
EECE.2160 ECE Application Programming
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Dale Roberts, Lecturer IUPUI
EECE.2160 ECE Application Programming
Functions, Part 2 of 3 Topics Functions That Return a Value
The while Looping Structure
EECE.2160 ECE Application Programming
Functions, Part 2 of 3 Topics Functions That Return a Value
More Loops Topics Counter-Controlled (Definite) Repetition
EECE.2160 ECE Application Programming
CHAPTER 6 Testing and Debugging.
Visit for more Learning Resources
Presentation transcript:

CSI 121 Structure Programming Language Lecture 18 Arrays (Part 2) CSE1301 Sem 2 -- 2003 CSI 121 Structure Programming Language Lecture 18 Arrays (Part 2) Lecture 19: Arrays (Part 2)

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

Two-dimensional Arrays CSE1301 Sem 2 -- 2003 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 Lecture 19: Arrays (Part 2)

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

Example (cont): YearlyRainfall-1 CSE1301 Sem 2 -- 2003 Example (cont): YearlyRainfall-1 #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 Lecture 19: Arrays (Part 2)

Example (cont): YearlyRainfall-2 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-1 CSE1301 Sem 2 -- 2003 Example (cont): YearlyRainfall-1 #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 Lecture 19: Arrays (Part 2)

Example (cont) : YearlyRainfall-1 CSE1301 Sem 2 -- 2003 Example (cont) : YearlyRainfall-1 #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 Lecture 19: Arrays (Part 2)

Example (cont): YearlyRainfall-2 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 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 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

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

Example 2: 2-D Array-1 twod2.c #include <stdio.h> CSE1301 Sem 2 -- 2003 Example 2: 2-D Array-1 #include <stdio.h> #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); } return 0; twod2.c Lecture 19: Arrays (Part 2)

Example 2 (cont): 2-D Array-1 CSE1301 Sem 2 -- 2003 Example 2 (cont): 2-D Array-1 #include <stdio.h> #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); } return 0; twod2.c Lecture 19: Arrays (Part 2)

Example 2 (cont): 2-D Array-2 CSE1301 Sem 2 -- 2003 Example 2 (cont): 2-D Array-2 /* 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 Lecture 19: Arrays (Part 2)

Example 2 (cont): 2-D Array-2 CSE1301 Sem 2 -- 2003 Example 2 (cont): 2-D Array-2 /* 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 Lecture 19: Arrays (Part 2)

Example 2 (cont): 2-D Array-2 /* 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 /* 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-3 /* 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 CSE1301 Sem 2 -- 2003 Example 2 (cont): 2-D Array-3 /* 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 Lecture 19: Arrays (Part 2)

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

Compiler Problems with Floats (cont) Explanation: From Section Q:03.04 in http://www.faqs.org/faqs/msdos-programmer-faq/part2/ "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.

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); }

Reading King Chapter 8, Chapter 12 (12.2-12.4) Deitel and Deitel CSE1301 Sem 2 -- 2003 Reading King Chapter 8, Chapter 12 (12.2-12.4) Deitel and Deitel Chapter 6 (6.5 to 6.9) Lecture 19: Arrays (Part 2)