CSC 1060 Week 13 Arrays and Pointers PDF

Document Details

DivineZebra9695

Uploaded by DivineZebra9695

Red Rocks Community College

Tags

C++ arrays arrays and pointers data structures programming

Summary

This document is a set of lecture notes or study guides covering arrays and pointers in C++. It explains different aspects of arrays and how to manipulate memory using pointers. Examples are also included through the text.

Full Transcript

CSC 1060 ARRAYS & POINTERS OBJECTIVES AGENDA: WEEK 13 Understand how arrays 1. Arrays and Array Indexes, Syntax represent linear data structure and Initializers 2. Traversing an Array Dec...

CSC 1060 ARRAYS & POINTERS OBJECTIVES AGENDA: WEEK 13 Understand how arrays 1. Arrays and Array Indexes, Syntax represent linear data structure and Initializers 2. Traversing an Array Declare an array and initialize, access, modify the elements 3. User input into Array and Partially filled Arrays Program common array operations 4. Common algorithms and Bubble Apply arrays in application Sort development 5. Parallel Arrays Manipulate memory using pointers, 6. Multi-Dimensional Arrays effectively allocate and deallocate 7. Why Pointers? memory, access and work with 8. Arrays as Function Arguments arrays 9. Pointers and Memory Explore the relationship between 10. C-Style Strings arrays and pointers 11. TODO & Resources for Help PRE-CHALLENGE Review the content 5.2 Arrays Visualize the code in the 3 CodeLens programs Complete the 2 multiple choice questions at the end C++ ARRAYS Array is a linear data structure, that is a container to store elements (items) Rules for array 1. All elements are of the same data type 2. The array is created at compile-time with a fixed constant size. a) Hardcoded integer b) Compiler constant: const int MAX = 10; c) Pre-processor constant: #define MAX 10 By default, arrays of local scope, are not initialized when declared: Arrays of type int, float, char, etc... the elements are undetermined Arrays of class types like std::string, each element is initialized by the default constructor of the class ARRAY INDEXES (CPLUSPLUS) Elements are stored in contiguous memory locations that can be individually referenced using [index] notation In C++, indexes are 0 based It is the role of the developer to make sure that indexes are NEVER out-of-bounds. This is an intent / logic / runtime error, NOT a compiler error. Sometimes the program runs correctly and other times it blows up. Indexes: [0 to SIZE-1] ARRAY SYNTAX Declaring an array 0 1 2 3 const int MAX = 4; nums int nums[MAX]; Modifying / Populating values in the array: [index] nums = 10; nums = 25; 0 1 2 3 nums 10 nums = 14; 25 14 Accessing elements in the array: [index] std::cout

Use Quizgecko on...
Browser
Browser