Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sahar Mosleh California State University San MarcosPage 1 One Dimensional Arrays: Structured data types.

Similar presentations


Presentation on theme: "Sahar Mosleh California State University San MarcosPage 1 One Dimensional Arrays: Structured data types."— Presentation transcript:

1 Sahar Mosleh California State University San MarcosPage 1 One Dimensional Arrays: Structured data types

2 Sahar Mosleh California State University San MarcosPage 2 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: >, …

3 Sahar Mosleh California State University San MarcosPage 3 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

4 Sahar Mosleh California State University San MarcosPage 4 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 !)

5 Sahar Mosleh California State University San MarcosPage 5 One Dimensional Arrays int main() { char c1, c2,c3,c4,…, c80; cin >> c1 >> c2 >> c3 >> c4 >> ………>> c80; cout << c1 << c2 << c3 << … << c80 << endl; cout << c80 << c79 << ……. << c2 << c1 << endl; } - What if number of chars not known in advance? - What if a more complicated manipulation is required? - This approach is messy and inadequate.

6 Sahar Mosleh California State University San MarcosPage 6 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)

7 Sahar Mosleh California State University San MarcosPage 7 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

8 Sahar Mosleh California State University San MarcosPage 8 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

9 Sahar Mosleh California State University San MarcosPage 9 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]; } Loop runs i= 0, i= 1, i= 2,..., i= 79

10 Sahar Mosleh California State University San MarcosPage 10 One Dimensional Arrays - Arrays can be declared of any data type - char, int, float,.… - General format of array declaration: DataType ArrayName [ConstIntExpression]; - DataType is the data type of the array elements - ArrayName is the name of the array - ConstIntExpression is a literal int or int constant

11 Sahar Mosleh California State University San MarcosPage 11 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];

12 Sahar Mosleh California State University San MarcosPage 12 One Dimensional Arrays - Accessing Array Components - General form of array access is: ArrayName[IndexExpression] - ArrayName is the name of the array - IndexExpression 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

13 Sahar Mosleh California State University San MarcosPage 13 One Dimensional Arrays - Array Entry Assignment ivec[0]= 17; // ivec entry 0 assigned 17 ivec[0]= ivec[0]* 3+ 2; // ivec entry 0 assigned 53 ivec[ivec[0]-2]= 5; // ivec entry 51 assigned 5 cout << cmplx[0]; // print out cmplx entry 0 cin >> biglin[5]; // read biglin entry 5; foo( ivec[0]); // call function foo with value 53

14 Sahar Mosleh California State University San MarcosPage 14 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 ivec[0] ivec[2] ……. ivec[1] ivec[1999] Each location holds a single integer Memory Layout of int ivec[2000]

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

16 Sahar Mosleh California State University San MarcosPage 16 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!

17 Sahar Mosleh California State University San MarcosPage 17 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 IntsMod5[ 5]={ 0,1,2,3,4}; const float FunConstants[ 2]={3.14, 2.71}; // arrays of constants cannot be modified

18 Sahar Mosleh California State University San MarcosPage 18 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

19 Sahar Mosleh California State University San MarcosPage 19 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]

20 Sahar Mosleh California State University San MarcosPage 20 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

21 Sahar Mosleh California State University San MarcosPage 21 Another Array Example // Sorting an array of integers into increasing order #include 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]; }

22 Sahar Mosleh California State University San MarcosPage 22 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?

23 Sahar Mosleh California State University San MarcosPage 23 Another Array Example // print out sorted array for (i= 0; i< size; i++) { cout << arr[ i] << " "; } } // end of main program

24 Sahar Mosleh California State University San MarcosPage 24 Another Array Example - What is the output on the following input: 3 8 -1 5 6 3 8 -1 5 6i= 1 // 1st time while loop 3 -1 8 5 6i= 2 3 -1 5 8 6i= 3 3 -1 5 6 8i= 4 -1 3 5 6 8 i= 1 // 2nd time while loop............... // …. // …. - Therefore, the output is: -1 3 5 6 8


Download ppt "Sahar Mosleh California State University San MarcosPage 1 One Dimensional Arrays: Structured data types."

Similar presentations


Ads by Google