CSE 100 Lecture Notes - Lecture 32: Array Data Structure, In C, Empty String

116 views4 pages
Chapter 8: Arrays
Array: a variable that can store multiple values of the same type
The values are stored in consecutive memory locations
It is declared using [ ] operator
const int ISIZE = 5;
int tests[ISIZE];
The definition
int tests[ISIZE]; //ISIZE is 5
Allocates the follow memory
Element 0
Element 1
Element 2
Element 3
Element 4
Array Terminology
In the definition int tests[ISIZE];
int is the data type of the array elements
tests is the name of the array
ISIZE, in [ISIZE], is the size declarator
It shows the number of elements in the array
The size of an array is the number of bytes allocated for it
(number of elements)*(bytes needed for each element)
Array Terminology Examples
Assumes int uses 4 bytes and double uses 8 bytes
const int ISIZE = 5, DSIZE = 10;
int tests[ISIZE]; //holds 5 ints, array occupies 20 bytes
double volume[DSIZE]; //holds 10 doubles, array occupies 80 bytes
8.2 Accessing Array Elements
Each array element has a subscript, used to accdss the element
Subscripts start at 0
0
1
2
3
4
Array elements (accessed by array name and subscript) can be used as regualr
variables
Tests
0
1
2
3
4
tests [0] = 79;
cout << tests[0];
cin >> tests[1];
tests[4] = tests[0] + tests[1];
cout << tests; //illegal due to missing subscript
Unlock document

This preview shows page 1 of the document.
Unlock all 4 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Array : a variable that can store multiple values of the same type. The values are stored in consecutive memory locations. It is declared using [ ] operator. Int is the data type of the array elements. Tests is the name of the array. Isize, in [isize], is the size declarator. The size of an array is the number of bytes allocated for it. It shows the number of elements in the array. (number of elements)*(bytes needed for each element) Assumes int uses 4 bytes and double uses 8 bytes. Const int isize = 5, dsize = 10; Int tests[isize]; //holds 5 ints, array occupies 20 bytes. Double volume[dsize]; //holds 10 doubles, array occupies 80 bytes. Each array element has a subscript, used to accdss the element. Array elements (accessed by array name and subscript) can be used as regualr variables. Cout << tests; //illegal due to missing subscript.

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents