CP1_6_Arrays_1112

CP1_6_Arrays_1112 - Arrays Arrays The The need for arrays...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Arrays
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The need for arrays An array is used to process a collection of data of the same type Examples: A list of names A list of a stock’s daily closing price A list of test scores Why do we need arrays? Why not just store each data item in a separate variable? Imagine keeping track of 10, or 100, or 1000 closing prices in memory and calculating the moving average! py g g g How would you name all the variables? 2 CSIS1117B Computer Programming 1 2011-2012 How would you process each of the variables?
Background image of page 2
Processing 10 assignment scores nt 1 2 3 4 5 6 7 8 9 1 0 int s_1, s_2, s_3, s_4, s_5, s_6, s_7, s_8, s_9, s_10; cin >> s_1 >> s_2 >> s_3 >> s_4 >> s_5 >> s_6 >> s_7 >> s_8 >> s_9 >> s_10; ouble mean = s s s s double mean = (s_1 + s_2 + s_3 + s_4 + s_5 + s_6 + s_7 + s_8 + s_9 + s_10) / 10.0; cout << "Mean: " << mean << endl; // calculate the standard deviation, etc. Even in this case with only 10 variables it is very inconvenient to store and process the data. 3 CSIS1117B Computer Programming 1 2011-2012
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Containers A container is an object that stores other objects and provides ways to access them. The objects stored in a container are known as the elements of the container. The C++ built-in array is a commonly used type of container. To declare an array, you must specify: e type of the data it will contain and the type of the data it will contain, and the maximum number of data items to be stored. Just as we saw for other variable declarations, the complier needs both 4 CSIS1117B Computer Programming 1 2011-2012 pieces of information to know how to store the data in main memory
Background image of page 4
The Array Variables nt cores[10 ; / toring 10 nt Like declaring 10 variables of type int int scores[10]; // storing 10 int char myword[31]; // storing 31 char ike declaring 31 The elements of an array are also referred to s: Like declaring 31 variables of type char as: Indexed variables Subscripted variables The number of indexed variables in an array is e declared size of the array the declared size of the array
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 16

CP1_6_Arrays_1112 - Arrays Arrays The The need for arrays...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online