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

Slides:



Advertisements
Similar presentations
Topic Reviews For Unit ET156 – Introduction to C Programming Topic Reviews For Unit
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite.
Introduction to C Programming
Chapter 7: Arrays In this chapter, you will learn about
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Arrays. INTRODUCTION TO ARRAYS Just as with loops and conditions, arrays are a common programming construct and an important concept Arrays can be found.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
1 CS 201 Passing Function as Parameter & Array Debzani Deb.
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
Lab 11 rArrays rExercises Note: Read the whole Chapter 8.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
1 ICS103 Programming in C Lecture 12: Arrays I. 2 Outline Motivation for One-dimensional Arrays What is a One-dimensional Array? Declaring One-dimensional.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CPCS 202 Chapter 2 – Input/Output
 2006 Pearson Education, Inc. All rights reserved Arrays.
chap8 Chapter 8 Arrays (Hanly) chap8 2 Data Structure Simple data types use a simple memory to store a variable. Data Structure: a.
A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.
Chapter 8 Arrays and Strings
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Chapter 8 Arrays Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of.
+ ARRAYS - SEARCHING - SORTING Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
ARRAY Prepared by MMD, Edited by MSY1.  Introduction to arrays  Declaring arrays  Initializing arrays  Examples using arrays  Relationship with pointers.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
Chapter 8 Arrays Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Arrays Why we need data structure? Simple data types use a single memory cell to store a variable. Sometimes (for example scores of a class) it is more.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
(7-2) Arrays I H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (October 9, 2015) Washington State University.
ICS103 Programming in C Lecture 11: Arrays I
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Prepared by MMD, Edited by MSY1 CHAPTER 4 ARRAY. Prepared by MMD, Edited by MSY2 Arrays  Introduction to arrays  Declaring arrays  Initializing arrays.
Computer Programming for Engineers
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 6.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Unit 6 Data Types and Arrays. Key Concepts Explicit and automatic conversion ASCII Enumerated types Function parameters Arrays Loops and arrays Passing.
Arrays. Arrays are objects that help us organize large amounts of information.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Lecture 2. Algorithms and Algorithm Convention 1.
ARRAYS.
Computer Programming BCT 1113
Arrays and Records.
7 Arrays.
Presentation transcript:

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

© CHAPTER 8 - Arrays Chapter 8 1. Arrays 1a. 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 #

© Arrays A. 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

© Arrays Declaring A. 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 Types Numbers only Integer (int) Double (double) Character (char) 1a

© Arrays Manipulating ExplanationStatement Displays the value of x[0], which is 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 sum += x[2]; Adds 1.0 to x[3]. The new x[3] is x[3] += 1.0; Stores the sum of x[0] and x[1] in x[2]. The new x[2] is x[2] = x[0] + x[1]; 1b

© Arrays Manipulating ExplanationStatement 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 ( 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

© Arrays Declaring and Initializing A. 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

© Arrays and Loops A. 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][2][3][4][5][6][7][8][9] 2

© 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 Implementation ProblemAnalysis DesignOutline Testing Maintenance

© Calculating Mean and SD 1 Implementation ProblemAnalysis DesignOutline Testing Maintenance

© Calculating Mean and SD 1 Implementation ProblemAnalysis DesignOutline Testing Maintenance

© Arrays and Pointers A. 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; ‘b’ andy ‘a’‘c’ [0] [1] [2] ‘b’ fred ‘a’‘c’ [0] [1] [2] 1776 ted 3

© Arrays and Pointers Tutorial int 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 1 Way 2 Way 3 Way 4 Way 5 no pointer

© Arrays and Functions A. 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

© Arrays and Functions Arrays as Output Arguments A. 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

© Arrays and Functions Arrays as Output Arguments B. Example 4a

© Arrays and Functions Arrays as Input Arguments A. 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 int list[]) or test(x) 4b

© Arrays and Functions Arrays as Input Arguments B. Example 4b

© Arrays and Functions C. Example 4

© 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 Implementation ProblemAnalysis DesignOutline Testing Maintenance

© Simulating Stacks Write the main() function for this program? 2 Implementation ProblemAnalysis DesignOutline Testing Maintenance

© 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 Implementation ProblemAnalysis DesignOutline Testing Maintenance

© 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 Implementation ProblemAnalysis DesignOutline Testing Maintenance

© Array Search - Linear Search 3 Implementation ProblemAnalysis DesignOutline Testing Maintenance

© 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 Implementation ProblemAnalysis DesignOutline Testing Maintenance

© Sorting an Array 4 Implementation ProblemAnalysis DesignOutline Testing Maintenance

© 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

© Multidimensional 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

© 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 Implementation ProblemAnalysis DesignOutline Testing Maintenance

© Tic-Tac-Toe Implementation ProblemAnalysis DesignOutline Testing Maintenance 5

© 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 Implementation ProblemAnalysis DesignOutline Testing Maintenance 6

© Enrollment Data in 3D // Display number of students in each course for (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); } Implementation ProblemAnalysis DesignOutline Testing Maintenance 6

© Enrollment Data in 3D // Display number of students at each campus for (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); } Implementation ProblemAnalysis DesignOutline Testing Maintenance 6

© CHAPTER 8 - Arrays Chapter 8 1. Arrays 1a. 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