1 / 14

CS 1400

CS 1400. 13 Oct 2006 Chap 7. Arrays. Declaration examples; int costs[10] float rates[100]; int ages [n]; Can’t declare size with a variable! Initialization examples; int costs [5] = {99, 88, 77, 66, 55}; float rates [100] = {1.0}; References examples; costs[n] = 21; cout << rates[5];

xiu
Télécharger la présentation

CS 1400

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. CS 1400 13 Oct 2006 Chap 7

  2. Arrays • Declaration examples; int costs[10] float rates[100]; int ages [n]; Can’t declare size with a variable! • Initialization examples; int costs [5] = {99, 88, 77, 66, 55}; float rates [100] = {1.0}; • References examples; costs[n] = 21; cout << rates[5]; ages[n]++; how is this different from ages[n++]?

  3. Array indexes… • Indexes may be constants (integers) or variables • Indexes may also be simple integer expressions; cout << costs[n+2]; • There is no array bounds checking. • Array cell indexes always start at 0 (zero) for the first cell. • In an executable statement, only array cells may be referenced costs = rates * 2; exception: char arrays holding words or strings exception: passing an array reference to a function

  4. char arrays… • Note that char arrays are an exception. • can be input or output as “strings” char word[30]; cin >> word; • can also used as arrays if (word[0] == ‘a’) word[0] = ‘A’; cout << word[0] << word[5];

  5. Example: test grading • Write a program to grade a group of true/false tests. Each test has five (5) answers. • assume: the test key is in key.txt • assume: the tests are in students.txt (one line per student) Example: key.txt: T F T F F students.txt: Fred T F F T F Jane T F T T F Bill F T F F F Ann T F T F F

  6. Example output: student: Fred score 3 student: Jane score 4 student: Bill score 2 student: Ann score 5 correct answers summary by question: question 1: 3 question 2: 3 question 3: 2 question 4: 2 question 5: 4

  7. Pseudocode… • Read in the answers array from “key.txt” • Process each student in turn from “students.txt” • Input this students answers • Compare against correct answers and determine score • Output student score • Tabulate correct answers to summary table • Output summary table of correct answers

  8. 1. Read in correct answers char key[5]; ifstream fin_key; … fin_key.open (“e:\\key.txt”); // 1. read in correct answers for (int n=0; n<5; n++) fin_key >> key[n];

  9. 2. Process each student in turn… char name[32]; ifstream fin_ans; bool done = false; … fin_ans.open (“e:\\answers.txt”); // 2. process each student in turn while (!done) { fin_ans >> name; if (fin_ans.fail()) done = true; else { … // 2.a, b, c, d } … // 3. }

  10. 2. Process each student in turn… char answers[5]; … else { for (int n=0; n<5; n++) // 2.a input a student’s answers fin_ans >> answers[n]; … // 2.b, c, d } … // 3.

  11. 2. Process each student in turn… else { … // 2.a input student’s answers int score = 0; // 2.b determine score for (j=0; j<5; j++) if (answers[j] == key[j]) score++; … }

  12. 2. Process each student in turn… else { … // 2.a input student’s answers … // 2.b determine score cout << “student: “ << name << “ score: “ << score << endl; // 2.c output student’s score … }

  13. 2. Process each student in turn… int table[5] = {0, 0, 0, 0, 0}; … else { … // 2.a input student’s answers … // 2.b determine score … // 2.c output student’s score for (m=0; m<5; m++) if (answers[m] == key[m]) // 2.d tabulate scores table[m]++; }

  14. 3. Output summary table… … cout << “correct answers summary by question: \n”; for (int p=0; p<5; p++) cout << “ question “ << p << “: “ << table[p] << endl; …

More Related