lec08-array

lec08-array - CSC 1111 Introduction to Computing using C+...

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

View Full Document Right Arrow Icon
1 CSC 1111 Introduction to Computing using C++ 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
2 Outlines Introduction to arrays (1-D arrays) Defining and declaring arrays Accessing array elements Passing arrays to functions Examples Sorting array elements using Insertion Sort 2-D arrays Declaring and initializing 2-D arrays Passing 2-D arrays to functions
Background image of page 2
3 #include <iostream> using namespace std; int main() { int grade1, grade2, grade3; cout << "Student 1: " ; cin >> grade1; cout << "Student 2: " ; cin >> grade2; cout << "Student 3: " ; cin >> grade3; cout << "Average = " << (grade1 + grade2 + grade3) / 3.0 << endl; return 0 ; } The program works if there are only three students. What if there are 100 students?
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Array to the rescue! Ordinary Variable Like a box for storing one value Array Like a cabinet containing many drawers. Each drawer stores one value. We can refer to each drawer as 1 st drawer, 2 nd drawer, 3 rd drawer, etc.
Background image of page 4
5 Array Stores same type of data Array size = # of elements in the array Array size remains unchanged throughout program execution To refer to an array element arrayname[ index ] Index always starts from 0 Index to last element is ( array size – 1 ) grade[0] grade[1] grade[2] grade[3] grade[96] grade[97] grade[98] grade[99] Array name . . . . . . Array Index Storage
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Declaring Arrays type arrayName [ arraySize ]; type : Data type of each array elements arrayName : Identifier name; just like naming a variable arraySize : Number of elements in the array e.g., int grade[ 100 ]; // array of 100 integers float d[ 3284 ]; // array of 3284 floats Declaring multiple arrays of the same type in one declaration int arrayA[100], arrayB[27];
Background image of page 6
7 Array Elements Array elements are like other variables grade[0] = 3; cout << grade[0]; Index can be of any integral-type value. ( int, long, short, char) Index can be expression e.g.: c[5 – 2] is evaluated as c[3]
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 #include <iostream> #include <iomanip> // Needed for setw() using namespace std; int main() { int n[ 10 ]; // n is an array of 10 integers // Set all elements of array to 0 for ( int i = 0 ; i < 10 ; i++ ) n[i] = 0 ; cout << "Element" << setw( 13 ) << "Value" << endl; // output contents of array n in tabular format for ( int j = 0 ; j < 10 ; j++ ) cout << setw( 7 ) << j << setw( 13 ) << n[j] << endl; return 0 ; // indicates successful termination 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Background image of page 8
9 Element Value 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0
Background image of page 9

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

View Full DocumentRight Arrow Icon
10 Initializing Arrays In Declaration Specify value of each element when array is declared int n[ 5 ] = { 1, 2, 3, 4, 5 }; If not enough initializers, rightmost elements are set to 0 int n[ 5 ] = { 1 }; // n[1], …, n[4] are 0 int n[ 5 ] = { 0 }; // All elements are 0 If too many initializers, syntax error int n[ 5 ] = { 1, 2, 3, 4, 5, 6 }; // Error If array size is omitted, initializers determine size
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 34

lec08-array - CSC 1111 Introduction to Computing using C+...

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

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