Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Arrays H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 13, 2011) Washington State University."— Presentation transcript:

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

2 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

3 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 1012089191 a[0]a[1]a[2]a[3]a[4]a[5]

4 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 */ 1012089191 a[0]a[1]a[2]a[3]a[4]a[5] C. Hundhausen, A. O’Fallon

5 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? */ 1012089191 a[0]a[1]a[2]a[3]a[4]a[5]

6 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.

7 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

8 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 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 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 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

12 //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)

13 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)

14 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];

15 C. Hundhausen, A. O’Fallon15 Parallel Arrays (2) The parallel arrays of student numbers and class standings might look something like this: 123031246534784235239785 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 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.


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

Similar presentations


Ads by Google