CS 242T: Computer Programming - Arrays PDF

Document Details

SophisticatedGermanium

Uploaded by SophisticatedGermanium

Tags

arrays computer programming data structures c++

Summary

These lecture notes cover computer programming concepts related to arrays, including one-dimensional and multidimensional arrays, initialization techniques, and handling out-of-bound errors. The notes also include examples demonstrating array usage and the different initialization methods. This is a good resource for learning about arrays in C++.

Full Transcript

CS 242T : Computer Programming Arrays Content One-dimensional arrays Array elements Initialization of arrays Out-of-Bound error Multidimensional arrays 2 Array ...

CS 242T : Computer Programming Arrays Content One-dimensional arrays Array elements Initialization of arrays Out-of-Bound error Multidimensional arrays 2 Array  Data structure containing related data items of the same type Hold in consecutive group of memory locations.  “static” entity remain the same size throughout program execution.  Syntax: dataType arrayName[arraySize]; int c; c ? Array size (Number of elements) c ? c ? Name of array (any valid identifier) c ? Type of array elements c ? 3 Array Elements  Data items in the array.  Each element has a position in the array called index. First element has index 0 and last element has index arraySize-1.  To access any element, giving array name followed by index in [ ] c, c, …etc [ ] is an operator has same precedence and associativity of ( ). Position number must be Positive integer OR any expression that results in a positive integer. int a=5, b=6; c[a+b] +=2; // Adds 2 to array element c[ 11 ] 4 Array Elements (cont.) First element with index 0 Last element with index 11 Result of c[a+b] +=2; will store 80 in c 5 Array Initialization – Approach (1)  Using a loop to initialize array’s elements 1 #include 2 using namespace std; Declare n as an array of 3 ints with 10 elements 4 void main() { 5 6 int n[ 10 ]; // n is an array of 10 integers 7 Each int initialized is to 0 8 // initialize elements of array n to 0 9 for ( int i = 0; i < 10; i++ ) 10 n[ i ] = 0; // set element at location i to 0 11 12 cout

Use Quizgecko on...
Browser
Browser