Download presentation
Presentation is loading. Please wait.
Published byClare Paul Modified over 7 years ago
1
Department of Computer Science Western Michigan University
CS 1023 Intro to Engineering Computing 3: Computer Programming LM5 – One-Dimensional Arrays Mark Kerstetter Department of Computer Science Western Michigan University © Spring 2013
2
Arrays – Lists or Vectors Tables or Matrices
Engineering uses subscripted names like xk , yj+1 , z10 , mij to represent lists, vectors, tables and matrices. arrays store groups of homogeneous data use when it is necessary to keep several related values in memory at the same time. Don’t use if not necessary! array names same naming rules as everything else E.g., temperature, elevation, direction, x_coord, y_coord array elements & array element names – indicated by [ ] temperature[0], elevation[100], direction[k], x_coord[p1], y_coord[p1], month[day[n]] element index (aka element subscript) must evaluate to an integer array element is an expression just like a simple variable Can be used in calculations Can be assigned a value Can be passed to a function as an actual paramter Can receive a value via scanf or fscanf Can print a value via printf or fprintf every element of an array is the same data type (i.e., homogeneous) E.g., all int, all double, all char, all long, etc. array processing arrays and loops are often used together xk becomes x[k] yj+1 becomes y[j+1] z10 becomes z[10] mij becomes m[i,j]
3
Arrays 1-D 1-D Array - Lists M T W R F S N Reserve Space Only!
Sample Declarations int quiz[5] ; double gpa[150] ; int monthL[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} ; char day[7] = {‘M’, ‘T’, ‘W’, ‘R’, ‘F’, ‘S’, ‘N’} ; Reserve + Initialize gpa[0] gpa[1] gpa[2] gpa[3] gpa[147] gpa[148] gpa[149] quiz[0] quiz[1] quiz[2] quiz[3] quiz[4] 31 28 30 monthL [0] monthL [1] monthL [2] monthL[3] monthL[4] monthL[5] monthL[6] monthL[10] monthL[11] M T W R F day[0] day[1] day[2] day[3] S N day[4] day[5] day[6]
4
Arrays 2-D 2-D Array - Matrices or Tables
Terminology: dimensions, rows, and columns double year[52, 7] ; /* 52 rows representing weeks of a year */ int quiz[4,15] ; /* 4 quizzes up to 15 questions per quiz * 7 columns 52 rows 364 array elements of double precision numbers 15 columns 4 rows
5
Parallel Arrays
Use several arrays of the same dimension, i.e., size and shape, to hold related information of the same or differing data types E.g., char *name[150], char *ltrGrade[150], double gpa[150] ; E.g., int x[20], y[20], z[20] ; x y z [0] [1] [2] [3] [4] [5] [19] [18] name[0] name[1] name[2] name[3] name[147] name[148] name[149] ltrGrade[0] ltrGrade[1] ltrGrade[2] ltrGrade[3] ltrGrade[147] ltrGrade[148] ltrGrade[149] gpa[0] gpa[1] gpa[2] gpa[3] gpa[147] gpa[148] gpa[149]
6
Arrays 1-D 1-D Array E.g., int quiz[5] ; /* 1-D integer array declaration */ What’s the name of the 1st element in the array called quiz? the last element? Answers: quiz[0] and quiz[4] How can we initialize the elements of quiz to contain all zeros? Answer: int quiz[5] = {0} ; How can we initialize the elements of quiz to contain all 32’s? Answer: int quiz[5] = {32, 32, 32, 32, 32 } ; E.g., double gpa[150] ; /* 1-D double precision array declaration */ What’s the name of the 1st element in the array called gpa? The last element? Answer: gpa[0] and gpa[149] How can we initialize the elements of gpa to contain all zeros? Answer: double gpa[150] = {0} ; How can we initialize the elements of quiz to contain all 4.00’s? Answer: for (k=0; k<=149; k++) { gpa[k] = 4.00 } ;
7
Passing Arrays as Function Parameters
Declare the array(s) you want to use in the main (or calling) program double arr1[100], arr2[100] ; Create a programmer-defined function that uses an array of the same data type. void copy_array(double a[], double b[], int len) { } Copy the heading line of the programmer-defined function to the appropriate declaration area and add a trailing semicolon. void copy_array(double a[], double b[], int len) ; Use the array name, without square brackets, as the actual parameter corresponding to the array. copy_array(arr1, arr2, 47) ;
8
Passing Arrays as Function Parameters Syntax of Array Formal Parameters
Create a programmer-defined function that uses an array of the same data type. void copy_array(double a[], double b[], int len) { } Array parameters refer to entire arrays, not single array elements Array parameters are always pass-by-reference because multiple elements are passed through one parameter. Array parameters have special pass-by-reference syntax double a[] not double[] a not double *a[] syntax is … <type> <name> []
9
Passing Arrays as Function Parameters Syntax of Array Actual Parameters
Using an array as an actual parameter. Array parameters are always pass-by-reference because multiple elements are passed through one parameter. Just use the array name (without square brackets) copy_array(arr1, arr2, 47) ;
10
Area of a Polygon (x0,y0) (x1,y1) (x2,y2) (x3,y3) (x4,y4) (x5,y5) (x0,y0) (x1y1) (x2y2) (x3y3) (x4y4) (x5y5) Initial Draft Solution: input vertex points of polygon print the vertex points compute the polygon’s area using the vertex points print the polygon’s area
11
Characters character data type: char
Each memory location contains exactly one character Character constants – single characters enclosed in single quotes ‘M’ is the upper case character M and ‘m’ is the lower case character m ‘3’ is the character 3, while 3 (without single quotes) is the number 3, an integer. Integers and characters are stored differently. You can do arithmetic with integers, but not with characters! ‘ ‘ is the space character and ‘’ is the empty or null character. ‘\’ ’ is the single quote character (See Escape Character below.) Escape Character is \ (back slash) used to represent non-printing (invisible) characters, like newline (\n), tab (\t), backspace (\b), alarm (\a), newpage (\f), etc. used to “quote” the single character that follows the \ so the character temporarily does not have its programmed meaning E.g., to print a double-quote character within an I/O control string use \” to represent the single-quote character as a character constant use \’
12
Arrays Characters char day[7] = {‘M’, ‘T’, ‘W’, ‘R’, ‘F’, ‘S’, ‘N’} ;
What would result from … printf (“Day %i of the week is %c\n”, 2, day[1] ) ; Day 2 of the week is T char compass_rose[4] = {‘N’, ‘S’, ‘E’, ‘W’} ; What would result from … printf (“\”West\” on a compass is \”%c\”\n”, compass_rose[3] ) ; “West” on a compass is “W”. How to display a table using tabs ( \t ) for (d = 0; d <= 360; d+=15) { r = d * pi/180 ; printf(“%4i\t%f\t%f\%f\n”, d, sin(d), cos(d), tan(d) ) ; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.