Arrays H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 13, 2011) Washington State University.

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

Chapter 8: Arrays.
Programming and Data Structure
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
O-1 University of Washington Computer Programming I Lecture 14: Arrays © 2000 UW CSE.
Kernighan/Ritchie: Kelley/Pohl:
Recursion H&K Chapter 10 Instructor – Gokcen Cilingir Cpt S 121 (July 21, 2011) Washington State University.
Modular Programming (2) H&K Chapter 6 Instructor – Gokcen Cilingir Cpt S 121 (July 8, 2011) Washington State University.
C Language Elements (II) H&K Chapter 2 Instructor – Gokcen Cilingir Cpt S 121 (June 22, 2011) Washington State University.
Strings (II) H&K Chapter 9 Instructor – Gokcen Cilingir Cpt S 121 (July 20, 2011) Washington State University.
Recursion (II) H&K Chapter 10 Instructor – Gokcen Cilingir Cpt S 121 (July 25, 2011) Washington State University.
Structs H&K Chapter 11 Instructor – Gokcen Cilingir Cpt S 121 (July 18, 2011) Washington State University.
Iteration in C H&K Chapter 5 Instructor – Gokcen Cilingir Cpt S 121 (July 1, 2011) Washington State University.
Selection structures – logical expressions and if statements H&K Chapter 4 Instructor – Gokcen Cilingir Cpt S 121 (June 28, 2011) Washington State University.
Arrays (III) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 15, 2011) Washington State University.
Data Types H&K Chapter 7 Instructor – Gokcen Cilingir Cpt S 121 (July 12, 2011) Washington State University.
Selection structures in C (II) H&K Chapter 4 Instructor – Gokcen Cilingir Cpt S 121 (June 30, 2011) Washington State University.
Arrays (II) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 14, 2011) Washington State University.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
O-1 University of Washington Computer Programming I Lecture 14: Arrays © 2000 UW CSE.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
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.
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.
Programming Arrays. Question Write a program that reads 3 numbers from the user and print them in ascending order. How many variables do we need to store.
Programming Arrays. Example 1 Write a program that reads 3 numbers from the user and print them in reverse order. How many variables do we need to store.
Chapter 8 Arrays and Strings
Problem: A company employs a group of fifty salespersons (with reference numbers ) who are paid commission if their individual sales exceeds two-thirds.
(4-2) Selection Structures in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 16, 2015) Washington State University.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter
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 Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
(2-2) Functions I H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (Spetember 9, 2015) Washington State University.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
(5-1) Selection Structures III in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 21, 2015) Washington State University.
(4-3) Selection Structures II in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 18, 2015) Washington State University.
Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.
(9-1) Strings I H&K Chapter 8 Instructor - Andrew S. O’Fallon CptS 121 (October 19, 2015) Washington State University.
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
(13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
(3-1) Functions II H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (September 9, 2015) Washington State University.
(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.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
CSE 251 Dr. Charles B. Owen Programming in C1 Intro to Arrays Storing List of Data.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
(6-2) Iteration in C II H&K Chapter 5 Instructor - Andrew S. O’Fallon CptS 121 (February 19, 2016) Washington State University.
Data Types H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (March 4, 2016) Washington State University.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Introduction to programming in java Lecture 21 Arrays – Part 1.
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Data Storage So far variables have been able to store only one value at a time. What do you do if you have many similar values that all need to be stored?
ARRAYS.
Arrays and Records.
EKT150 : Computer Programming
(1-1) C Review: Pointers, Arrays, Strings, & Structs
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
(1 - 2) Introduction to C Data Structures & Abstract Data Types
ICS103 Programming in C Lecture 12: Arrays I
Presentation transcript:

Arrays H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 13, 2011) Washington State University

Motivation example Problem statement: Receive grades from the user until user enters a negative grade (cue for stopping; sentinel value) and compute&display the difference between each grade and the mean of the grades. Assume at max 100 grades will be given. Related formula: Mean = (Grade 1 + Grade 2 +….+ Grade n )/n How to do it? ◦ We’d like to calculate the mean first and then go over each grade to calculate the difference from mean (by looping through them perhaps) ◦ We need to store these grades in some place we can access later to calculate the differences from mean. It would be best if we can store them in an organized way that we can access them efficiently

What is an array? An array is a data structure ◦ A data structure is a way of storing and organizing data in memory so that it may be accessed and manipulated efficiently An array is a sequence of items (of the same data type and of the same size) that are contiguously allocated in memory Arrays are declared in much the same way as variables: int a[6]; declares an array a with 6 cells that hold integers: Notice that array indexing begins at 0. C. Hundhausen, A. O’Fallon a[0]a[1]a[2]a[3]a[4]a[5]

Declaring and manipulating arrays We can declare arrays alongside simple variables: int students[100], count, teachers[50]; double gpa[100], average; char ch, name[100]; /*char arrays have a special name: string*/ Assuming this array: all of the following statements are valid: a[0] = 4; /* changes the value of a[0] from 10 to 4 */ a[2] += 2; /* sets the value of a[2] to 2 */ a[5] = a[3] – a[4]; /* sets the value of a[5] to 88 */ a[0]a[1]a[2]a[3]a[4]a[5] C. Hundhausen, A. O’Fallon

5 Array Subscripts We can do arithmetic on array subscripts! Assume this array: Then all of the following are valid: int x = 2; printf("%d", a[x + 2]); /* a[4] == 1 */ printf("%d", a[2 * x – 1]); /* a[3] == 89 */ printf("%d", a[x] – a[x-1]); /* -12 */ printf("%d", a[++x]); /* a[3] == 89; x == 3 */ a[x – 1] = a[x – 2]; /* assigns 12 to a[2] */ printf("%d", a[x + 4]); /* Does a[7] exist? */ a[0]a[1]a[2]a[3]a[4]a[5]

C. Hundhausen, A. O’Fallon6 Initializing Arrays We can initialize arrays at the time we declare them. Just as int count = 0; is valid, so too is int student_id[] = {3423, 8794, 4595, 1423, 4311, 5153, 9182, 1481, 1253, 1222, 2521, 2251, 2111}; Notice how you can omit the size of the array; the compiler deduces the size from the number of values listed.

Initializing Arrays (2) We can initialize arrays after we declared the array, using a loop: #define SIZE_LIMIT 100 int a[SIZE_LIMIT],i; for(i = 0; i < SIZE_LIMIT; i++) { a[i] = 0; } It’s common to define a constant that holds the max number of items we anticipate to store in an array Initializing array a by setting every cell of this array to zero

C. Hundhausen, A. O’Fallon8 Example Problem statement: Write a segment of code that creates an array of 10 double values, populates the array with the values 1.0 through 10.0, and finally exchanges the 1 st and 10 th values.

9 Example – cont’d #define ARRAY_SIZE 10 int main(void) { double array[ARRAY_SIZE]; //double array[] = {1.0, 2.0, 3.0, 4.0, 5.0, //6.0, 7.0, 8.0, 9.0, 10.0}; double value = 1.0, temp; int i = 0; //initializing the array for(i = 0; i < ARRAY_SIZE; i++) { grades[i] = value; value += 1.0; } //swapping first and last element temp = array[0]; /*last element would be at index ARRAY_SIZE -1*/ array[0] = array[ARRAY_SIZE -1]; array[ARRAY_SIZE -1] = temp; }

10 Motivation example - revisited We often need to process each element of an array successively ◦ Recall motivation example: Computing the difference from the mean for a group of grades We can accomplish this with a counter loop that goes from 0 to one less than the array size, inclusive.

11 Motivation example solution (1) #include #define SIZE_LIMIT 100 #define SENTINEL -1 void print_array (double arr[], int size); void print_differences (double arr[], int size, double mean); double calculate_mean (double arr[], int size); //assumed: user always enters double formattable input int main (void) { int i = 0, size = 0; double grades[SIZE_LIMIT], mean = 0.0; printf("Please enter grades, and when you finish enter -1 to indicate that's the end of the grades (max 100 grades)\n"); //storing all the grades inputted in grades array i=0; scanf("%lf", &grades[i]); while(grades[i] != SENTINEL && i < SIZE_LIMIT){ i++; scanf("%lf", &grades[i]); } size = i; // set size of the array portion used

//control function, just to see if inputting part was successful //print_array(grades,size); //calculate&display mean mean = calculate_mean(grades,size); printf("Mean is %.2lf\n", mean); //print differences from mean print_differences(grades, i, mean); return 0; } void print_array(double arr[], int size) { int i = 0; for(i = 0; i< size; i++) printf("%.2lf ", arr[i]); printf("\n"); } Motivation example solution (2)

void printDifferences (double arr[], int size, double mean) { int i = 0; for(i = 0; i< size; i++) printf("%.2lf %.2lf\n", arr[i], arr[i] - mean); } double calculate_mean (double arr[], int size) { double mean = 0.0; int i=0; for(i = 0; i< size; i++) mean += arr[i]; mean /= size; return mean; } Motivation example solution (2)

C. Hundhausen, A. O’Fallon14 Parallel Arrays (1) Often, we'd like to associate the values in one array with those in another array ◦ A list of student numbers, together with their class standings, for example We can declare parallel arrays to accomplish this: #define NUM_STUDENTS 100 typedef enum {freshman, sophomore, junior, senior} class_t; int id[NUM_STUDENTS]; class_t class[NUM_STUDENTS];

C. Hundhausen, A. O’Fallon15 Parallel Arrays (2) The parallel arrays of student numbers and class standings might look something like this: id[0]id[1]id[2]id[3]id[4]id[5] freshmanseniorjunior sopho- more juniorfreshman class[0]class[1]class[2]class[3]class[4]class[5]

16 References J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (6 th Ed.), Addison- Wesley, 2010 P.J. Deitel & H.M. Deitel, C How to Program (5 th Ed.), Pearson Education, Inc., 2007.