150 likes | 268 Views
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.
E N D
Principles of Computer Science IHonors Section Note Set 3 CSE 1341
Today: Arrays – Part 1
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.
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]
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]
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];
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
“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
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; }
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; }
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
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
char pWord[25]; cin >> pWord; Will read characters to the first white-space character
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