Download presentation
Presentation is loading. Please wait.
1
One-Dimensional Array Introduction Lesson xx
In this presentation, we’ll introduce you to one dimensional arrays.
2
Objectives One-dimensional array concept Array declaration
Initializing arrays Operation with arrays Constant and variable subscripts Advantage of using arrays Program 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
3
Array Illustration & Definition
An array is a group of consecutive memory locations that have the same name. In our example, we have the array called y.
4
Array Declaration int y [10]; y[0] (7a1) y[1] (7a5) y[2] (7a9) . . .
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)
5
More Array Declarations
float 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.
6
Initializing Arrays int y [10] = {13,25,37}; 13 y[0] (7a1) 25
. . . y[0] (7a1) y[1] (7a5) y[2] (7a9) y[9] 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 don’t initialize any of the elements, the entire array contains garbage, not 0s.
7
Operation with Arrays int x[10]; x[5] = 49; cin >> x[1];
cout << x [5]; if (x[3] > 42) cout << “greater”; If you have an array called x, shown here are some of the operations that can be performed on subscripted variables. x[5] = 49; is an example of assignment. Here, we are storing the # 49 element 5 of array x. cin >> x[1]; reads an item of input into x [1]; cout << x[5]; prints out the contents of x[5]. if (x[3] >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.
8
Code Without Arrays int g1,g2,g3,g4… g 100; . . .
cin >> g1 >> g2 >> g3…. >> g100; Let’s take a look at some code that doesn’t use arrays. Suppose you wanted to read in 100 grades. You would declare 100 variable as we have in the statement: int g1,g2…g100; In order to read in the 100 grades, you write: cin >> g1 >> g2… >>g100; Of course, you can’t 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.
9
Advantage of Using Arrays
int 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]; . . . Let’s 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.
10
Advantage of Using Arrays
int 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, let’s 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. We’ll illustrate this for you in the next slide.
11
Initialize Counter Variable
. . . g[0] (7a1) g[1] (7a5) g[2] (7a9) g[99] int g [100]; int i; for (i= 0; i < 100; i++) cin >> g[i]; . . . 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. i
12
Reading Input into Array
17 . . . g[0] (7a1) g[1] (7a5) g[2] (7a9) g[99] int g [100]; int i; for (i= 0; i < 100; i++) cin >> g[i]; . . . 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]; i
13
Incrementing the for Loop
17 . . . g[0] (7a1) g[1] (7a5) g[2] (7a9) g[99] int g [100]; int i; for (i= 0; i < 100; i++) cin >> g[i]; . . . 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. 1 i
14
Read in 2nd Item of Input 17 g[0] (7a1) int g [100]; int i;
32 . . . g[0] (7a1) g[1] (7a5) g[2] (7a9) g[99] int g [100]; int i; for (i= 0; i < 100; i++) cin >> g[i]; . . . 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 computer’s 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<< g1 << g2… g100. You can see that one of the big advantages of using an array is that you can use a variable for a subscript and the variable can be controlled by a loop. 1 i
15
Program Description Read in 30 temperatures, 1 for each day of the month Calculate & print the average temperature for the month 3. Print out a chart. Each line should contains the day #, temperature and deviation from the average Let’s write a program to do the following: 1) Read in 30 temperatures, 1 for each day of the month 2) Calculate & print the average temperature for the month 3) Print out a chart. Each line of the chart should contains the day #, temperature and deviation from the average
16
Program Listing Part 1 #include <iostream> using std::cin; using std::cout; using std::endl; const int MAX = 30; int main() { double temp[MAX]; double sum = 0; int i; for (i = 0; i < MAX; i++) { cout << endl << "enter temperature for day " << (i + 1) << " "; cin >> temp[i]; sum += temp[i]; } This is the listing of the 1st part of the code. Basically, it sets up the array, reads in and sums the 30 temperatures.
17
Program Listing Part 2 double avg = sum / (double)MAX; cout << "The average is " << avg << endl; cout << "\tDay#\tTemp. \tDeviation" << endl << endl; for (i = 0; i < MAX; i++) cout << '\t' << (i + 1) << '\t' << temp[i] << '\t' << (temp[i] - avg) << endl; return 0; } This is part 2 of the code. This section calculates the average temperature for the month and prints out the chart. Now, let’s go through the code in detail.
18
Preprocessor Directives & const Declarations
#include <iostream> using std::cin; using std::cout; using std::endl; const int MAX = 30; At the beginning of our program, we have our preprocessor directives which are listed for you in yellow. The statement in green: const int MAX = 30; sets up a constant integer called MAX and it is initialized to 30. You will see that throughout the program, we need to use the # 30. Declaring MAX as a constant int makes it easy for us to modify the program later if we change our mind and we want it to work for a different # of days. This way, we only need to change the constant statement and not every occurrence of the # 30.
19
Declarations int main() { double temp[MAX]; double sum = 0; int i;
. . . temp[0] (7a1) temp[1] (7a5) temp[2] (7a9) temp[29] int main() { double temp[MAX]; double sum = 0; int i; At the beginning of main(), we declare an array called temp which is MAX elements long (in this case 30). sum is used to add up the temperatures that are read in and i is a counter. Shown on the right is a picture of the computer’s memory up to this point in the code. sum i
20
Read in & Sum Temperatures
for (i = 0; i < MAX; i++) { cout << endl << "enter temperature for day " << (i + 1) << " "; cin >> temp[i]; sum += temp[i]; } In this part of the code, we have a for loop to read in the temperatures for each day of the month. Our prompt asks the user to enter the temperature for day 1, day2, etc. After each prompt, the input is read in to temp[i]. The temperature for day 1 is stored in temp[0], the temperature for day 2 is store in temp [1] etc. The last statement of the loop: sum+=temp[i]; takes the temperature that was read into temp[i] and adds it to sum. The next slide is a picture of the memory after we have executed this loop 30 times.
21
Memory Diagram After the for Loop
30 29 33 . . . 22 temp[0] (7a1) temp[1] (7a5) temp[2] (7a9) temp[29] 900 sum i The picture is dependent on the data that the user enters. In this example, the first temperature entered is 30 and it is stored in temp[0]. The 2nd temperature is 29 and it is stored in temp[1]. The 30th temperature is 22 and is stored in temp [29]. Sum will have the sum of the numbers that are stored in temp[0]-temp[29].
22
Calculate and Print Average
double avg = sum / (double) MAX; cout << "The average is " << avg << endl; cout << "\tDay#\tTemp. \tDeviation" << endl << endl; When we get to this part of the code, we have just exited the for loop and we have the sum of the 30 temperatures in the variable sum. If we take the sum and divide it by 30, we get the average temperature for the month which we store in avg. In the 1st cout statement, we print the average. In the 2nd cout, we print a heading for our table.
23
Print Body of Chart for (i = 0; i < MAX; i++) cout << '\t' << (i + 1) << '\t' << temp[i] << '\t' << (temp[i] - avg) << endl; In this code, we print one line of information for each day of the month. Each line contains: the day #, the temperature for the day and the amount of deviation from the average.
24
Sample Output The average = 30.0 Day# Temp . Deviation 30.0 0.
. . . Depending on the input entered, the output to this program will look something like the one shown here. We have a chart and each line of the chart contains the day #, the temperature for the day and the deviation from the average. You can see that this program is much easier to manage by using one-dimensional arrays. We can use variable subscripts which can be controlled by a counter in a loop rather than using individual scalar variable.
25
Summary One-dimensional array concept Array declaration
Initializing arrays Operation with arrays Constant and variable subscripts Advantage of using arrays Program using arrays In this module, we showed you pictorially what an array looked like. We talked about array declarations, initializing arrays and array operations. We went on and discussed the advantage of using arrays with variable subscripts. Finally, we wrote a program using arrays.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.