1 / 34

Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman. CP 202 Chapter 8. 11-10-1429. CHAPTER 8 - Arrays. #. Arrays. 1. Introduction An array is a collection of two or more adjacent memory cells that are associated with a particular symbolic name.

keely
Télécharger la présentation

Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Problem Solving and Program Design in C (5th Edition)by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 8 11-10-1429

  2. CHAPTER 8 - Arrays #

  3. Arrays 1 • Introduction • An array is a collection of two or more adjacent memory cells that are associated with a particular symbolic name. • Subtopics • Declaring arrays • Manipulating arrays • Declaring and initializing arrays

  4. ArraysDeclaring 1a typearray_name[size] ; • Introduction • Declaring arrays is similar to declaring variables, but declaring arrays requires declaring the name of the array and the number of the cells associated with it. • Syntax • Example • int id[3]; • double dollar[5]; • char gender[2];

  5. ArraysManipulating 1b

  6. ArraysManipulating 1b

  7. ArraysDeclaring and Initializing 1c • Introduction • Arrays can be initialized when they are declared as same as variables • Example • int prime[] = {2, 3, 5, 7, 11, 13, 17, 19}; • char vowels[] = {‘A’, ‘E’, ‘I’, ‘O’, ‘U’};

  8. Arrays and Loops 2 • Introduction • If you wish to process the elements of an array in sequence, you can use loop • Note: any array starts with element zero (not one) • Example • Write a fragment of a program that stores the squares of the integers 0 through 9 ? int square[10], i;for (i=0; i<10; ++i) square[i] = i * i;

  9. Calculating Mean and SD Implementation Testing Maintenance Problem Analysis Design Outline 1 Write a program that computes the mean and standard deviation of an array of data and displays the difference between each value and the mean.

  10. Calculating Mean and SD Implementation Testing Maintenance Problem Analysis Design Outline 1

  11. Calculating Mean and SD Implementation Testing Maintenance Problem Analysis Design Outline 1

  12. Arrays and Pointers 3 andy [0] [1] [2] ‘a’ ‘b’ ‘c’ 1775 1776 1777 1778 1779 fred ted [0] [1] [2] 1776 ‘a’ ‘b’ ‘c’ • Without arrays: intandy, *ted; andy = 25; fred = andy; ted = &andy; • With arrays: char andy[]={‘a’,‘b’,‘c’}; char fred[3]; char *ted; fred[0] = andy[0]; fred[1] = andy[1]; fred[2] = andy[2]; ted = andy;

  13. *p Arrays and PointersTutorial [0] [1] [2] [3] [4] ‘a’ ‘b’ ‘c’ ‘d’ ‘e’ 1775 1776 1777 1778 1779 letters Way 1 Way 2 Way 3 Way 4 Way 5 no pointer int main (void) { char letters[5], char *p;int n; p = letters; *p = ‘a’; // ≡ letters[0]=‘a’ p++; *p = ‘b’; // ≡ letters[1]=‘b’ p = &letters[2]; *p = ‘c’; // ≡ letters[2]=‘c’ p = letters + 3; *p = ‘d’; // ≡ letters[3]=‘d’ p = letters; *(p+4) = ‘e’; // ≡ letters[4]=‘e’ for (n=0; n<5; n++) printf(“%c”, letters[n]); return (0); }

  14. Arrays and Functions 4 • Introduction • Previous examples in functions show that the arguments are variables, so changing their values inside the functions will not affect the original values in the main function • However, using pointers allows you to send the address of a variable instead of the value of a variable, so the changing will take affect • Sending an array as an argument requires sending the address of the first element in the array • Subtopics • Arrays as output arguments • Arrays as input arguments

  15. Arrays and FunctionsArrays as Output Arguments 4a ortest(int list[]) orlist[5] = 10; ortest(x) • Introduction • In simple (not array) parameters: void test(int *list) { *list = 10;}int main(void) {int x; test(&x);} • In array parameters: void test(int *list) {*(list+5) = 10;}int main(void) {int x[10]; test(&x[0]);}

  16. Arrays and FunctionsArrays as Output Arguments 4a • Example

  17. Arrays and FunctionsArrays as Input Arguments 4b or test(constint list[]) or test(x) • Introduction • In simple (not array) parameters: void function test(int list) { : }int main(void) {int x; test(x);} • In array parameters: void test(const int *list) { : }int main(void) {int x[10]; test(&x[0]);}

  18. Arrays and FunctionsArrays as Input Arguments 4b • Example

  19. Arrays and Functions 4 • Example

  20. Simulating Stacks Implementation Testing Maintenance Problem Analysis Design Outline 2 A stack is a data structure in which only the top element can be accessed. Inserting a new element at the top of a stack is called push. Removing the top element of a stack is called pop.

  21. Simulating Stacks Implementation Testing Maintenance Problem Analysis Design Outline 2 Write the main() function for this program?

  22. Array Search - Linear Search Implementation Testing Maintenance Problem Analysis Design Outline 3 Search for a value inside an array. The simple process is to check each element in the array using a loop and exit the loop if the value is found or you finished checking all the elements. This process is called a linear search.

  23. Array Search - Linear Search Implementation Testing Maintenance Problem Analysis Design Outline 3 • Assume the target has not been found. • Start with the initial array element. • Repeat while the target is not found and there are more array elements. • If the current element matches the target • Set a flag to indicate that the target has been found. • else • Advance to the next array element. • If the target was found • Return the target index as the search result. • else • Return -1 as the search result.

  24. Array Search - Linear Search Implementation Testing Maintenance Problem Analysis Design Outline 3

  25. Sorting an Array Implementation Testing Maintenance Problem Analysis Design Outline 4 • Start from the first data element until the previous of the last one and do the following: • Search for the smallest element from the current data element until the last one [subarray]. • If you find one, exchange it with the current data element.

  26. Sorting an Array Implementation Testing Maintenance Problem Analysis Design Outline 4

  27. Multidimensional Arrays 5 char jimmy[3][5]; int cube[100][5][4]; cube[0][2][3] = 2; • Multi-dimensional arrays are arrays with two or more dimensions. • Example of two dimensions: • Example of three dimensions:

  28. Multidimensional ArraysDeclaring and Initializing 5 One Dimension arrays:int prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};char vowels[] = {‘A’, ‘E’, ‘I’, ‘O’, ‘U’}; Two Dimensions arrays:int words[2][3] = { {‘a’,’n’,’d’}, {‘o’,’r’,’ ’}};

  29. Tic-Tac-Toe Implementation Testing Maintenance Problem Analysis Design Outline 5 ttt_brd[1][2] Write a function to check whether a tic-tac-toe board is completely filled, assuming the original value for each cell is one space?

  30. Tic-Tac-Toe Implementation Testing Maintenance Problem Analysis Design Outline 5

  31. Enrollment Data in 3D Implementation Testing Maintenance Problem Analysis Design Outline 6 • Assuming that you have three-dimensional array, called enroll, that may be used to store the enrollment data for a college. • The college offers 100 courses at five different campuses. We will number the freshman year 0, the sophomore year 1,… 3 • Write the code for: • Display number of students in each course • Display number of students at each campus

  32. Enrollment Data in 3D Implementation Testing Maintenance Problem Analysis Design Outline 6 // Display number of students in each course for (course = 0; course < 100; course++) { course_sum = 0; for (campus = 0; campus < 5; campus++) { for (cls_rank = 0; cls_rank < 4; cls_rank++) { course_sum += enroll[course][campus][cls_rank]; } } printf(“Number of students in course %d is %d\n”, course, course_sum); }

  33. Enrollment Data in 3D Implementation Testing Maintenance Problem Analysis Design Outline 6 // Display number of students at each campus for (campus = 0; campus < 5; campus++) { campus_sum = 0; for (course = 0; course < 100; course++) { for (cls_rank = 0; cls_rank < 4; cls_rank++) { campus_sum += enroll[course][campus][cls_rank]; } } printf(“Number of students at campus %d is %d\n”, campus, campus_sum); }

  34. CHAPTER 8 - Arrays C

More Related