5 array
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,033
On Slideshare
1,033
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
73
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Array is a collection of identical data objects which are stored in consecutive memory locations under a common heading or a variable name. Array Declaration storage_class data_type array_name[expression]; static char page[8]; where storage_class refers to the scope of the array variable such as external, static, automatic. 09/04/131 VIT - SCSE Array
  • 2. Array Initialization storage_class data_type array_name[expression] = {element 1, element 2,……,element n}; Types of Array 1. Single Dimensional Array 2. Two Dimensional Array 3. Multi Dimensional Array 09/04/132 VIT - SCSE
  • 3. Single Dimensional #include<iostream.h> void main() { int a[7]={11,12,13,14,15,16,17}; int i; cout<<”Contents of the array n”; for(i=0;i<6;i++) { cout<<a[i]<<’t’; }} 09/04/133 VIT - SCSE
  • 4. A program to read a set of numbers from the keyboard and to find out the largest number in the given array. #include<iostream.h> void main() { int a[100]; int i,n,larg; cout<<”How many numbers are in the array?”<<endl; cin>>n; cout<<”Enter the elements”<<endl; for(i=0;i<=n-1;i++) { cin>>a[i]; } 09/04/134 VIT - SCSE cout<<”Contents of the array”<<endl; for(i=0;i<=n-1;i++) { cout<<a[i]<<’t’; } cout<<endl; larg=a[0]; for(i=0;i<=n-1;i++) { if(larg<a[i]) larg=a[i]; } cout<<”Largest value in the array=”<<larg; }
  • 5. A program to read a set of numbers from the standard input device and to sort them in ascending order. 09/04/135 VIT - SCSE cout<<endl; for(i=0;i<=n-1;i++) { for(j=0;j<=n-1;j++) if(a[i]<a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } cout<<”Contents(sorted form)”<<endl; for(i=0;i<=n-1;i++) { cout<<a[i]<<’t’; } cout<<endl; } #include<iostream.h> void main() { int a[100]; int i,j,n,temp; cout<<”How many numbers are in the array?”<<endl; cin>>n; cout<<”Enter the elements”<<endl; for(i=0;i<=n-1;i++) { cin>>a[i]; }cout<<”Contents”<<endl; for(i=0;i<=n-1;i++) {cout<<a[i]<<’t’; }
  • 6. A program to read a set of numbers from the keyboard and to find out the sum of all elements of the given array using function 09/04/136 VIT - SCSE Arrays and Functions for(i=0;i<=n-1;i++) { cin>>a[i]; } cout<<”Output from the array”<<endl; output(a,n); sum=sumarray(a,n); cout<<endl; cout<<”Sum of the values of the array=”<<sum; } #include<iostream.h> #define MAX 100 void main() { void output(int a[],int n); int sumarray(int a[],int n); int a[MAX]; int I,n,sum; cout<<”How many numbers are in the array?”<<endl; cin>>n; cout<<”Enter the elements”<<endl;
  • 7. 09/04/137 VIT - SCSE void output(int a[],int n) { cout<<endl; for(i=0;i<=n-1;i++) { cout<<a[i]<<’t’; } } int sumarray(int x[],int max) { int value=0; for(int i=0;i<=max-1;i++) { value=value+x[i]; } return(value); }
  • 8. 09/04/138 VIT - SCSE Multidimensional Array Syntax: storage_class data_type arrayname[expr1][expr2]….[exprn]; float value[10][10]; int line[20][10][5];
  • 9. A program to initialize a set of numbers in a two dimensional array and to display the content of the array on the screen 09/04/139 VIT - SCSE cout<<”Contents of the array”<<endl; for(i=0;i<=N-1;i++) { for(j=0;j<=M-1;j++) cout<<a[i][j]<<’t’; cout<<endl; } } #include<iostream.h> #define N 3 #define M 4 void main() { int i,j; float a[N][M] = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12} };
  • 10. Character Array char page[40]; 09/04/1310 VIT - SCSE #include<iostream.h> void main() { int i; char name[5]={‘r’,’a’,’v’,’i’}; cout<<”Contents of the array”<<endl; for(i=0;i<=4;i++) { cout<<”name[“<<i<<”]=”<<name[i]<<endl; } }
  • 11. Pointer 09/04/1311 VIT - SCSE Pointer Declaration Pointer Operator (*) Address Operator (&) #include<iostream.h> void main() { char x,y; char *p; x=’c’; p=&x; y=*p; cout<<”Value of x=”<<x<<endl; cout<<”pointer value=”<<y<<endl; }
  • 12. Pointer Arithmetic +, -, ++, -- 09/04/1312 VIT - SCSE #include<iostream.h> void main() { int x,y; int *p; x=10; p=&x; cout<<”value of x=”<<x<<”and pointer=”; cout<<*p<<endl; y=*p+1; cout<<”Value of y=”<<y<<”and pointer=”; cout<<*p<<endl; }
  • 13. Pointers and Functions 09/04/1313 VIT - SCSE Use of the pointers in a function definition may be classified in to two groups. 1.call by value 2.call by reference
  • 14. call by value //A program to exchange the contents of two variables using a call by value 09/04/1314 VIT - SCSE #include<iostream.h> void main() { int x,y; void swap(int,int); x=100; y=200; cout<<”values before swap”<<endl; cout<<”x=”<<x<<”y=”<<y<<endl; swap(x,y); cout<<”values after swap”<<endl; cout<<”x=”<<x<<”y=”<<y<<endl; } void swap(int x,int y) { int temp; temp=x; x=y; y=temp; }
  • 15. call by reference Address of the actual arguments are copied onto the formal arguments 09/04/1315 VIT - SCSE #include<iostream.h> void main() { int x,y; void swap(int *x,int *y); x=100; y=200; cout<<”values before swap”<<endl; cout<<”x=”<<x<<”y=”<<y<<endl; swap(&x,&y); cout<<”values after swap”<<endl; cout<<”x=”<<x<<”y=”<<y<<endl; } void swap(int *x,int *y) { int temp; temp=*x; *x=*y; *y=temp; }