One Dimensional Arrays Structured data types The contents

  • Slides: 24
Download presentation
One Dimensional Arrays: Structured data types The contents of this lecture prepared by the

One Dimensional Arrays: Structured data types The contents of this lecture prepared by the instructors at the University of Manitoba in Canada and modified by Dr. Ahmad Reza Hadaegh A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 1

One Dimensional Arrays Structured data types One Dimensional Arrays - Seen only simple or

One Dimensional Arrays Structured data types One Dimensional Arrays - Seen only simple or atomic types so far: - int, char, float - Plus ifstream and ofstream which are a bit more complex - structure is largely hidden from programmer - access only through operations: <<, >>, … A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 2

Structured Data - Often work with a bunch of data with some overall structure

Structured Data - Often work with a bunch of data with some overall structure - Analogous to: - working with a page of text and not a few hundred individual characters - working with a book, and not a few hundred loose pages - C++ provides “Arrays” as a basic way of working with structured data A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 3

One Dimensional Arrays - Often want to manipulate a line of text with a

One Dimensional Arrays - Often want to manipulate a line of text with a computer - For example: - read, print and reverse a line of 80 chars - with techniques introduced so far: - read 80 chars into 80 variables of type char - print them in the order read - print them in reverse order (Looks Hard !) A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 4

One Dimensional Arrays int main() { char c 1, c 2, c 3, c

One Dimensional Arrays int main() { char c 1, c 2, c 3, c 4, …, c 80; cin >> c 1 >> c 2 >> c 3 >> c 4 >> ………>> c 80; cout << c 1 << c 2 << c 3 << … << c 80 << endl; cout << c 80 << c 79 << ……. << c 2 << c 1 << endl; } - What if number of chars not known in advance? - What if a more complicated manipulation is required? - This approach is messy and inadequate. A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 5

One Dimensional Arrays - Declare a single variable line which holds an array of

One Dimensional Arrays - Declare a single variable line which holds an array of 80 chars with the declaration: char line[80]; - the individual chars found in line are accessed as: line[0] line[1] line[2] ……. line[79] - numbering from 0 to 79 (not 1 to 80) A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 6

One Dimensional Arrays int main() { char line[80]; cin >> line[0] >> line[1] >>

One Dimensional Arrays int main() { char line[80]; cin >> line[0] >> line[1] >> … >> line[79]; cout << line[0] << line[1] << … << line[79] << endl; cout << line[79] << line[78] <<. . . << line[0]<< endl; } - This doesn't really help much yet - Only the variable declaration was simpler A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 7

One Dimensional Arrays - In the declaration char line[80]; - line is a one-dimensional

One Dimensional Arrays - In the declaration char line[80]; - line is a one-dimensional arrays - 80 is the size of the array - the number of integers the array contains - In the use of the array cout << line[ 21] - 21 is called an index to the array line A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 8

One Dimensional Arrays - Key observation: - arrays can be indexed with variables int

One Dimensional Arrays - Key observation: - arrays can be indexed with variables int main() { char line[ 80]; int i; for (i= 0; i< 80; i= i+ 1) cin >> line[ i]; for( i= 0; i< 80; i= i+ 1) cout << line[ i]; for( i= 79; i>= 0; i= i- 1) cout << line[ i]; } A. R. Hadaegh Dr. Ahmad R. Hadaegh Loop runs i= 0, i= 1, i= 2, . . . , i= 79 California State University San Marcos (CSUSM) Page 9

One Dimensional Arrays - Arrays can be declared of any data type - char,

One Dimensional Arrays - Arrays can be declared of any data type - char, int, float, . … - General format of array declaration: Data. Type Array. Name [Const. Int. Expression]; - Data. Type is the data type of the array elements - Array. Name is the name of the array - Const. Int. Expression is a literal int or int constant A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 10

One Dimensional Arrays - Examples of Array Declaration - float cmplx[2]; // entries: cmplx[0],

One Dimensional Arrays - Examples of Array Declaration - float cmplx[2]; // entries: cmplx[0], cmplx[1] - int ivec[2000]; // entries: ivec[0], . . . , ivec[1999] - const int arraylen= 1000; - char biglin[arraylen]; // array biglin has entries biglin[0], . . . , biglin[999]; A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 11

One Dimensional Arrays - Accessing Array Components - General form of array access is:

One Dimensional Arrays - Accessing Array Components - General form of array access is: Array. Name[Index. Expression] - Array. Name is the name of the array - Index. Expression is any integer expression - When evaluated, an array access is just like any other accessed variable - treat it like you would any value of the array type A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 12

One Dimensional Arrays - Array Entry Assignment ivec[0]= 17; ivec[0]= ivec[0]* 3+ 2; ivec[0]-2]=

One Dimensional Arrays - Array Entry Assignment ivec[0]= 17; ivec[0]= ivec[0]* 3+ 2; ivec[0]-2]= 5; cout << cmplx[0]; cin >> biglin[5]; foo( ivec[0]); A. R. Hadaegh Dr. Ahmad R. Hadaegh // ivec entry 0 assigned 17 // ivec entry 0 assigned 53 // ivec entry 51 assigned 5 // print out cmplx entry 0 // read biglin entry 5; // call function foo with value 53 California State University San Marcos (CSUSM) Page 13

One Dimensional Arrays - Array Storage - array entries are stored one after another

One Dimensional Arrays - Array Storage - array entries are stored one after another in memory - the index is just the offset from the beginning of the array Memory Layout of int ivec[2000] A. R. Hadaegh Dr. Ahmad R. Hadaegh ivec[0] ivec[1] ivec[2] ……. ivec[1999] Each location holds a single integer California State University San Marcos (CSUSM) Page 14

One Dimensional Arrays // E. G: find the largest element in array of integers

One Dimensional Arrays // E. G: find the largest element in array of integers start out assuming int main() { Value. Array[ 0] is largest int Value. Array[500]; . . . ; // somehow move data into Value. Array int maxval, i; maxval= Value. Array[0]; for (i= 1; i< 500; i++) Update maxval if a if (Value. Array[ i]> maxval) larger value found maxval= Value. Array[i]; cout << "Maximum value in array is " << maxval; } A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 15

One Dimensional Arrays - Out of Bounds Array Indices - What does the following

One Dimensional Arrays - Out of Bounds Array Indices - What does the following code do? char line[80]; line[80]= 'X'; - Code accesses element 80, but the last defined element in the array is line[79] - result is undetermined - it may even modify other variables - C++ does not check this - your program may do weird things - this is invalid code! A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 16

One Dimensional Arrays Initializing arrays - Like variables of type char, float and int,

One Dimensional Arrays Initializing arrays - Like variables of type char, float and int, arrays can be initialized when declared: int littlearray[5]={2, 4, 6, 8, 10}; float wintertemp[3] = {-40, -45, -50}; - This is especially important for const arrays const int Ints. Mod 5[ 5]={ 0, 1, 2, 3, 4}; const float Fun. Constants[ 2]={3. 14, 2. 71}; // arrays of constants cannot be modified A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 17

Array Operations - C++ does not provide "Aggregate" array operations - Operations which act

Array Operations - C++ does not provide "Aggregate" array operations - Operations which act on entire array - Example: int x[100], y[100], z[100]; // two arrays of 100 ints x= y; // this does not assign contents of y to x x= y+z; // this does not assign contents of y+z to x A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 18

Array Operations - To assign the contents of array y to x, you need

Array Operations - To assign the contents of array y to x, you need a loop: int i; int x[100], y[100]; for (i= 0; i< 100; i++) x[i]= y[i]; // assign entry y[i] to value x[i] A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 19

Array Operations - Also cannot - Output arrays: int arr[3]={ 1, 2, 3}; cout

Array Operations - Also cannot - Output arrays: int arr[3]={ 1, 2, 3}; cout << arr; - Return arrays: return arr; - Operations can be defined (by user functions) - Not defined by automatically for most types A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 20

Another Array Example // Sorting an array of integers into increasing order #include <iostream>

Another Array Example // Sorting an array of integers into increasing order #include <iostream> using namespace std; const int size = 5; // constant for array size int main() { int arr[size]; int i, tmp; bool sorted = false; // get the user to read in the array for (i= 0; i< size; i++) { cout << "Enter entry " << i << ": "; cin >> arr[ i]; } A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 21

Another Array Example // sort by continually swapping out of order elements while (!

Another Array Example // sort by continually swapping out of order elements while (! sorted) { sorted= true; // stays true if nothing out of order for (i= 1; i< size; i++) { if (arr[ i]< arr[ i- 1]) { sorted = false; // swap arr[ i] and arr[ i- 1]; tmp = arr[ i]; arr[ i]= arr[ i- 1]; arr[ i- 1]= tmp; } } } Note use of tmp variable Why is this necessary? A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 22

Another Array Example // print out sorted array for (i= 0; i< size; i++)

Another Array Example // print out sorted array for (i= 0; i< size; i++) { cout << arr[ i] << " "; } } // end of main program < See: Example 3> A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 23

Another Array Example - What is the output on the following input: 3 8

Another Array Example - What is the output on the following input: 3 8 -1 5 6 3 -1 8 5 6 3 -1 5 8 6 3 -1 5 6 8 -1 3 5 6 8. . . . i= 1 i= 2 i= 3 i= 4 i= 1 // 1 st time while loop // 2 nd time while loop // …. - Therefore, the output is: -1 3 5 6 8 A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 24