problem solving and program design in c (5th edition) by jeri r. hanly and elliot b. koffman

34
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 8 11-10-1429

Upload: keely

Post on 14-Feb-2016

15 views

Category:

Documents


0 download

DESCRIPTION

Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman. CP 202 Chapter 8. 11-10-1429. CHAPTER 8 - Arrays. #. Arrays. 1. Introduction An array is a collection of two or more adjacent memory cells that are associated with a particular symbolic name. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

Problem Solving and Program Design in C

(5th Edition)

by Jeri R. Hanly and Elliot B. Koffman

CP 202Chapter 8

11-10-1429

Page 2: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAU

CHAPTER 8 - ArraysChapter 8

1. Arrays1a.

Declaring

1b. Manipulating

1c. Declaring and Initializing

2. Arrays and Loops

3. Arrays and Pointers

4. Arrays and Functions

4a. Arrays as Output

Arguments

4b. Arrays as Input Arguments

5. Multidimensional Arrays

#

Page 3: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAU

ArraysA. Introduction

An array is a collection of two or more adjacent memory cells that are associated with a particular symbolic name.

B.Subtopics Declaring arrays Manipulating arrays Declaring and initializing arrays

1

Page 4: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAUArrays

DeclaringA. Introduction

Declaring arrays is similar to declaring variables, but declaring arrays requires declaring the name of the array and the number of the cells associated with it.

B.Syntax

C.Example int id[3]; double dollar[5]; char gender[2];

Data TypesNumbers

onlyInteger

(int)Doubl

e (double)

Character

(char)type array_name[size] ;

1a

Page 5: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAUArrays

Manipulating

Explanation StatementDisplays the value of x[0], which is 16.0. printf(“%.1f”, x[0]);

Stores the value 25.0 in x[3]. x[3] = 25.0;

Stores the sum of x[0] and x[1], which is 28.0 in the variable sum.

sum = x[0] + x[1];

Adds x[2] to sum. The new sum is 34.0. sum += x[2];

Adds 1.0 to x[3]. The new x[3] is 26.0. x[3] += 1.0;

Stores the sum of x[0] and x[1] in x[2].The new x[2] is 28.0.

x[2] = x[0] + x[1];

1b

Page 6: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAUArrays

ManipulatingExplanation Statement

i = 5;

Displays 4 and 2.5 ( value of x[4]) printf(“%d %.1f”, 4, x[4]);

Displays 5 and 12.0 ( value of x[5]) printf(“%d %.1f”, i, x[i]);

Displays 13.0 ( value of x[5] plus1) printf(“%.1f”, x[i] +1);

Displays 17.0 ( value of x[5] plus5) printf(“%.1f”, x[i] +i);

Displays14.0 ( value of x[6]) printf(“%.1f”, x[i +1]);

Invalid. Attempt to display x[10] printf(“%.1f”, x[i +i]);

Invalid. Attempt to display x[10] printf(“%.1f”, x[2 * i]);

Displays -54.5 ( value of x[7]) printf(“%.1f”, x[2 * i-3]);

Displays 6.0 ( value of x[2]) printf(“%.1f”, x[(int) x[4]]);

Displays12.0 ( value of x[5]); then assign 6 to I printf (“%.1f”, x[i++]);

Assigns 5(6-1) to i and then displays 12.0 (value of x[5]) printf (“%.1f”, x[--i]);

Assigns 12.0 (value of x[5]) to x[4] x[ i – 1] = x[i];

Assigns 14.0 (value of x[6]) to x[5] x[i] = x[i+1];

Illegal assignment statement x[i] – 1 = x[i];

1b

Page 7: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAUArrays

Declaring and InitializingA. Introduction

Arrays can be initialized when they are declared as same as variables

B.Example int prime[] = {2, 3, 5, 7, 11, 13, 17, 19}; char vowels[] = {‘A’, ‘E’, ‘I’, ‘O’, ‘U’};

1c

Page 8: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAU

Arrays and LoopsA. Introduction

If you wish to process the elements of an array in sequence, you can use loop

Note: any array starts with element zero (not one)B.Example

Write a fragment of a program that stores the squares of the integers 0 through 9 ?

int square[10], i;for (i=0; i<10; ++i)

square[i] = i * i;

0 1 4 9 16 25 36 49 64 81[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

2

Page 9: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAU

Calculating Mean and SD Write a program that computes the mean and

standard deviation of an array of data and displays the difference between each value and the mean.

1

ImplementationProblem Analysis Design Outline Testing Maintenance

Page 10: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAU

Calculating Mean and SD 1

ImplementationProblem Analysis Design Outline Testing Maintenance

Page 11: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAU

Calculating Mean and SD 1

ImplementationProblem Analysis Design Outline Testing Maintenance

Page 12: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAU

Arrays and PointersA.Without arrays:int andy, *ted;andy = 25;fred = andy;ted = &andy;

B.With arrays:char andy[]={‘a’,‘b’,‘c’};char fred[3];char *ted;fred[0] = andy[0];fred[1] = andy[1];fred[2] = andy[2];ted = andy;

1775 1776 1777 1778 1779

‘b’

andy

‘a’ ‘c’

[0] [1] [2]

‘b’

fred

‘a’ ‘c’

[0] [1] [2] 1776

ted

3

Page 13: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAUArrays and Pointers

Tutorialint main (void) {

char letters[5], char *p;int n;p = letters; *p = ‘a’; // ≡ letters[0]=‘a’p++;*p = ‘b’; // ≡ letters[1]=‘b’p = &letters[2]; *p = ‘c’; // ≡ letters[2]=‘c’p = letters + 3; *p = ‘d’; // ≡ letters[3]=‘d’p = letters; *(p+4) = ‘e’; // ≡ letters[4]=‘e’ for (n=0; n<5; n++)

printf(“%c”, letters[n]);return (0);

}

letters

[0] [1] [2] [3] [4]

*p

‘b’‘a’ ‘c’ ‘d’ ‘e’

Way

1W

ay 2

Way

3W

ay 4

Way

5no

po

inte

r

1775 1776 1777 1778 1779

Page 14: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAU

Arrays and FunctionsA. Introduction

Previous examples in functions show that the arguments are variables, so changing their values inside the functions will not affect the original values in the main function

However, using pointers allows you to send the address of a variable instead of the value of a variable, so the changing will take affect

Sending an array as an argument requires sending the address of the first element in the array

B.Subtopics Arrays as output arguments Arrays as input arguments

4

Page 15: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAUArrays and Functions

Arrays as Output ArgumentsA. Introduction

In simple (not array) parameters:void test(int *list) {

*list = 10;}int main(void) {

int x;test(&x);

} In array parameters:void test(int *list) {

*(list+5) = 10;}int main(void) {

int x[10];test(&x[0]);

}

or test(int list[])

or test(x)

or list[5] = 10;

4a

Page 16: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAUArrays and Functions

Arrays as Output ArgumentsB.Example

4a

Page 17: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAUArrays and Functions

Arrays as Input ArgumentsA. Introduction

In simple (not array) parameters:void function test(int list) {

:}int main(void) {

int x;test(x);

} In array parameters:void test(const int *list) {

:}int main(void) {

int x[10];test(&x[0]);

}

or test(

const in

t list[]

)

or test(x)

4b

Page 18: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAUArrays and Functions

Arrays as Input ArgumentsB.Example

4b

Page 19: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAU

Arrays and FunctionsC.Example

4

Page 20: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAU

Simulating Stacks A stack is a data structure in which only the

top element can be accessed.

Inserting a new element at the top of a stack is called push.

Removing the top element of a stack is called pop.

2

ImplementationProblem Analysis Design Outline Testing Maintenance

Page 21: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAU

Simulating Stacks

Write the main() function for this program?

2

ImplementationProblem Analysis Design Outline Testing Maintenance

Page 22: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAU

Array Search - Linear Search Search for a value inside an array. The simple process is to check each element

in the array using a loop and exit the loop if the value is found or you finished checking all the elements. This process is called a linear search.

3

ImplementationProblem Analysis Design Outline Testing Maintenance

Page 23: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAU

Array Search - Linear Search Assume the target has not been found. Start with the initial array element. Repeat while the target is not found and there

are more array elements. If the current element matches the target

Set a flag to indicate that the target has been found. else

Advance to the next array element. If the target was found

Return the target index as the search result. else

Return -1 as the search result.

3

ImplementationProblem Analysis Design Outline Testing Maintenance

Page 24: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAU

Array Search - Linear Search 3

ImplementationProblem Analysis Design Outline Testing Maintenance

Page 25: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAU

Sorting an Array Start from the first

data element until the previous of the last one and do the following:

Search for the smallest element from the current data element until the last one [subarray]. If you find one,

exchange it with the current data element.

4

ImplementationProblem Analysis Design Outline Testing Maintenance

Page 26: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAU

Sorting an Array 4

ImplementationProblem Analysis Design Outline Testing Maintenance

Page 27: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAU

Multidimensional Arrays Multi-dimensional arrays are arrays with two

or more dimensions. Example of two dimensions:

Example of three dimensions: int cube[100][5][4];cube[0][2][3] = 2;

char jimmy[3][5];

5

Page 28: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAUMultidimensional Arrays

Declaring and Initializing One Dimension arrays:

int prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};

char vowels[] = {‘A’, ‘E’, ‘I’, ‘O’, ‘U’};

Two Dimensions arrays:

int words[2][3] = { {‘a’,’n’,’d’}, {‘o’,’r’,’ ’}};

5

Page 29: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAU

Tic-Tac-Toe Write a function to check whether a tic-tac-toe

board is completely filled, assuming the original value for each cell is one space?

ttt_brd[1][2]

5

ImplementationProblem Analysis Design Outline Testing Maintenance

Page 30: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAU

Tic-Tac-ToeImplementationProblem Analysis Design Outline Testing Maintenance

5

Page 31: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAU

Enrollment Data in 3D Assuming that you have three-dimensional

array, called enroll, that may be used to store the enrollment data for a college.

The college offers 100 courses at five different campuses. We will number the freshman year 0, the sophomore year 1,… 3

Write the code for: Display number of students in each course Display number of students at each campus

ImplementationProblem Analysis Design Outline Testing Maintenance

6

Page 32: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAU

Enrollment Data in 3D// Display number of students in each coursefor (course = 0; course < 100; course++) {

course_sum = 0;for (campus = 0; campus < 5; campus++) {

for (cls_rank = 0; cls_rank < 4; cls_rank++) {

course_sum += enroll[course][campus][cls_rank];

}}printf(“Number of students in course %d is %d\

n”, course, course_sum);

}

ImplementationProblem Analysis Design Outline Testing Maintenance

6

Page 33: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAU

Enrollment Data in 3D// Display number of students at each campusfor (campus = 0; campus < 5; campus++) {

campus_sum = 0;for (course = 0; course < 100; course++) {

for (cls_rank = 0; cls_rank < 4; cls_rank++) {

campus_sum += enroll[course][campus][cls_rank];

}}printf(“Number of students at campus %d is %d\

n”, campus, campus_sum);}

ImplementationProblem Analysis Design Outline Testing Maintenance

6

Page 34: Problem Solving and Program Design in C  (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

© F

CIT@

KAU

CHAPTER 8 - ArraysChapter 8

1. Arrays1a.

Declaring

1b. Manipulating

1c. Declaring and Initializing

2. Arrays and Loops

3. Arrays and Pointers

4. Arrays and Functions

4a. Arrays as Output

Arguments

4b. Arrays as Input Arguments

5. Multidimensional Arrays

C