Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS-161 Computer Programming Lecture 14: Arrays I

Similar presentations


Presentation on theme: "CS-161 Computer Programming Lecture 14: Arrays I"— Presentation transcript:

1 CS-161 Computer Programming Lecture 14: Arrays I
Spring 2015 (Sections A & B) Saqib Rasheed

2 Arrays They are special kind of data type In C++ each array has name
size They occupy contagious area of memory

3 Storage of an array in memory
C[0] C[1] C[2] C[3] C[4] C[5] C[6] C[7] C[8] C[9] Name ... 35 59 24 memory Index

4 Declaration of Arrays arrayType arrayName[numberOfElements ];
For example , int age [ 10 ] ; More than one array can be declared on a line int age [10] , height [10] , names [20] ; Mix declaration of variables with declaration of arrays int i , j , age [10] ;

5 Defining Arrays When defining arrays, specify Examples:
Name Type of array Number of elements arrayType arrayName[ numberOfElements ]; Examples: int week[ 10 ]; float myArray[ 3284 ]; Defining multiple arrays of same type Format similar to regular variables Example: int b[ 100 ], x[ 27 ];

6 Arrays Array To refer to an element, specify Format:
Name of array (Note that all elements of this array have the same name, week) Array Group of consecutive memory locations Same name and same type To refer to an element, specify Array name Position number Format: arrayname[ position number ] First element at position 0 n element array named week: week[ 0 ], week[ 1 ]...week[ n–1] Saturday temp Sunday temp Monday temp Tuesday temp Wednesday temp Thursday temp Friday temp week[0] week[1] week[2] week[3] week[4] week[5] week[6] Position number of the element within array week

7 Arrays Array elements are like normal variables
week[ 0 ] = 3; cout << week[ 0 ]; Perform operations in subscript. If x equals 3 week[ ] = week[ 3 ] = week[ x ] Static entry (same size throughout program execution) Some arrays are dynamic (that can be shrink and increase at execution time) called Dynamic Array.

8 Initializers Initializers
int n[ 5 ] = { 1, 2, 3, 4, 5 }; If not enough initializers, rightmost elements become 0 int n[ 5 ] = { 0 } If too many a syntax error is produced If size omitted, initializers determine it int n[ ] = { 1, 2, 3, 4, 5 }; 5 initializers, therefore 5 element array

9 Referring to individual elements of a Array
We can refer the individual elements of an array using subscripts (the number in brackets following the array name). The number specifies the element’s position in the array. All the array elements are numbered, starting at 0 to size. Example: week [2]. cin >> week[2]; Saturday temp Sunday temp Monday temp Tuesday temp Wednesday temp Thursday temp Friday temp week[0] week[1] week[2] week[3] week[4] week[5] week[6] cout << “The temperature is “ << week [2];

10 Entering Data into the Array
Example: for (int day = 0; day < 7; day ++ ) { cout << “Enter temperature for day“ << day+1; cin >> week [day]; }

11 Displaying Data from Array
Example: for (int day = 0; day < 7; day ++ ) { cout <<Week[day]; }

12 Reading Data from the Array
To calculate the average of the week’s temperature using array (here is the code). int sum = 0; for (int day = 0; day < 7; day++ ){ sum += week[day]; } cout << “Average is “ << sum/7;

13 Example #include<iostream.h> void main() { int marks [5]; int total=0,i; cout<< "Please enter marks of five students:\n"; for (i=0; i<5; i++) { cin >> marks[i]; } total + = marks[i]; float classavg = total/5; for(int j=0; j<5; j++) { cout<< "Students # "<< j+1<< “ scored "<<marks[j]<<" marks"; cout<< "Class average is "<<classavg<<endl;

14 Class Task Write a program to store and display the mathematical table of given number using arrays. Write a program to reverse elements in an array of size 10. Write a program that reads 10 integers from user and then print the largest and smallest integer using arrays.


Download ppt "CS-161 Computer Programming Lecture 14: Arrays I"

Similar presentations


Ads by Google