Array Lesson 1CS1313 Spring 2019
1
Array Lesson 1 Outline
Array Lesson 1 OutlineMean of a List of NumbersMean: DeclarationsMean: Greeting, InputMean: CalculationMean: OutputMean: Compile, RunMean: 5 Input ValuesMean: 7 Input ValuesMean: One Line DifferentMean: Compile, Run for 5Mean: Compile, Run for 7Scalars #1Scalars #2Another Scalar ExampleA Similar Program, with MultiplicationA Similar Program, with a TwistArraysArray Element Properties
Array Properties #1Array Properties #2Array Properties #3Array Properties #4Array Properties #5Array Indices #1Array Indices #2Multidimensional Arrays & 1D ArraysArray Declarations #1Array Declarations #2Array Declarations #3Assigning a Value to an Array ElementArray Element Assignment ExampleGetting Array Element Value withscanfArray ElementscanfExample #1Array ElementscanfExample #2for Loops for Tasks on Arrays #1for Loops for Tasks on Arrays #2Another for/Array Example #1Another for/Array Example #2Another for/Array Example #3Don’t Need to Use Entire Declared Length
Array Lesson 1CS1313 Spring 2019
2
Mean of a List of Numbers
Consider a list of real numbers of lengthnelements:x1,x2,x3, …,xnThemean(average) of this list is:(x1+x2+x3+ … +xn) /n
Array Lesson 1CS1313 Spring 2019
3
Mean: Declarations
#include <stdio.h>intmain (){ /* main */const floatinitial_sum= 0.0;constintnumber_of_elements= 5;constintfirst_element= 0;constintprogram_success_code= 0;floatinput_value[number_of_elements];float sum;float mean;intelement;
Array Lesson 1CS1313 Spring 2019
4
Mean: Greeting, Input
printf("I'm going to calculate the\n");printf(" mean of a list ");printf("of length %d values.\n",number_of_elements);printf("What are the %d values of the list?\n",number_of_elements);for (element =first_element;element <number_of_elements; element++) {scanf("%f", &input_value[element]);} /* for element */
Array Lesson 1CS1313 Spring 2019
5
Mean: Calculation
sum =initial_sum;for (element =first_element;element <number_of_elements; element++) {sum +=input_value[element];} /* for element */mean = sum /number_of_elements;
Array Lesson 1CS1313 Spring 2019
6
Mean: Output
printf("The %d input values of the list are:\n",number_of_elements);for (element =first_element;element <number_of_elements; element++) {printf("%f ",input_value[element]);} /* for element */printf("\n");printf("The mean of the %d values",number_of_elements);printf(" in the list is %f.\n",mean);returnprogram_success_code;} /* main */
Array Lesson 1CS1313 Spring 2019
7
Mean: Compile, Run
%gcc-o mean5 mean5.c%mean5I'm going to calculate themean of a list of length 5 values.What are the 5 values of the list?123.25 234.50 345.75 456.00 567.25The 5 input values of the list are:123.250000 234.500000 345.750000 456.000000 567.250000The mean of the 5 values in the list is 345.350006.
Array Lesson 1CS1313 Spring 2019
8
Mean: 5 Input Values
#include <stdio.h>intmain (){ /* main */const floatinitial_sum= 0.0;constintnumber_of_elements= 5;constintfirst_element= 0;constintprogram_success_code= 0;floatinput_value[number_of_elements];float sum;float mean;intelement;
Array Lesson 1CS1313 Spring 2019
9
Mean: 7 Input Values
#include <stdio.h>intmain (){ /* main */const floatinitial_sum= 0.0;constintnumber_of_elements= 7;constintfirst_element= 0;constintprogram_success_code= 0;floatinput_value[number_of_elements];float sum;float mean;intelement;
The rest of the program isEXACTLYTHE SAME!
Array Lesson 1CS1313 Spring 2019
10
Mean: One Line Different
%diff mean5.c mean7.c6c6< constintnumber_of_elements= 5;---> constintnumber_of_elements= 7;ThediffUnix command compares two files of text and shows which lines are different.The only statement that differs betweenmean5.candmean7.cis the declaration ofnumber_of_elements.
Array Lesson 1CS1313 Spring 2019
11
Mean: Compile, Run for 5
%gcc-o mean5 mean5.c%mean5I'm going to calculate themean of a list of length 5 values.What are the 5 values of the list?123.25 234.50 345.75 456.00 567.25The 5 input values of the list are:123.250000 234.500000 345.750000 456.000000 567.250000The mean of the 5 values in the list is 345.350006.
Array Lesson 1CS1313 Spring 2019
12
Mean: Compile, Run for 7
%gcc-o mean7 mean7.c%mean7I'm going to calculate themean of a list of length 7 values.What are the 7 values of the list?12.75 23.75 34.75 45.75 56.75 67.75 78.75The 7 input values of the list are:12.750000 23.750000 34.750000 45.750000 56.750000 67.750000 78.750000The mean of the 7 values in the list is 45.750000.
Array Lesson 1CS1313 Spring 2019
13
Scalars #1
%catscalar_names.c#include <stdio.h>intmain (){ /* main */intb, c, d, e, f;b = 0;c = 2;d = 4;e = 6;f = 8;printf("b = %d\n", b);printf("c = %d\n", c);printf("d = %d\n", d);printf("e = %d\n", e);printf("f = %d\n", f);return 0;} /* main */
%gcc-oscalar_names\scalar_names.c%scalar_namesb = 0c = 2d = 4e = 6f = 8Note that, in Unix, abackslashat the end of a Unix command line means: “continue this Unix command on the next line.”
Array Lesson 1CS1313 Spring 2019
14
Scalars #2
%cat scalar_names.c#include <stdio.h>int main (){ /* main */int b, c, d, e, f;b = 0;c = 2;d = 4;e = 6;f = 8;printf("b = %d\n", b);printf("c = %d\n", c);printf("d = %d\n", d);printf("e = %d\n", e);printf("f = %d\n", f);return 0;} /* main */
All of the variables in the program are simpleintvariables. Each of the individualintvariables has asinglename, asingleaddress, asingledata type and asinglevalue.Such variables, whether their type isint,float,charor whatever, are referred to asscalarvariables.
Array Lesson 1CS1313 Spring 2019
15
Another Scalar Example
%catscalar_a.c#include <stdio.h>intmain (){ /* main */inta0, a1, a2, a3, a4;a0 = 0;a1 = 2;a2 = 4;a3 = 6;a4 = 8;printf("a0 = %d\n", a0);printf("a1 = %d\n", a1);printf("a2 = %d\n", a2);printf("a3 = %d\n", a3);printf("a4 = %d\n", a4);return 0;} /* main */
%gcc-oscalar_a\scalar_a.c%scalar_aa0 = 0a1 = 2a2 = 4a3 = 6a4 = 8The only difference between this program and the previous program is the names of the scalar variables (and therefore some of the output).
Array Lesson 1CS1313 Spring 2019
16
A Similar Program, with Multiplication
%catscalar_mult.c#include <stdio.h>intmain (){ /* main */inta0, a1, a2, a3, a4;a0 = 0 * 2;a1 = 1 * 2;a2 = 2 * 2;a3 = 3 * 2;a4 = 4 * 2;printf("a0 = %d\n", a0);printf("a1 = %d\n", a1);printf("a2 = %d\n", a2);printf("a3 = %d\n", a3);printf("a4 = %d\n", a4);return 0;} /* main */
%gcc-oscalar_mult\scalar_mult.c%scalar_multa0 = 0a1 = 2a2 = 4a3 = 6a4 = 8Notice that, in this program, the values of the scalar variables are obtained by multiplying a constant by the number associated with the scalar variable.
Array Lesson 1CS1313 Spring 2019
17
A Similar Program, with a Twist
%catarray_mult.c#include <stdio.h>intmain (){ /* main */inta[5];a[0] = 0 * 2;a[1] = 1 * 2;a[2] = 2 * 2;a[3] = 3 * 2;a[4] = 4 * 2;printf("a[0] = %d\n", a[0]);printf("a[1] = %d\n", a[1]);printf("a[2] = %d\n", a[2]);printf("a[3] = %d\n", a[3]);printf("a[4] = %d\n", a[4]);return 0;} /* main */
%gcc -o array_mult \array_mult.c%array_multa[0] = 0a[1] = 2a[2] = 4a[3] = 6a[4] = 8Huh?
Array Lesson 1CS1313 Spring 2019
18
Arrays
inta[5];Anarrayis a special kind of variable. Like a scalar variable, an array has:a name;an address;a data type.But instead of an array having exactly one single value, it can havemultiple values.Each of these values is referred to as anelementof the array.If you’re familiar withvectorsin mathematics, you can think of an array as the equivalent idea, but in computing instead of in mathematics.
Array Lesson 1CS1313 Spring 2019
19
Array Element Properties
Each of theelementsof an array is just aboutexactly like a scalar variable of the same data type.Anelementof an array has:aname, which it shares with all of the other elements of the array that it belongs to;anaddress, which we’ll learn about shortly;adata type, which it shares with all of the other elements of the array that it belongs to;asingle value.But, anelementof an array also has:anindex, which we’ll learn about shortly.
Array Lesson 1CS1313 Spring 2019
20
Array Properties #1
inta[5];An arrayas a wholehas the following properties:It has adata type, which is the data type of each of its elements; for example,int.
Array Lesson 1CS1313 Spring 2019
21
Array Properties #2
inta[5];An arrayas a wholehas the following properties:It as adimensionattribute, sometimes called itslength, which describes thenumber of elementsin the array; for example,[5].
Array Lesson 1CS1313 Spring 2019
22
Array Properties #3
inta[5];An arrayas a wholehas the following properties:It has exactly as manyvaluesas it has elements, and in fact each of its elements contains exactly one of its values.
Array Lesson 1CS1313 Spring 2019
23
Array Properties #4
inta[5];An arrayas a wholehas the following properties:Its elements are accessed viaindexingwith respect to the variable name; for example,a[2] = 7;
Array Lesson 1CS1313 Spring 2019
24
Array Properties #5
int a[5];An arrayas a wholehas the following properties:Its elements arecontiguousin memory; for example,
?
a[0]
?
a[1]
?
a[2]
?
a[3]
?
a[4]
a[0]
????????
Address 12340
a[1]
????????
Address 12344
a[2]
????????
Address 12348
a[3]
????????
Address 12352
a[4]
????????
Address 12356
Array Lesson 1CS1313 Spring 2019
25
Array Indices #1
inta[5];We access a particular element of an array usingindexnotation:a[2]This notation is pronounced “aof2” or “asub2.”The number in square brackets – for example, the2ina[2]– is called theindexorsubscriptof the array element.Array indices are exactly analogous to subscript numbers in mathematics:a0, a1, a2, a3, a4
Array Lesson 1CS1313 Spring 2019
26
Array Indices #2
inta[5];An individual element of an array– for example,a[2]–has exactly the same properties as a scalar variable of the same data type– except for being accessed via indexing.Notice that the elements of an array are numbered from0through(length- 1); in the above example, the elements ofaarea[0],a[1],a[2],a[3],a[4]
Array Lesson 1CS1313 Spring 2019
27
Multidimensional Arrays & 1D Arrays
An array can havemultiple dimensions:intarray2d[8][5];For now, we’re going to concentrate on arrays with only one dimension.A one-dimensional array is sometimes called avector, because of the close relationship between arrays in computing and vectors in mathematics.A two-dimensional array is sometimes called amatrix.A three-dimensional array is sometimes called afield.
Array Lesson 1CS1313 Spring 2019
28
Array Declarations #1
The general form of an array declaration is:typearrayname1[dimension1],arrayname2[dimension2],... ;For example:inta[8], b[4], c[9];causes the compiler to set up threeintarrays in memory.
Array Lesson 1CS1313 Spring 2019
29
Array Declarations #2
inta[5], b[4], c[9];causes the compiler to set up threeintarrays in memory, like so:
?
a[0]
?
a[1]
?
a[2]
?
a[3]
?
a[4]
?
b[0]
?
b[1]
?
b[2]
?
b[3]
?
c[0]
?
c[1]
?
c[2]
?
c[3]
?
c[4]
?
c[5]
?
c[6]
?
c[7]
?
c[8]
Array Lesson 1CS1313 Spring 2019
30
Array Declarations #3
inta[8], b[4], c[9];In principle, these arrays could be remote from each other in memory (for example,acould start at address 12340,bcould start at address 67890 andccould start at address 981439294).In practice, they are usually contiguous or almost contiguous in memory; that is, the last byte of arrayawill typically be right next to the first byte of arrayb, and the last byte of arraybwill typically be right next to the first byte of arrayc.However, the compilerisn’t requiredto make the different arrays contiguous in memory.The only contiguity constraint is that,within each array, all of the elements are contiguous and sequential.
Array Lesson 1CS1313 Spring 2019
31
Assigning a Value to an Array Element
Because an individual array element is exactly analogous to a scalar variable, we can assign or input a value into it in exactly the same ways that we assign or input values into scalar variables.For example, we can use a scalar assignment for each individual element.
Array Lesson 1CS1313 Spring 2019
32
Array Element Assignment Example
%catarrayeltassn.c#include <stdio.h>intmain (){ /* main */inta[3];a[0] = 5;a[1] = 16;a[2] = -77;printf("a[0] = %d\n",a[0]);printf("a[1] = %d\n",a[1]);printf("a[2] = %d\n",a[2]);return 0;} /* main */
%gcc -o arrayeltassn \arrayeltassn.c%arrayeltassna[0] = 5a[1] = 16a[2] = -77
Array Lesson 1CS1313 Spring 2019
33
Getting Array Element Value withscanf
Just as we can assign a value to an individual array element, we can usescanfto obtain the value of each individual array element.
Array Lesson 1CS1313 Spring 2019
34
Array ElementscanfExample #1
#include <stdio.h>intmain (){ /* main */float a[3];printf("Input a[0],a[1],a[2]:\n");scanf("%f %f %f", &a[0], &a[1], &a[2]);printf("a[0] = %f\n", a[0]);printf("a[1] = %f\n", a[1]);printf("a[2] = %f\n", a[2]);return 0;} /* main */
Array Lesson 1CS1313 Spring 2019
35
Array ElementscanfExample #2
%gcc-oarrayeltreadarrayeltread.c%arrayeltreadInput a[0],a[1],a[2]:5.5 16.16 -770.770a[0] = 5.500000a[1] = 16.160000a[2] = -770.770020
Array Lesson 1CS1313 Spring 2019
36
forLoops for Tasks on Arrays #1
#include <stdio.h>intmain (){ /* main */const intnumber_of_elements= 5;const intprogram_success_code= 0;inta[number_of_elements];intcount;for (count = 0; count <number_of_elements; count++) {a[count] = 2 * count;} /* for count */for (count = 0; count <number_of_elements; count++) {printf("a[%2d] = %2d\n", count, a[count]);} /* for count */returnprogram_success_code;} /* main */
Array Lesson 1CS1313 Spring 2019
37
forLoops for Tasks on Arrays #2
%gcc -o array_for_mult array_for_mult.c%array_for_multa[ 0] = 0a[ 1] = 2a[ 2] = 4a[ 3] = 6a[ 4] = 8
Array Lesson 1CS1313 Spring 2019
38
Anotherfor/Array Example #1
#include <stdio.h>#include <stdlib.h>intmain (){ /* main */constintminimum_number_of_elements= 1;constintmaximum_number_of_elements= 15;constintprogram_failure_code= -1;constintprogram_success_code= 0;inta[maximum_number_of_elements];intnumber_of_elements;intcount;printf("How long will the array be (%d to %d)?\n",minimum_number_of_elements,maximum_number_of_elements);scanf("%d", &number_of_elements);if ((number_of_elements<minimum_number_of_elements) ||(number_of_elements>maximum_number_of_elements)) {printf("That’s not a valid array length!\n");exit(program_failure_code);} /* if ((number_of_elements< ...) || ...) */
Array Lesson 1CS1313 Spring 2019
39
Anotherfor/Array Example #2
for (count = 0; count <number_of_elements; count++) {a[count] = 2 * count;} /* for count */for (count = 0; count <number_of_elements; count++) {printf("a[%2d] = %2d\n", count, a[count]);} /* for count */returnprogram_success_code;} /* main */
Array Lesson 1CS1313 Spring 2019
40
Anotherfor/Array Example #3
%gcc-oarray_for_mult_readarray_for_mult_read.c%array_for_mult_readHow long will the array be (1 to 15)?0That’s not a valid array length!%array_for_mult_readHow long will the array be (1 to 15)?16That’s not a valid array length!%array_for_mult_readHow long will the array be (1 to 15)?5a[ 0] = 0a[ 1] = 2a[ 2] = 4a[ 3] = 6a[ 4] = 8
Array Lesson 1CS1313 Spring 2019
41
Don’t Need to Use Entire Declared Length
#include <stdio.h>intmain (){ /* main */constintminimum_number_of_elements= 1;constintmaximum_number_of_elements= 15;constintprogram_failure_code= -1;constintprogram_success_code= 0;inta[maximum_number_of_elements];...} /* main */...%array_for_mult_readHow long will the array be (1 to 15)?5a[ 0] = 0a[ 1] = 2a[ 2] = 4a[ 3] = 6a[ 4] = 8
Notice that we candeclarean array to belargerthan the portion of the array that we actually use, because RAM is cheap.
0
Embed
Upload