One-Dimensional Array Introduction Lesson xx

25
One-Dimensional Array Introduction Lesson xx

description

One-Dimensional Array Introduction Lesson xx. Objectives. One-dimensional array concept Array declaration Initializing arrays Operation with arrays Constant and variable subscripts Advantage of using arrays Program using arrays. Array Illustration & Definition. y. Array Declaration. - PowerPoint PPT Presentation

Transcript of One-Dimensional Array Introduction Lesson xx

Slide 1

One-Dimensional Array IntroductionLesson xx

In this presentation, well introduce you to one dimensional arrays.1ObjectivesOne-dimensional array conceptArray declaration Initializing arraysOperation with arraysConstant and variable subscriptsAdvantage of using arraysProgram using arrays

We want to show you the following items in this module: one-dimensional array concept, array declaration, initializing arrays, operation with arrays, constant and variable subscripts, advantage of using arrays and finally a program using arrays

2Array Illustration & Definitiony

An array is a group of consecutive memory locations that have the same name. In our example, we have the array called y.3Array Declaration. . .y[0] (7a1)y[1] (7a5)y[2] (7a9)y[9] int y [10];

int y [10]; is how you declare an integer array called y that contains 10 elements. The individual elements of y are labeled y[0], y[1], all the way through y[9]; Notice that in an array, each element is consecutive in memory. If y[0] is at address 7a1, then, y[1] is at address 7a5 (assuming that an int occupies 4 bytes)4More Array Declarationsfloat abc [50];

char name [18];

time t [40];

You can have a one-dimensional array of any data type. In the first example, float abc[50]; this declares a one-dimensional array of floating point numbers called abc. In the second example, char name [18]; we have an array of characters that is called name and is 18 elements long. You can even have a one-dimensional array of structures as in the last example. t is an array that is 40 elements long and each element of the array is of the structure time.5Initializing Arrays132537. . .0y[0] (7a1)y[1] (7a5)y[2] (7a9)y[9] int y [10] = {13,25,37};

int y[10] = {13,25,37}; is how you initialize an array at the time of declaration. In this example, y[0] contains the #13, y[2] contains the #37. Since we only have 3 initializers and the array is 10 elements long, elements y[3]-y[9] are initialized to 0. This is the rule, if you initialize at least 1 element of the array, the rest of the array is set to 0s. However, if you dont initialize any of the elements, the entire array contains garbage, not 0s. 6Operation with Arraysint x[10];

x[5] = 49;

cin >> x[1];

cout 42) cout > x[1]; reads an item of input into x [1]; cout 42) tests to see if the # in x[3] is > 42. You can see that any operation the can be performed on regular scalar variable can be done with array variable. The only thing you have to remember is to use the [ ]s.7Code Without Arraysint g1,g2,g3,g4 g 100;. . . cin >> g1 >> g2 >> g3. >> g100;

Lets take a look at some code that doesnt use arrays. Suppose you wanted to read in 100 grades. You would declare 100 variable as we have in the statement: int g1,g2g100; In order to read in the 100 grades, you write: cin >> g1 >> g2 >>g100; Of course, you cant use the ellipsis, you would have to specify each variable, g1 through g 100. Wow, this is a lot of work! What if we need to store 1000 grades? It would take us forever just to declare 1000 variables.8Advantage of Using Arraysint g1,g2,g3,g4 g 100;. . . cin >> g1 >> g2 >> g3. >> g100;. . . int g [100];int i;

for (i= 0; i < 100; i++)cin >> g[i];. . .

Lets rewrite the same code so that you can see the advantage of using arrays. We have the original code in the red box and the new code in the yellow box. Looking at the code on the bottom, you can see that in order to declare an array with 100 elements, we write: int g [100]; This declares 100 consecutive locations that are called g[0], g[1], all the way through g[99]. This sure beats having to enumerate 100 separate variables as in the top example.9Advantage of Using Arraysint g1,g2,g3,g4 g 100;. . . cin >> g1 >> g2 >> g3. >> g100;. . . int g [100];int i;

for (i= 0; i < 100; i++)cin >> g[i];. . .

Now, lets look at reading in 100 items of input. In the top box, we write: cin >> g1 >> g2 all the through >> g100. When you use a one-dimensional array, subscripts may be constants as in g[2] or you may use a subscript that is a variable. The 2 statements at the bottom of the yellow box: for (i=0; i < 100; i++) cin>> g[i]; reads in 100 variables without you having to enumerate each variable. Well illustrate this for you in the next slide.10Initialize Counter Variableint g [100];int i;

for (i= 0; i < 100; i++)cin >> g[i];. . .

. . .g[0] (7a1)g[1] (7a5)g[2] (7a9)g[99] 0i

After we have executed the for statement the 1st time, we have a picture as shown above: we have the array g that contains element g[0]-g[99], the variable i is initialized to 0 in the for loop.11Reading Input into Arrayint g [100];int i;

for (i= 0; i < 100; i++)cin >> g[i];. . .

17. . .g[0] (7a1)g[1] (7a5)g[2] (7a9)g[99] 0i

The statement cin >> g[i]; tells the computer to read a number into the ith element of array g. Since i = 0; the computer will read a # into into g[0];12Incrementing the for Loopint g [100];int i;

for (i= 0; i < 100; i++)cin >> g[i];. . .

17. . .g[0] (7a1)g[1] (7a5)g[2] (7a9)g[99] 1i

After the 1st number has been read into g[0], the computer performs the i++ part of the for loop and now i = 1 as shown.13Read in 2nd Item of Inputint g [100];int i;

for (i= 0; i < 100; i++)cin >> g[i];. . .

1732. . .g[0] (7a1)g[1] (7a5)g[2] (7a9)g[99] 1i

Since i < 100; we execute the statement: cin>> g[i]; again. This tells the computer to read in a # to the ith element of the array. However, i is now 1, so it reads the 2nd item of input into g[1]. The picture shown is what the computers memory looks like after we have executed the cin statement the 2nd time. Now you get the story, the cin statement is executed 100 times and it reads in input into g[0], g[1] etc. This sure beats writing: cin