one dimensional arrays: structured data types

24
Sahar Mosleh California State University San Marcos Page 1 One Dimensional Arrays: Structured data types

Upload: shiro

Post on 08-Jan-2016

19 views

Category:

Documents


0 download

DESCRIPTION

One Dimensional Arrays: Structured data types. 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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: One Dimensional Arrays: Structured data types

Sahar Mosleh California State University San Marcos Page 1

One Dimensional Arrays:

Structured data types

Page 2: One Dimensional Arrays: Structured data types

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

Page 3: One Dimensional Arrays: Structured data types

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

Page 4: One Dimensional Arrays: Structured data types

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

Page 5: One Dimensional Arrays: Structured data types

Sahar Mosleh California State University San Marcos Page 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.

Page 6: One Dimensional Arrays: Structured data types

Sahar Mosleh California State University San Marcos Page 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)

Page 7: One Dimensional Arrays: Structured data types

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

Page 8: One Dimensional Arrays: Structured data types

Sahar Mosleh California State University San Marcos Page 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 arraycout << line[ 21]

- 21 is called an index to the array line

Page 9: One Dimensional Arrays: Structured data types

Sahar Mosleh California State University San Marcos Page 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 runsi= 0, i= 1, i= 2, ..., i= 79

Page 10: One Dimensional Arrays: Structured data types

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

Page 11: One Dimensional Arrays: Structured data types

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

Page 12: One Dimensional Arrays: Structured data types

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

Page 13: One Dimensional Arrays: Structured data types

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

Page 14: One Dimensional Arrays: Structured data types

Sahar Mosleh California State University San Marcos Page 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 ofint ivec[2000]

Page 15: One Dimensional Arrays: Structured data types

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

Page 16: One Dimensional Arrays: Structured data types

Sahar Mosleh California State University San Marcos Page 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!

Page 17: One Dimensional Arrays: Structured data types

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

Page 18: One Dimensional Arrays: Structured data types

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

Page 19: One Dimensional Arrays: Structured data types

Sahar Mosleh California State University San Marcos Page 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]

Page 20: One Dimensional Arrays: Structured data types

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

Page 21: One Dimensional Arrays: Structured data types

Sahar Mosleh California State University San Marcos Page 21

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 arrayfor (i= 0; i< size; i++) {

cout << "Enter entry " << i << ": ";cin >> arr[ i];

}

Page 22: One Dimensional Arrays: Structured data types

Sahar Mosleh California State University San Marcos Page 22

Another Array Example

// sort by continually swapping out of order elementswhile (! sorted) {

sorted= true; // stays true if nothing out of orderfor (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 variableWhy is this necessary?

Page 23: One Dimensional Arrays: Structured data types

Sahar Mosleh California State University San Marcos Page 23

Another Array Example

// print out sorted arrayfor (i= 0; i< size; i++){

cout << arr[ i] << " ";}

} // end of main program

< See: Example 3>

Page 24: One Dimensional Arrays: Structured data types

Sahar Mosleh California State University San Marcos Page 24

Another Array Example

- What is the output on the following input:3 8 -1 5 6

3 8 -1 5 6 i= 1 // 1st time while loop

3 -1 8 5 6 i= 23 -1 5 8 6 i= 33 -1 5 6 8 i= 4-1 3 5 6 8 i= 1 // 2nd time while loop

............... // ….// ….

- Therefore, the output is:-1 3 5 6 8