Download presentation
Presentation is loading. Please wait.
Published byClaribel Watson Modified over 8 years ago
1
142 M -1 Arrays Chapter 8 Motivation: to deal with large amounts of data e.g sorting values: Input: 10, 15, 4, 15, 17, 3, 12, 36, 48, 32, 9, 21 Want the Output: 3, 4, 9, 10, 12, 15, 17, 21, 25, 32, 36, 48 averaging grades: with what we know double grade1, grade2, grade3; double grade4, grade5, grade6; double grade7, total;... /* initialize the grades */... total = grade1+grade2+grade3+grade4 +grade5+grade6+grade7; printf("average=%.2f\n",total/7.0); is there a better way?
2
Data Structure 142 M -2 We have functions to organize the program We also need a data structure to organize data large amounts of data variable amounts of data sets of data where the individual pieces of data have special relationships to one another e.g. data related to an employee age, SSN, duration of employment For this C offers: arrays structs combination of arrays and structs especially for
3
142M -3 Arrays Definition: A named, ordered, collection of identical type Example grades for 7 students Name the collection grade Number the elements of the collection 0 3.0 3.8 1.7 2.0 2.5 2.1 3.2 1 2 3 4 5 6 start at 0 C expressions grade[0] is 3.0 grade[6] is 3.2 2.0*grade[3] is 4.0
4
142 M -4 Program example double grade[7], total; int i; /*initialize the grades*/ for (i=0; i<7; i++) { printf("Enter grade %i: ",i+1); scanf("%lf",&grade[i]); } /* average */ total = 0; for (i=0; i<7; i++) total = total + grade[i]; printf("\n average=%.2f",total/7.0); array declaration grade[i] is a regular double variable
5
142 M - 5 Array Terminology type name[size] array declaration size MUST be a constant double grade[7]; e.g. grade is of type array of double with size 7 grade[0], grade[1],..., grade[6] are the elements of the array grade. Each is of type double. 0, 1,...,6 are the indices of the array. Also called subscripts The bounds are the lowest and highest values of the subscripts (here 0 and 6)
6
142 M -6 Names of Arrays Array names are identifiers Use the same rules as for function and variable names names MUST start with a letter names MUST be declared before they are used... if you read in a program: x[y] x must be the name of an array y must have an integer value
7
Some rules(1) 142 M -7 Index rule: An array index must evaluate to an int between 0 and n-1 where n is the number of elements of the array No Exceptions Example: double grade[7];... grade[i+3+k] = 3.0; Can have complicated expressions: grade[(int)(3.1*2.71828*sin(2.*PI))] grade[i] OK if 0 i+3+k 6
8
Some rules(2) Element rule: An array element can be used wherever a simple variable of same type can be used No Exceptions Example: scanf("%lf",&grade[i]); grade[i]=sin(2.0*PI*sqrt(32.1)); grade[i] 142 M -8
9
142 M -9 Program Example Goal: Get the grades of a class (the class size may vary) Print the average grade Print the grades above the average How? Have an array double grade[CLASS_SIZE_MAX]; #define constant Ask for the size of the class: class_size Check that class_size<=CLASS_SIZE_MAX Get the grades ( for loop) compute the average ( for loop) display the average and the grades above average ( for loop)
10
142 M -10 Using several arrays Computing a course grade: Need to compute an average between the Midterm and the Final Use three arrays double midterm[CLASS_SIZE_MAX]; double final[CLASS_SIZE_MAX]; double course[CLASS_SIZE_MAX]; parallel arrays And to compute the weighted average: for(i=0; i < class_size; i++) { course[i] = MT_WEIGHT*midterm[i] + FINAL_WEIGHT*final[i]; } MT_WEIGHT and FINAL_WEIGHT are #define constants equal for instance to 0.3 and 0.7
11
Shifting elements of an array 142 M -11 Goal: Given an array x, shift x[0], x[1],..., x[n-1] by one position upward to make space for a new element at x[0] Insert the new value at x[0] Update the dimension n of the array How? Check that x is big enough to receive a new element. Move x[k] into x[k+1] (k=n-1, n-2,...,0) Get x[0] Update n
12
In C #include #define SIZE_MAX 20 int main(void) { int x[SIZE_MAX],size,k,done=0; x[0]=1; x[1]=2; x[2]=3; size=3; while(!done && size<=SIZE_MAX) { /* shift x */ for(k=size-1; k>=0; k--) x[k+1] = x[k]; /* get x[0] */ printf("new element: "); scanf("%i",&x[0]); size = size + 1; /* again? */ printf("stop? (yes=1) "); scanf("%i",&done); } for(k=0; k<size; k++) printf("x[%i]=%i\n",k,x[k]); return EXIT_SUCCESS; } 142 M -12
13
Pitfalls of Arrays 142 M -13 double array1[SIZE], array2[SIZE]; some #define constant previously defined Suppose array1 has been initialized: Cannot do: array2 = array1; Cannot do: if (array1 == array2)... Cannot do: use scanf to directly read an array scan("??",&array1); But can do this on array elements Write your own functions
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.