lect09_Array

lect09_Array - Containers Containers Arrays CSIS1117...

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

View Full Document Right Arrow Icon
Containers -- Arrays SIS1117 omputer Programming CS S Co pute og a g 1 c1117 lecture 9
Background image of page 1

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

View Full DocumentRight Arrow Icon
ontents Contents rrays Arrays Initialization ut of bound problem Out of bound problem Pass index variables all y alue Call-by-value Call-by-reference Multi-dimensional array c1117 lecture 9 2
Background image of page 2
toring a group of data Storing a group of data some situations we need to keep track on In some situations, we need to keep track on many data items in the program for further rocessing processing. E.g. Write a program to read ten numbers, then turn the mean and the standard deviation of return the mean and the standard deviation of them We may need to use ten variables to keep the ten numbers in the program. It is rather troublesome. ee en ariables cc s an example See ten-variables.cc as an example. c1117 lecture 9 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
int x1, x2, x3, x4, x5, x6, x7, x8, x9, x10; cin >> x1 >> x2 >> x3 >> x4 >> x5 > x > x > x > x > x 0 >> x6 >> x7 >> x8 >> x9 >> x10; double mean = (x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10) / 10.0; cout << "Mean: " << mean << endl; ... // calculate the standard deviation It seems to be troublesome to use 10 variables for storing the 10 numbers. How about write a program to read 100 numbers?? reate 00 riables? c1117 lecture 9 4 Create 100 variables?
Background image of page 4
ontainers Containers ntainer a collection of ariables f the me A container is a collection of variables of the same type . he C++ built facility rray used for such The C++ built-in facility array is used for such purpose to form containers . To declare an array , we have to specify the type of the data and the maximum no. of data to be stored. nt score set[ 0 ; // storing 0 nt int score_set[10]; // storing 10 int char myword[31]; // storing 31 char c1117 lecture 9 5
Background image of page 5

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

View Full DocumentRight Arrow Icon
n rray a group of ariables each of them An array is a group of variables , each of them can be accessed using the subscript operator [] . nt x[ 0 ; double total = int x[10]; double total = 0; const max_no_input = 10; or(int i = i < max no input; ++i){ for(int i 0; i < max_no_input; ++i){ cin >> x[i]; total += x[i]; } double mean = total / max_no_input; cout << "Mean: " << mean << endl; ... // calculate the standard deviation c1117 lecture 9 6 See ten-variables-array.cc as an example
Background image of page 6
imilar to access the characters in a ring the Similar to access the characters in a string , the elements storing in an array with size n are ccessing from dex - accessing from index 0 to n 1 . 4 dependent 8 1 9 0 x1 x2 x3 x4 x5 Independent variables An array of lements 9 8 4 1 0 -3 -8 6 3 -2 [9] elements x[0] x[9] c1117 lecture 9 7
Background image of page 7

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

View Full DocumentRight Arrow Icon
rray initialization Array initialization stead of ssigning e values one by one the Instead of assigning the values one by one, the elements of an array can be initialized by a list of lements elements . char choice[5]; choice[0] = 'A'; choice[1] = 'B'; choice[2] = 'C'; choice[3] = 'D'; choice[4] = 'E'; har choice[] = {'A', 'B', 'C', 'D', 'E'}; The size can be omitted if it is equal to the no. of char choice[] {A, B, C, D, E}; c1117 lecture 9 8 items in the list.
Background image of page 8
rray Initialization Array Initialization har choice[ = {'A', 'B', 'C', 'D', 'E'}; char choice[5] {A, B, C, D, E} ; cout << choice << endl; It is a common error to print the array name What is the output of the above code?
Background image of page 9

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

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

Page1 / 35

lect09_Array - Containers Containers Arrays CSIS1117...

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

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