320 likes | 489 Views
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.
E N D
Objectives • One-dimensional array concept • Array declaration • Initializing arrays • Operation with arrays • Constant and variable subscripts • Advantage of using arrays • Program using arrays
Array Declaration int y [10]; y[0] (7a1) y[1] (7a5) y[2] (7a9) . . . y[9]
More Array Declarations float abc [50]; char name [18]; time t [40];
Initializing Arrays int y [10] = {13,25,37}; 13 y[0] (7a1) 25 y[1] (7a5) 37 y[2] (7a9) . . . 0 y[9]
Operation with Arrays int x[10]; x[5] = 49; cin >> x[1]; cout << x [5]; if (x[3] > 42) cout << “greater”;
Code Without Arrays int g1,g2,g3,g4… g 100; . . . cin >> g1 >> g2 >> g3…. >> g100;
Advantage of Using Arrays int g1,g2,g3,g4… g 100; . . . cin >> g1 >> g2 >> g3…. >> g100; . . . int g [100]; inti; for (i= 0; i < 100; i++) cin >> g[i]; . . .
Advantage of Using Arrays int g1,g2,g3,g4… g 100; . . . cin >> g1 >> g2 >> g3…. >> g100; . . . int g [100]; inti; for (i= 0; i < 100; i++) cin >> g[i]; . . .
Initialize Counter Variable g[0] (7a1) int g [100]; inti; for (i= 0; i < 100; i++) cin >> g[i]; . . . g[1] (7a5) g[2] (7a9) . . . g[99] 0 i
Reading Input into Array 17 g[0] (7a1) int g [100]; inti; for (i= 0; i < 100; i++) cin >> g[i]; . . . g[1] (7a5) g[2] (7a9) . . . g[99] 0 i
Incrementing the for Loop 17 g[0] (7a1) int g [100]; inti; for (i= 0; i < 100; i++) cin >> g[i]; . . . g[1] (7a5) g[2] (7a9) . . . g[99] 1 i
Read in 2nd Item of Input 17 g[0] (7a1) 32 int g [100]; inti; for (i= 0; i < 100; i++) cin >> g[i]; . . . g[1] (7a5) g[2] (7a9) . . . g[99] 1 i
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
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; inti; for (i = 0; i < MAX; i++) { cout << endl << "enter temperature for day " << (i + 1) << " "; cin >> temp[i]; sum += temp[i]; }
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; }
Preprocessor Directives & const Declarations #include <iostream> using std::cin; using std::cout; using std::endl; const int MAX = 30;
Declarations temp[0] (7a1) int main() { double temp[MAX]; double sum = 0; inti; temp[1] (7a5) temp[2] (7a9) . . . temp[29] 0 sum i
Read in & Sum Temperatures for (i = 0; i < MAX; i++) { cout << endl << "enter temperature for day " << (i + 1) << " "; cin >> temp[i]; sum += temp[i]; }
Memory Diagram After the for Loop 30 temp[0] (7a1) 29 temp[1] (7a5) 33 temp[2] (7a9) . . . 22 temp[29] 900 sum 30 i
Calculate and Print Average double avg = sum / (double) MAX; cout << "The average is " << avg << endl; cout << "\tDay#\tTemp. \tDeviation" << endl << endl;
Print Body of Chart for (i = 0; i < MAX; i++) cout << '\t' << (i + 1) << '\t' << temp[i] << '\t' << (temp[i] - avg) << endl;
Sample Output The average = 30.0 Day# Temp . Deviation 30.0 0. 28.0 -2.0 33.0 3 . . . 22.0 -8.0
Summary • One-dimensional array concept • Array declaration • Initializing arrays • Operation with arrays • Constant and variable subscripts • Advantage of using arrays • Program using arrays