Array Usage Powerful storage mechanism Can issue command like: "Do this to ith indexed variable" where i is computed by program "Display all elements of array score" "Fill elements of array score from user input" "Find highest value in array score" "Find lowest value in array score" Copyright 2006 Pearson Addison-Wesley.All rights reserved. 5-6
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-6 Array Usage ¨ Powerful storage mechanism ¨ Can issue command like: ¨ "Do this to i th indexed variable" where i is computed by program ¨ "Display all elements of array score" ¨ "Fill elements of array score from user input" ¨ "Find highest value in array score" ¨ "Find lowest value in array score
Array Program Example: Display 5.1 Program Using an Array(1 of 2) Display 5.1 Program Using an Array 1 //Reads in five scores and shows how much each 2 //score differs from the highest score. 3 #include <iostream> 4 using namespace std; int main() 6 7 int i,score[5],max; cout <<"Enter 5 scores:\n"; 9 cin >score[0]; max =score[0]; 11 for (i =1;i<5;i++) 13 cin >score[i]; 14 if (score[i]max) 15 max score[i]; 16 //max is the largest of the values score[0],.,score[i]. 17 Copyright006 Pearson Addison-Wesley.All rights reserved. 5-7
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-7 Array Program Example: Display 5.1 Program Using an Array (1 of 2)
Array Program Example: Display 5.1 Program Using an Array(2 of 2) 1 cout <"The highest score is "<max <endl 19 <"The scores and their\n" <<"differences from the highest are:\n"; 21 for(i=0;i<5;i+) 2 cout <score[i]<<off by 23 <<(max-score[i])<<endl; 24 return 0; 25 SAMPLE DIALOGUE Enter 5 scores: 592106 The highest score is 10 The scores and their differences from the highest are: 5 off by 5 9 off by 1 2 off by 8 10 off by 0 6 off by 4 Copyright 2006 Pearson Addison-Wesley.All rights reserved. 5-8
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-8 Array Program Example: Display 5.1 Program Using an Array (2 of 2)
for-loops with Arrays Natural counting loop Naturally works well "counting thru"elements of an array ◆Example: for (idx 0;idx<5;idx++) { cout <score[idx]<"off by <max-score[idx]<endl; } Loop control variable (idx)counts from 0-5 Copyright 2006 Pearson Addison-Wesley.All rights reserved. 5-9
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-9 for-loops with Arrays ¨ Natural counting loop ¨ Naturally works well "counting thru" elements of an array ¨ Example: for (idx = 0; idx<5; idx++) { cout << score[idx] << "off by " << max – score[idx] << endl; } ¨ Loop control variable (idx) counts from 0 – 5
Major Array Pitfall ◆Array indexes always start with zero! Zero is "first"number to computer scientists C++will "let"you go beyond range Unpredictable results ◆Compiler will not detect these errors! Up to programmer to "stay in range" Copyright 2006 Pearson Addison-Wesley.All rights reserved. 5-10
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-10 Major Array Pitfall ¨ Array indexes always start with zero! ¨ Zero is "first" number to computer scientists ¨ C++ will "let" you go beyond range ¨Unpredictable results ¨Compiler will not detect these errors! ¨ Up to programmer to "stay in range