Upcoming SlideShare
×

# arrays

289 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
289
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
13
0
Likes
0
Embeds 0
No embeds

No notes for slide

### arrays

1. 1. FUNDAMENTALS OF PROGRAMMING Lec 10
2. 2. Today‟s agenda  Arrays
3. 3. What are Arrays?   An array is a series of elements of the same data type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier. That means that, for example, we can store 5 values of type int in an array without having to declare 5 different variables, each one with a different identifier. Instead of that, using an array we can store 5 different values of the same type, int for example, with a unique identifier.
4. 4. Types   One dimensional Multi dimensional
5. 5. Declaration   DataType ArrayName[size] E.g int Age[12]; declares a group or array of 12 values, each one being an integer
6. 6. Declaration & Initialization  int billy [5]; //declaration  int billy [5] = { 16, 2, 77, 40, 12071 };  OR int billy [] = { 16, 2, 77, 40, 12071 };
7. 7. Cont.. int age[4]; age[0]=23; age[1]=34; age[2]=65; age[3]=74;
8. 8. Important  The most important thing to remember about array allocation is that the elements of the array are indexed starting at 0, not 1. If the number of cells in an array is N, then the indexes run from 0 to N-1.
9. 9. Accessing the elements of an array.  We can access the value of any of its elements individually as if it was a normal variable, thus being able to both read and modify its value.  billy[2] = 75; a = billy[2]; b = billy [a+2]; A= billy[2] + 5;   
10. 10. int billy [] = {16, 2, 77, 40, 12071}; int n, result=0; for ( n=0 ; n<5 ; n++ ) { result += billy[n]; } cout << result;
11. 11. sizeof     Imagine you declare an array as follows: int number[] = {18, 42, 25, 12, 34, 15, 63, 72, 92, 26, 26, 12, 127, 4762, 823, 236, 84, 5}; Instead of counting the number of members of this array you can use sizeof as follows: int var= sizeof(number)/sizeof(int);
12. 12. Things you cannot do   int array[10]; cout << array << endl; int array[10]; cin >> array;
13. 13. Example int abc[5],i; for(i=0;i<5;i++) cin>>abc[i]; for(i=4;i>=0;i--) cout<<abc[i];
14. 14. Class Practice  Input data in two arrays and add them and store the result in a third array.
15. 15. Recall     char str[5]; Null character 0 char str[10]=“Pakistan”; OR char str[10]={„P‟,‟A‟,‟K‟,‟I‟…..}
16. 16. char str[10]=“pakistan”; cout<<str;
17. 17. Example Copy one string into another char str1[15],str2[15]; int I; cout<<“ENTER STRING”; cin>>str1; for(i=0;str[i]!=„0‟;i++) str2[i]=str1[i]; str2[i]=„0‟; 
18. 18. Next class     Think over the logic of Insert an element into an array Delete an element from the array Merge two arrays