1 / 25

One-Dimensional Array Introduction Lesson xx

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.

leland
Télécharger la présentation

One-Dimensional Array Introduction Lesson xx

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. One-Dimensional Array IntroductionLesson xx

  2. Objectives • One-dimensional array concept • Array declaration • Initializing arrays • Operation with arrays • Constant and variable subscripts • Advantage of using arrays • Program using arrays

  3. Array Illustration & Definition y

  4. Array Declaration int y [10]; y[0] (7a1) y[1] (7a5) y[2] (7a9) . . . y[9]

  5. More Array Declarations float abc [50]; char name [18]; time t [40];

  6. Initializing Arrays int y [10] = {13,25,37}; 13 y[0] (7a1) 25 y[1] (7a5) 37 y[2] (7a9) . . . 0 y[9]

  7. Operation with Arrays int x[10]; x[5] = 49; cin >> x[1]; cout << x [5]; if (x[3] > 42) cout << “greater”;

  8. Code Without Arrays int g1,g2,g3,g4… g 100; . . . cin >> g1 >> g2 >> g3…. >> g100;

  9. 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]; . . .

  10. 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]; . . .

  11. 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

  12. 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

  13. 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

  14. 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

  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

  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; inti;   for (i = 0; i < MAX; i++)   { cout << endl      << "enter temperature for day "       << (i + 1) << " "; cin >> temp[i];     sum += temp[i];   }

  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; }

  18. Preprocessor Directives & const Declarations #include <iostream> using std::cin; using std::cout; using std::endl; const int MAX = 30;

  19. Declarations temp[0] (7a1) int main() {   double temp[MAX];   double sum = 0; inti; temp[1] (7a5) temp[2] (7a9) . . . temp[29] 0 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];   }

  21. 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

  22. Calculate and Print Average double avg = sum / (double) MAX; cout << "The average is " << avg << endl; cout << "\tDay#\tTemp. \tDeviation"      << endl << endl;

  23. Print Body of Chart for (i = 0; i < MAX; i++) cout << '\t' << (i + 1) << '\t'       << temp[i] << '\t'       << (temp[i] - avg) << endl;

  24. Sample Output The average = 30.0 Day# Temp . Deviation 30.0 0. 28.0 -2.0 33.0 3 . . . 22.0 -8.0

  25. Summary • One-dimensional array concept • Array declaration • Initializing arrays • Operation with arrays • Constant and variable subscripts • Advantage of using arrays • Program using arrays

More Related