chapter 7 arrays i ntroduction t o c omputer p rogramming (csc425)

29
CHAPTER 7 arrays INTRODUCTION TO COMPUTER PROGRAMMING (CSC425)

Upload: evelyn-cain

Post on 04-Jan-2016

246 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

CHAPTER 7arrays

INTRODUCTION TO COMPUTER

PROGRAMMING(CSC425)

Page 2: CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

• Introduction• Single-Dimensional Arrays

2

CONTENTS

CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

Page 3: CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

• Array :is a collection of a fixed number of components where in all of the components have the same data type

3

INTRODUCTION

CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

Page 4: CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

• Is used to store multiple value in a single identifier.• General array declaration syntax:

data-type array name [number of items]

For example:– float temp[5] = to store 5 float value in temp.– int max[4] = to store 4 integer value in max– char y[20] = to store a string of value

4

SINGLE-DIMENSIONAL ARRAYS

CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

Page 5: CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

5

SINGLE-DIMENSIONAL ARRAYS

CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

Each item in an array is called an element or component. Every item has its position in an array, this position is called

index or subscript; always start at 0. Example :

int num[10] store 10 data.

Note : int num[] error! int num[]={1,2,3} not error.

num[0] refer to the first data

num[1] refer to the second data

num[2] refer to the third data

Page 6: CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

6

SINGLE-DIMENSIONAL ARRAYS

CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

Input & output :

int num[100] // declarationcin >> num[20]; // to enter the 21th valuecout << num[20]; // to display the 21th

value

//using looping:for(int i=0; i<5; i++)cout << num[i];

Page 7: CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

7

SINGLE-DIMENSIONAL ARRAYS

CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

Input & output :

Array can be initialized within their declaration:

int temp[5]={1,2,3,4,5};char codes[6]={‘s’,’a’,’m’,’p’,’l’,’e’}; orchar codes[6]=“sample”;Note : a string is terminated with a special sentinel,“\0”

the null character.

Page 8: CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

8

SINGLE-DIMENSIONAL ARRAYS

CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

NOTE: If what we declare doesn’t fulfill what the size of the array

wants, for example:

int max[6]={1,2,3} we only declare three but the array wants 6; the compiler will

assume the next three integer are zero :

“ 1,2,3,0,0,0 “ If what we declare exceeds what the array wants, for example:

int max[3]={1,2,3,4,5,6,7,8} the array wants three but we declare 8” the compiler will only

take the first three integer :

“1,2,3”

Page 9: CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

9

SINGLE-DIMENSIONAL ARRAYS

CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

Example 3: How arrays are stored in the computer memory

Array list : int list[10];

Page 10: CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

10

SINGLE-DIMENSIONAL ARRAYS

CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

Accessing Array component : list[5]=34;

Page 11: CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

11

SINGLE-DIMENSIONAL ARRAYS

CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

Adding components in an array

List[3] = 10;

List[6] = 35;

List[5] = list[3] + list[6];

Page 12: CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

12

Processing single-dimensional arrays

CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

Some basic operations performed on a one-dimensional array are: Initialize Input data Output data stored in an array Find the largest and/or smallest element

Each operation requires ability to step through the elements of the array

Easily accomplished by a loop

Page 13: CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

13

Accessing array components

CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

Consider the declaration

int list[100]; //list is an array of the size 100int i;

This for loop steps-through each element of the array list starting at the first element

for (i = 0; i < 100; i++) //Line 1 //process list[i] //Line 2

Page 14: CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

14

Accessing array components

CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

If processing list requires inputting data into list :

the statement in Line 2 takes the form of an input statement, such as the cin statement

for (i = 0; i < 100; i++) //Line 1 cin >> list[i];

Page 15: CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

15

Accessing array components

CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

If processing list requires printing data from an array:

the statement in Line 2 takes the form of an output statement, such as the cout statement

for (i = 0; i < 100; i++) //Line 1 cout << list[i];

Page 16: CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

#include<iostream.h>int main (){

double myList[10], yourList[10];int index;

cout<<" Enter 10 numbers :";

for(index=0; index <10 ; index++){

cin>>myList[index]; //read component}

cout<<"\n The components inside the myList array are >> ";

for (index=0; index <10; index++){

cout<<myList[index]<< " "; //print component}

cout<<"\n\n The numbers inside the yourList array are >> ";

for (index=0; index <10; index++){

yourList[index] = myList[index]; //copy componentcout<<yourList[index]<< " ";

}cout<<"\n\n The numbers in yourList[5] is >> " <<yourList[5];

return 0;}

Example 1

Page 17: CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

17

Example 2 of Single-Dimensional Arrays

CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

#include <iostream.h>

Page 18: CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

18

Example 2 - Answer

CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

Page 19: CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

• Arrays are passed by reference only

• The symbol & is not used when declaring an array as a formal parameter

• The size of the array is usually omitted

19

Arrays as parameters to function

CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

Page 20: CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

• If the size of one-dimensional array is specified when it is declared as a formal parameter

– It is ignored by the compiler

• The reserved word const in the declaration of the formal parameter can prevent the function from changing the actual parameter

20

Arrays as parameters to function

CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

Page 21: CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

Example 1 :

Constant arrays as formal parameters :

21

Arrays as parameters to function

CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

Page 22: CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

22

Arrays as parameters to functionExample 2 :

Page 23: CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

23

Arrays as parameters to functionExample 2 :

Page 24: CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

24

Arrays as parameters to functionExample 3 :

Page 25: CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

25

Arrays as parameters to functionExample 4 :

Page 26: CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

26

Arrays as parameters to function

CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

Example 4 (cont’d) :

Page 27: CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

27

Arrays as parameters to function

CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

Example 4 (cont’d) :

Page 28: CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

28

Arrays as parameters to function

CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

Example 4 (cont’d) :

Page 29: CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

29

Arrays as parameters to function

CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING

Example 4 - Answer :