1 / 15

Introduction to Arrays in Computer Science: Concepts and Applications

This note set covers the fundamental concepts of arrays, a core data structure in computer science. It explains how arrays can store multiple pieces of homogeneous data contiguously in memory, accessed using their index. Key topics include basic array declaration and initialization, accessing elements, memory considerations, and passing arrays to functions. The document highlights common pitfalls such as the "off-by-one" error and the importance of zero-based indexing. Practical examples are provided to illustrate concepts, making it an essential resource for students in CSE 1341.

geri
Download Presentation

Introduction to Arrays in Computer Science: Concepts and Applications

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. Principles of Computer Science IHonors Section Note Set 3 CSE 1341

  2. Today: Arrays – Part 1

  3. The Array • Array • Can store multiple pieces of data • Homogeneous-typed data structure • Stored contiguously in memory • Each piece of data is an element • Each element can be accessed with one identified and an index.

  4. The Array h[0] ? • Arrays are zero subscripted. • Contiguous in memory • amt. of memory = size of datatype * number of elements Basic Declaration: int h[10]; ? h[1] ? h[2] ? h[3] ? h[4] ? h[5] ? h[6] ? h[7] ? h[8] ? h[9]

  5. Array Declaration and Initialization int n[3] = {2, 4, 6}; Initialization List 2 ? ? 4 ? 6 n[0] n[1] n[2] int g[3] = {9}; Partial Initialization List 9 ? ? 0 ? 0 g[0] g[1] g[2]

  6. Size Declarators • Size declarators must be literals or named constants • Cannot be a variable • Compiler needs to know how much memory to set aside for the array. int temp; cin >> temp; int myArray[temp];

  7. Accessing Array Elements • Name of an array is like a constant pointer to a location in memory • Name of array used with a subscript to access individual elements of the array int g[5] = {9, 4, 2}; g[0] = 0; for (int i=0; i<4; i++) g[i]++; subscript – bracket notation subscript can be a variable

  8. “Off By One” and No Bounds Checking int g[35] = {0}; int i = 0; cout << “Let’s enter some grades!”; for (i = 0; i <= 35; i++) { ... } • Past the end of the array • Compiler doesn’t care if you do this • Not a syntax error but almost always a logic error

  9. Some Grades… int g[35] = {0}; int i = 0; cout << “Let’s enter some grades!”; for (i = 0; i < 35; i++) { cout << “Enter a grade or -1 to quit: “; cin >> g[i]; if(g[i] == -1) break; }

  10. Passing Some Grades to a Function int g[35] = {0}; int i = 0; cout << “Let’s enter some grades!”; for (i = 0; i < 35; i++) { cout << “Enter a grade or -1 to quit: “; cin >> g[i]; if(g[i] == -1) break; } average(g, i); Just sending a memory address double average(int g[], int sz) { int sum = 0; for (int i = 0; i < sz; i++) sum += g[i]; return static_cast<float>(sum)/sz; }

  11. Arrays to Functions • When passing arrays to functions • function call has no [ ] • function header & prototype have empty [ ] • the array is always passed by “reference” • you’re just passing a memory address

  12. Character Arrays null-terminated c-string char fName[] = “Mark”; char lName[] = “Fontenot”; cout << lName << “, “ << fName << endl; • You can display the entire c-string by passing the name of the array to cout • DOES NOT WORK FOR numerical-basedarrays

  13. char pWord[25]; cin >> pWord; Will read characters to the first white-space character

  14. Remember… • Arrays are zero-subscripted • last subscript is SizeDeclarator – 1 • Use subscripts to access array • Arrays are always passed by reference • Array contents cannot be copied with = operator – must use loop

  15. Questions??? ?

More Related