Anda di halaman 1dari 17

Arrays

Arrays
 An array is a consecutive group of memory locations that
all have the same type.
 These locations are referenced by a single variable called
array name.
 Each element of an array is referenced by its position in
the array.
 Position of an element in an array is represented by an
Index value or subscript
 The first element in array has subscript 0 and is called
zeroth element i.e. a[0] - a sub zero
Arrays
 For example;
 int c[12];
 If an array has ‘n’ elements, the index values will be from 0
to n-1.
 The above array contains integer elements and its name is
‘c’. It has 12 elements. First element of array is a[0] and
12th element is the last element and its reference is
a[11].
 Name of entire array is ‘c’. Its 12 elements are referred to
as c[0] to c[11].
Arrays
 A subscript must be an integer or integer expression.
 For example; a=5, b=6
 c[ a+b ]+=2;
 To print sum of first three elements of array
 cout<< c[0]+c[1]+c[2];
Declaring Arrays
 Type array name[array size];
 Using a loop to initialize array’s elements;
1. #include<iostream.h>
2. #include<conio.h>
3. int main()
4. {
5. int i, c[12];
6. for(i=0;i<12;i++)
7. c[i]=0;
8. for(i=0;i<12;i++)
9. cout<<c[i]<<endl;
10. getch();
11. }
Initializing an array in a declaration with
an initializer list
1. #include<iostream.h>
2. #include<conio.h>
3. #include<iomanip.h>
4. int main()
5. {
6. int i;
7. int c[12]={0,1,2,3,4,5,6,7,8,9,10,11};
8.
9. for(i=0;i<12;i++)
10. cout<<setw(3)<<c[i]<<endl;
11. getch();
12. }
Specifying an array’s size with a constant
variable and setting array elements with
calculations
1. #include<iostream.h>
2. #include<conio.h>
3. #include<iomanip.h>
4. int main()
5. {
6. const int arraysize=50;
7. int c[arraysize],i;
8. for(i=0;i<arraysize;i++)
9. c[i]=2+i*2;
10. cout<<"first 50 even numbers"<<endl;
11. for(i=0;i<arraysize;i++)
12. cout<<setw(10)<<c[i]<<endl;
13. getch();
14. }
Summing the elements of an array
1. #include<iostream.h>
2. #include<conio.h>
3. #include<iomanip.h>
4. int main()
5. {
6. const int arraysize=10;
7. int c[arraysize]={10,20,30,40,50,60,70,80,90,100};
8. int total=0;
9.
10. for(int i=0;i<arraysize;i++)
11. {
12. cout<<setw(4)<<c[i]<<endl;
13. total+=c[i];
14. }
15. cout<<"total is :"<<total<<endl;

16. getch();
17. }
Searching array
 #include<iostream.h>
 #include<conio.h>
 int main()
 {
 const int arraysize=10;
 int c[arraysize];
 int searchkey,k=0;
 for(int i=0;i<arraysize;i++)
 c[i]=i*2;

 cout<<"enter search key :"<<endl;
 cin>>searchkey;
 for(int i=0;i<arraysize;i++)
 {
 if(searchkey==c[i])
 {
 k=i;
 break;
 }
 }
 if (k==0)
 cout<<"key not found"<<endl;
 else
 cout<<"key found at c["<<k<<"]";

 getch();
 }
String Variable
 It is an array of character type variables
 Syntax is;
 char varname[n];
 The last character of every string variable is Null
character. Null character is represented by ‘/0’.
 For example;
 char a[5];
 In this variable max of 4 characters can be entered
because fifth character will be a Null character. If 2
characters will be entered, 3rd will be Null. Null character
is automatically added at end of the data in the string.
String Variable
 char str[9]="pakistan";
 char str[9]={'p','a','k','i','s','t','a','n','\0'};
 If Null character is not added at the end of string then
the variable of char type is handled as an array.
Sorting Arrays
 The process of arranging data in a specified order is
called sorting.
1. Bubble Sort
2. Selection Sort
Bubble Sort:
To arrange values of an array in ascending or descending
order
1. Two neighboring elements are compared
2. If one element is larger than the other, the two are
exchanged.
It is a slow process and used for sorting only small amount
of data.
 #include<iostream.h>
 #include<conio.h>
 int main()
 {
 int i, u, t, arr[4]={4,19,1,3};
 for(u=3;u>=1;u--)
 for(i=0;i<u;i++)
 if (arr[i]>arr[i+1])
 {
 t=arr[i];
 arr[i]=arr[i+1];
 arr[i+1]=t;
 }
 cout<<"sorted values are"<<endl;
 for(i=0;i<=3;i++)
 cout<<arr[i]<<endl;
 getch();
 }
Selection Sort
 To arrange values of an array in ascending or descending
order
1. Find out the smallest value from the list starting from
first element to the last element
2. Find out the smallest value from the list starting from
second element to the last element
3. Find out the smallest value from the list starting from
third element to the last element and so on.
 #include<iostream.h>
 #include<conio.h>
 int main()
 {
 int i, u, t,p,m, arr[4]={4,19,1,13};

 for(u=0;u<3;u++)
 {
 m=arr[u];
 for(i=u;i<=3;i++)
 if(m>arr[i])
 {
 m=arr[i];
 p=i;
 }

 t=arr[p];
 arr[p]=arr[u];
 arr[u]=t;
 }
 cout<<"sorted values are"<<endl;
 for(i=0;i<=3;i++)
 cout<<arr[i]<<endl;
 getch();
 }

Anda mungkin juga menyukai