Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Programming Lecture 14 Arrays (Part 2)

Similar presentations


Presentation on theme: "Computer Programming Lecture 14 Arrays (Part 2)"— Presentation transcript:

1 Computer Programming Lecture 14 Arrays (Part 2)
Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering

2 Topics The relationship between pointers and arrays
Multi-dimensional arrays Passing multi-dimensional arrays to functions

3 The relationship between pointers and arrays
Arrays and pointers are intimately related in C and may be used almost interchangeably An array name can be thought as a constant pointer Pointers can be used to do any operation involving array subscripting

4 The relationship between pointers and arrays
Assume an integer array b[5] and integer pointer bPtr The following expressions are equivalent bPtr = b; ≡ bPtr = &b[0]; b[3] ≡ *(bPtr + 3) &b[3] ≡ bPtr + 3 b[3] ≡ *(b + 3) b[3] ≡ bPtr[3]

5 The relationship between pointers and arrays
Good programming practice Use array notation instead of pointer notation

6 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

7 Multidimensional Array Declaration
data_type arrayname[size1][size2]…[sizen]; Examples double table[NROWS][NCOLS]; int array[7][5][6];

8 Initialization of Multidimensional Arrays
You can initialize multidimensional arrays like one-dimensional array However, instead of listing all table values in one list, the values are usually grouped by rows Example int table[3][3] = { {1,2,3},{4,5,6},{7,8,9} };

9 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

10 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

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

12 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

13 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

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

15 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

16 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

17 Example: 3-D Array Page 4 Page 3 Page 2 Page 1

18 Example 1: 3-D Array - 1 multid1.c #include <stdio.h>
#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 */ { printf("%10.2f", ledger[page][row][column]); printf("\n"); return 0; multid1.c

19 Example 1 (cont): 3-D Array-1
#include <stdio.h> #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

20 Example 1 (cont): 3-D Array-1
/* 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"); return 0; multid1.c

21 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

22 Example 2: 3-D Array-2 multid2.c #include <stdio.h>
#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); } return 0; multid2.c

23 Example 2 (cont): 3-D Array-2
#include <stdio.h> #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); } return 0; multid2.c

24 Example 2 (cont): 3-D Array-2
/* 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

25 Example 2 (cont): 3-D Array-2
/* 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

26 Example 2 (cont): 3-D Array-2
/* 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

27 Example 2 (cont): 3-D Array-2
/* 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

28 Example 2 (cont): 3-D Array-2
/* 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"); multid2.c

29 Example 2 (cont): 3-D Array-2
/* 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"); multid2.c

30 Notes ANSI C requires that an implementation allow multidimensional arrays of at least six dimensions

31 fig06_07.c (Part 1 of 2)

32 fig06_07.c (Part 2 of 2) Program Output
Rating Frequency

33 fig06_08.c (Part 1 of 2)

34 fig06_08.c (Part 2 of 2) Program Output
Element Value Histogram ******************* *** *************** ******* *********** ********* ************* ***** ***************** * fig06_08.c (Part 2 of 2) Program Output

35 fig06_10.c (Part 1 of 2)

36 fig06_10.c (Part 2 of 2) Enter a string: Hello there string1 is: Hello
string2 is: string literal string1 with spaces between characters is: H e l l o fig06_10.c (Part 2 of 2)

37 fig06_15.c (Part 1 of 3)

38 fig06_15.c (Part 2 of 3)

39 fig06_15.c (Part 3 of 3) Program Output
Data items in original order Data items in ascending order fig06_15.c (Part 3 of 3) Program Output

40 fig06_16.c (Part 1 of 8)

41 fig06_16.c (Part 2 of 8)

42 fig06_16.c (Part 3 of 8)

43 fig06_16.c (Part 4 of 8)

44 fig06_16.c (Part 5 of 8)

45 fig06_16.c (Part 6 of 8)

46 fig06_16.c (Part 7 of 8)

47 fig06_16.c (Part 8 of 8)

48 Program Output ******** Mean The mean is the average value of the data
items. The mean is equal to the total of all the data items divided by the number of data items ( 99 ). The mean value for this run is: 681 / 99 = Median The unsorted array of responses is The sorted array is Program Output

49 Program Output (continued)
The median is element 49 of the sorted 99 element array. For this run the median is 7 ******** Mode Response Frequency Histogram * *** **** ***** ******** ********* *********************** *************************** ******************* The mode is the most frequent value. For this run the mode is 8 which occurred 27 times.

50 fig06_18.c (Part 1 of 3)

51 fig06_18.c (Part 2 of 3)

52 fig06_18.c (Part 3 of 3) Program Output
Enter integer search key: 36 Found value in element 18 37 Value not found

53 fig06_19.c (Part 1 of 5)

54 fig06_19.c (Part 2 of 5)

55 fig06_19.c (Part 3 of 5)

56 fig06_19.c (Part 4 of 5)

57 fig06_19.c (Part 5 of 5)

58 Program Output Enter a number between 0 and 28: 25 Subscripts:
Subscripts: * * 24 26* 28 24*  25 not found Enter a number between 0 and 28: 8  Subscripts: * 8 10* 12 8*  8 found in array element 4 Program Output

59 Enter a number between 0 and 28: 6 Subscripts:
Subscripts: * *  6 found in array element 3


Download ppt "Computer Programming Lecture 14 Arrays (Part 2)"

Similar presentations


Ads by Google