CHAPTER 6 ONE DIMENSIONAL ARRAY CSC 126 FUNDAMENTALS

  • Slides: 41
Download presentation
CHAPTER 6: ONE – DIMENSIONAL ARRAY CSC 126 FUNDAMENTALS OF ALGORITHM DESIGN & COMPUTER

CHAPTER 6: ONE – DIMENSIONAL ARRAY CSC 126 FUNDAMENTALS OF ALGORITHM DESIGN & COMPUTER PROBLEM SOLVING

Table of contents. . What is an array? Array declaration Array initialization Processing array

Table of contents. . What is an array? Array declaration Array initialization Processing array

What is an array? ? An array is a series or list of variables

What is an array? ? An array is a series or list of variables in computer memory, with the same name and data type but are differentiated with special numbers called subscripts. A subscript is a unique number and also called an index, is a number that indicates the position of a particular item within an array.

What is an array? ? 2 Element 1 ? 0 Subscript/ Index Company Logo

What is an array? ? 2 Element 1 ? 0 Subscript/ Index Company Logo

What is an array? ? For example, we can store 6 values of integer

What is an array? ? For example, we can store 6 values of integer / real data type in an array without having to declare 6 different variables. By using an array, we can store 6 different values of the same data type with a unique identifier. E. g. Read GPA student in 6 semesters. Company Logo www. themegallery. com

Introduction – array Each variable in a one – dimensional array is identified by

Introduction – array Each variable in a one – dimensional array is identified by a unique number, called index or subscripts. The index indicates the variable’s position in the array. First variable – index equal to ‘ 0’. And so on… You refer to each variable in array by the array’s name and the variable’s index Array name Index names [0] Siti names [1] Ani names [2] Amat names [3] Ali

ARRAY DECLARATION

ARRAY DECLARATION

Declaration – one dimensional array The general form of declaring a one-dimensional array is:

Declaration – one dimensional array The general form of declaring a one-dimensional array is: data. Type array. Name [array. Size]; where array. Size must be an integer greater than zero. Example: double GPA [3];

Declaration – one dimensional array double GPA [3]; The compiler allocates the space for

Declaration – one dimensional array double GPA [3]; The compiler allocates the space for 3 double elements for array GPA. When array is created, its elements are assigned with arbitrary values Subscript/ Index Element GPA[0] GPA [1] GPA [2]

Assign value to array Syntax: array. Name [index] = value; Example: double GPA [3];

Assign value to array Syntax: array. Name [index] = value; Example: double GPA [3]; GPA [0] = 3. 2;

ARRAY INITIALIZATION

ARRAY INITIALIZATION

Initialization – one dimensional array Like any other simple variable, arrays can also be

Initialization – one dimensional array Like any other simple variable, arrays can also be initialized while they are being declared When initializing arrays while declaring them, it is not necessary to specify the size of the array The size of the array is determined by the number of initial values in the braces

Initialization – one dimensional array Example: double mylist [3]={1. 0, 2. 1, 3. 2};

Initialization – one dimensional array Example: double mylist [3]={1. 0, 2. 1, 3. 2}; OR double mylist [ ]={1. 0, 2. 1, 3. 2}; mylist [0] 1. 0 mylist [1] 2. 1 mylist [2] 3. 2

Partial Initialization The statement: double list[5] = {1. 0, 2. 2}; declares list to

Partial Initialization The statement: double list[5] = {1. 0, 2. 2}; declares list to be an array of 5 components and initializes the first two elements with values 1. 0 and 2. 2. list 1. 0 2. 2 0 0 1 2 3 4

Partial Initialization The statement: int list[5] = {0}; declares list to be an array

Partial Initialization The statement: int list[5] = {0}; declares list to be an array of 5 components and initializes all components to zero list 0 0 0

Partial Initialization The statement: char list[5] = {‘A’, ‘B’}; declares list to be an

Partial Initialization The statement: char list[5] = {‘A’, ‘B’}; declares list to be an array of 5 components and initializes the first two elements with va; ue ‘A’ and ‘B’ list A B 0 1 2 Company Logo 3 4 www. themegallery. com

Initializing array Initial with 0 const int size = 10; int array[size]; int index

Initializing array Initial with 0 const int size = 10; int array[size]; int index = 0; while(index<10) { { array[index] = 0; array[10]={0}; array[index] = 5; index++; array[10]={5}; index++; } } Why this is correct? Why this is wrong?

ACCESSING ARRAY

ACCESSING ARRAY

Accessing Array Components The general form (syntax) of accessing an array component is: array.

Accessing Array Components The general form (syntax) of accessing an array component is: array. Name[index. Exp] � where index. Exp, called index, is any expression whose value is a non negative integer � Index value specifies the position of the component in the array � The [] operator is called the array subscripting operator � The array index starts at 0

Accessing Array Components numbers [0] numbers 10 numbers [1] numbers [2] 20 30 To

Accessing Array Components numbers [0] numbers 10 numbers [1] numbers [2] 20 30 To replace the value 30 in the third elements of numbers with value 25. numbers[2] = 25; numbers [0] numbers 10 numbers [1] 20 numbers [2] 25

Accessing Array Components To pass the value of the third element of numbers to

Accessing Array Components To pass the value of the third element of numbers to a variable called a, we could write: a = numbers[2] ;

Accessing Array Components At this point it is important to be able to clearly

Accessing Array Components At this point it is important to be able to clearly distinguish between the two uses that brackets [ ] have related to arrays. They perform two different tasks: specify the size of arrays when they are declared to specify indices for concrete array elements. int numbers[3]; // specify the size of array numbers[2] = 20; // access elements of array

Accessing Array Components Some other valid operation with arrays: • numbers[0] = 20; •

Accessing Array Components Some other valid operation with arrays: • numbers[0] = 20; • numbers [a] = 75; • b = numbers [a+2]; • numbers [1] = numbers [0] + numbers [2];

Array Index Out of Bounds If we have the statements: double num[10]; int i;

Array Index Out of Bounds If we have the statements: double num[10]; int i; The component num[i] is a valid index if i = 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9 The index of an array is in bounds if the index >=0 and the index <= array. Size-1 If either the index < 0 or the index > array. Size 1, then we say that the index is out of bounds

MANIPULATE ONE – DIMENSIONAL ARRAY

MANIPULATE ONE – DIMENSIONAL ARRAY

Processing Arrays Some of the basic operations performed on a onedimensional array are: �

Processing Arrays Some of the basic operations performed on a onedimensional array are: � Assign/ set array � Input/ output array � find the largest and/or smallest element � Counting � Find total � Average Each of these operations requires the ability to step through the elements of the array Stepping-through the elements of an array is easily accomplished by a loop

Assign value in Array If we want to set a data or a value

Assign value in Array If we want to set a data or a value into an element, use the following way: cin>> …

Assign value in Array char letters [3]={‘A’, ‘B’, ‘C’}; //line 1 letters [0] =

Assign value in Array char letters [3]={‘A’, ‘B’, ‘C’}; //line 1 letters [0] = ‘x’ letters [1] = ‘y’ letters [2] = ‘z’ //line 2 //line 3 //line 4 Assigns the letters x, y, and z to the letters array, replacing the letters A, B, C Line 1: letters After line 2: letters ‘A’ ‘B’ ‘C’ [0] [1] [2] ‘x’ ‘y’ ‘z’ [0] [1] [2]

Assign value in Array string names [4] = {“bob”, “Nancy”, “Bill”, “Samuel”} names [1]

Assign value in Array string names [4] = {“bob”, “Nancy”, “Bill”, “Samuel”} names [1] = “Helen” //line 2 Assigns the name Helen to the second element in the names array, replacing the name Nancy After line 2: names “bob” [0] “helen” [1] “bill” [2] “samuel ” [3]

Assign value in Array int sub = 0; while(sub<4) { states[sub] = “ ”;

Assign value in Array int sub = 0; while(sub<4) { states[sub] = “ ”; sub = sub + 1; } Assigns the empty string to each element in the states array After exit loop: states “ ” [0] [1] “ ” “” [2] [3]

Assign value in Array for(int x = 1; x<=3; x = x + 1)

Assign value in Array for(int x = 1; x<=3; x = x + 1) nums[x - 1] = x*x; Assigns the squares of the numbers from 1 through 3 to the nums array, replacing the data stored in the arrray. After exit loop: nums 1 4 9 [0] [1] [2]

Input & output Get input Display output const int size = 10; const int

Input & output Get input Display output const int size = 10; const int size = 5; int array[size]={1, 5, 6, 7, 20}; int index = 0; while(index<10) while(index<5) { { } cin>>array[index]; cout<<array[index]; index++; }

Copying arrays Copy array const int ARRAY_SIZE = 5; for(int i = 0; i<ARRAY_SIZE;

Copying arrays Copy array const int ARRAY_SIZE = 5; for(int i = 0; i<ARRAY_SIZE; i++) { list[i] = mylist[i]; }

Exercise

Exercise

Sum & Average sum average const int size = 4; int array[size] = {2,

Sum & Average sum average const int size = 4; int array[size] = {2, 5, 10, 50}; int index = 0; int sum = 0; while(index<4) { { } sum = sum + array[index]; index++; } average = sum / 4

Exercise

Exercise

Max & Min Maximum Minimum const int size = 4; int array[size] = {2,

Max & Min Maximum Minimum const int size = 4; int array[size] = {2, 5, 10, 50}; int index = 0; int max =array[0]; int min =array[0]; while(index<4) { { if (max < array[index]) if (min > array[index]) max = array[index]; min = array[index]; index++; }

Exercise

Exercise

Counting Count even numbers const int size = 4; int array[size] = {2, 5,

Counting Count even numbers const int size = 4; int array[size] = {2, 5, 10, 50}; int index = 0; int count = 0; while(index<4) { if(array[index]%2==0) count++; index++; } cout<< “total of even numbers: ”<<count;

Exercise

Exercise