Sie sind auf Seite 1von 19

ONE DIMENTIONAL ARRAY

#include<stdio.h> #define max 5 void main() { int a[max]={1,2,3,4,5}; int b[max],i; printf("enter the element of b"); for(i=0;i<max;i++) { scanf("%d",&b[i]); } printf("element of array a:\n"); for(i=0;i<max;i++) printf("%d\t",a[i]); printf("\n element of b:\n"); for(i=0;i<max;i++) printf("%d\t",b[i]); getch(); }

TWO DIMENTIONAL ARRAY #include<stdio.h> main() { int a[2][2],i,j; printf("enter the element of array:"); for(i=0;i<2;i++) { for(j=0;j<2;j++) { scanf("%d",&a[i][j]); } printf("enter number for another row\n"); } printf("the elements are"); printf("\n\n"); for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("%d",a[i][j]); } printf("\n"); } getch(); }

THREE DIMENTIONAL ARRAY #include<stdio.h> #define max 3 main() { int a[max][max][max],i,j,k,count=0; printf("enter the elements of array a(3*3*3):\n"); for(i=0;i<max;i++) for(j=0;j<max;j++) for(k=0;k<max;k++) scanf("%d",&a[i][j][k]); for(i=0;i<max;i++) for(j=0;j<max;j++) for(k=0;k<max;k++) if(a[i][j][k]==0) count++; printf("number of zeros in given array are%d",count); getch(); }

SUM OF ARRAY #include<stdio.h> #define max 5 void main() { int sum=0,i; int b[max]; printf("enter the element of b"); for(i=0;i<max;i++) { scanf("%d",&b[i]); } printf("element of array are:\n"); for(i=0;i<max;i++) { printf("%d",b[i]); sum=sum+b[i]; } printf("sum of the array element are:%d\n",sum); getch(); }

INSERT ARRAY #include<stdio.h> void main() { int a[5],i,j,size=6,item,pos; printf("enter the element in sorted order"); for(i=0;i<6;i++) scanf("%d",&a[i]); printf("enter the element to be inserted"); scanf("%d",&item); for(i=0;i<size;i++) { if(item<a[i]) { pos=i; break; } } for(j=size;j>pos;j--) { a[j]=a[j-1]; } a[j]=item; size++; printf("array elements are"); for(i=0;i<size;i++) { printf("%d",a[i]); } getch(); }

DELETE ELEMENT FROM ARRAY #include<stdio.h> #include<conio.h> main() { int a[5],i,j,size=6,item,pos; printf("enter the element in sorted order"); for(i=0;i<6;i++) scanf("%d",&a[i]); printf("enter the item that u want to be deleted"); scanf("%d",&item); for(i=0;i<size;i++) { if(item==a[i]) { pos=i; break; } } for(j=pos;j<size-1;j++) { a[j]=a[j+1]; } size--; printf("array elements are"); for(i=0;i<size;i++) { printf("%d",a[i]); } getch(); }

MERGING ARRAY #include<stdio.h> #define max 10 main() { int a[max],b[max],c[2*max],i,j,k,n,m; printf("enter the size of the array a"); scanf("%d",&n); printf("enter the array of the a:\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\n enter the size of array b:"); scanf("%d",&m); printf("enter the element of the array b:\n"); for(i=0;i<m;i++) scanf("%d",&b[i]); for(i=0,j=0,k=0;i<n&&j<m;k++) if(a[i]<b[j]) c[k]=a[i++]; else c[k]=b[j++]; while(i<n) c[k++]=a[i++]; while(j<m) c[k++]=b[j++]; printf("the array of 3rd merged array c:\n"); for(i=0;i<(n+m);i++) printf("%d",c[i]); getch(); }

PRODUCT OF TWO MATRIX #include<stdio.h> #define max 10 main() { int a[max][max],b[max][max],c[max][max],m,n,p,q,i,j,k; printf("enter the row & column of first matrix a:"); scanf("%d%d",&m,&n); printf("enter the row & column of first matrix b:"); scanf("%d%d",&p,&q); if(n==p) { printf("enter the elements of matrix a:\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("enter the elements of a matrix b:\n"); for(i=0;i<p;i++) for(j=0;j<q;j++) scanf("%d",&b[i][j]); printf("matrix a is"); for(i=0;i<m;i++) { printf("\n"); for(j=0;j<n;j++) printf("%d",a[i][j]); } printf("\n matrix b is:"); for(i=0;i<p;i++) { printf("\n"); for(j=0;j<q;j++) printf("%d",b[i][j]); } printf("\n product of two matrix"); for(i=0;i<m;i++) { printf("\n"); for(j=0;j<q;j++) { c[i][j]=0; for(k=0;k<n;k++) c[i][j]=c[i][j]+a[i][k]*b[k][j]; printf("%d",c[i][j]); }

} } else { printf("\n matrix are compatible for multiplication"); } getch(); }

SEARCHING ELEMENT IN ARRAY #include<stdio.h> #define max 5 main() { int arr[5],flag=0,i,item,s; printf("enter the element of array\n"); for(i=0;i<max;i++) { scanf("%d",&arr[i]); } printf("enter element to search"); scanf("%d",&item); for(i=0;i<max;i++) { if(arr[i]==item) { if(flag==0) { s=arr[i]; printf("element found%d",arr[i]); } } else { if(flag==1) printf("element not found %d",arr[i]); } } getch(); }

GREATEST NUMBER IN AN ARRAY #include<stdio.h> #define max 3 main() { int a[5],i,c=0; printf("enter any element"); for(i=0;i<5;i++) { scanf("%d",&a[i]); } for(i=0;i<5;i++) { if(c<a[i]) { c=a[i]; } } printf("greatest no is%d",c); getch(); }

CALL BY VALUE #include<stdio.h> main() { int a=10; int b=20; swap(a,b); printf("the value of a is %d",a); printf("the value of b is %d",b); getch(); }swap(int x,int y) { int t; t=x; x=y; y=t; printf("x=%d\n",x); printf("y=%d\n",y); }

CALL BY REFRENCE #include<stdio.h> main() { int a=10;int b=20; swap(&a,&b); printf("the value of a is %d",a); printf("the value of b is %d",b); getch(); } swap(int *x,int *y) { int t; t=*x; *x=*y; *y=t; }

STRUCTURES #include<stdio.h> #include<conio.h> struct book { char author[10]; int pages; float price; }; main() { struct book b1; printf("enter author's name"); scanf("%s",&b1.author); printf("enter pages"); scanf("%d",&b1.pages); printf("enter book price"); scanf("%f",&b1.price); printf("\n the details of book is"); printf("\n author name is %s",b1.author); printf("\n pages are %d",b1.pages); printf("\n price is %f",b1.price); getch(); }

POINTERS #include<stdio.h> main() { int a=10; int *p; p=&a; printf("\n the value of a is %d",a); printf("\n the address of a is %d",&a); printf("\n the value of pointer variable is %d",p); printf("\n the value on address hold by pointer is %d",&p); getch(); }

ARRAY OF STRUCTURE #include<stdio.h> struct book { char author[10]; int pages; float price; }; main() { struct book b1[2]; int i; for(i=0;i<2;i++) { printf("enter the name,pages,price"); scanf("%s%d%f",&b1[i].author,&b1[i].pages,&b1[i].price); } printf("\n\n values are"); for(i=0;i<2;i++) { printf("\n\n author name is %s,pages are %d,price is %f",b1[i].author,b1[i].pages,b1[i].price); printf("\n\n\n"); } getch(); }

STRUCTURE WITH IN STRUCTURE #include<stdio.h> struct a { int marks; struct b { char result[10]; }b1; }a1; main() { struct b b1; printf("enter the marks and result of student\n\n\n"); scanf("%d%s",&a1.marks,&a1.b1.result); printf("\nthe marks of the student is %d",a1.marks); printf("\nthe result of the student is %s",&a1.b1.result); getch(); }

STRUCTURE WITH IN POINTER #include<stdio.h> struct employee { int emp_no; char emp_name[25]; int salary; }; main() { struct employee emp={101,"john smith",9500}; struct employee *eptr; eptr=&emp; display(eptr); getch(); } display(struct employee *e) { printf("employee no %d\n",e->emp_no); printf("employee name: %s\n",e->emp_name); printf("employee salary %d\n",e->salary); }

PASSING ARRAY TO FUNCTIONS #include<stdio.h> #define max 10 void reverse(int[],int); main() { int arr[max]={22,5,67,98,45,32,101,99,73,10}; int i,j; printf("the list before reversing :\n"); for(i=0;i<max;i++) printf("%d\t",arr[i]); reverse(arr,max); printf("\n the list after reversing:\n"); for(i=0;i<max;i++) printf("%d\t",arr[i]); getch(); } void reverse(int arr[],int n) { int i,j,temp; for(i=0,j=max-1;i<max/2;i++,j--) { temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } }

Das könnte Ihnen auch gefallen