From 247c10daf174be34316bdfaaaaf3ddf6bce57962 Mon Sep 17 00:00:00 2001 From: BlackHat Date: Fri, 25 Sep 2020 23:14:10 +0530 Subject: [PATCH] Added Programs --- COLUMN WIGGLY.c | 88 ++++++++++++++ Clock Function.c | 43 +++++++ Clock.c | 43 +++++++ Copy Array Using Pointers.c | 21 ++++ Distance between two Points.c | 46 ++++++++ External 1.c | 20 ++++ External 2.c | 33 ++++++ External 3.c | 31 +++++ Frequency of a character.c | 45 +++++++ Incrementing Pointer.c | 8 ++ Larger in 2 number.c | 17 +++ Loop Counter.c | 14 +++ Mallcoc Calloc.c | 22 ++++ Matrix Add Mul Using Malloc.c | 110 ++++++++++++++++++ Matrix Addition.c | 66 +++++++++++ Matrix Using Malloc.c | 65 +++++++++++ Multiplication.c | 8 ++ Pointer Arithmatic.c | 35 ++++++ Pointer Basics .c | 14 +++ ROW SUM.c | 74 ++++++++++++ ROW WIGGLY.c | 84 +++++++++++++ Realloc Str1 to store in Str 2.c | 25 ++++ Reverse an Array.c | 36 ++++++ SORTING ANTI DIAGONAL.c | 80 +++++++++++++ SUM OF ANTI DIAGONAL.c | 55 +++++++++ Scalar Product of an Array Using Malloc.c | 46 ++++++++ Scalar Product using VLA.c | 41 +++++++ Search an Element in an Array.c | 36 ++++++ Segmentation fault example.c | 20 ++++ Simple Malloc.c | 12 ++ Sort Array using Malloc.c | 41 +++++++ ...using Pointers and Find the Intersection.c | 98 ++++++++++++++++ Sort the Array using Pointers.c | 45 +++++++ Sorting a String.c | 51 ++++++++ Sum Sub Pro .c | 20 ++++ Sum and Realloc and Sum again.c | 40 +++++++ TRANSPOSE.c | 67 +++++++++++ UPPER TRIANGULAR.c | 67 +++++++++++ 38 files changed, 1667 insertions(+) create mode 100644 COLUMN WIGGLY.c create mode 100644 Clock Function.c create mode 100644 Clock.c create mode 100644 Copy Array Using Pointers.c create mode 100644 Distance between two Points.c create mode 100644 External 1.c create mode 100644 External 2.c create mode 100644 External 3.c create mode 100644 Frequency of a character.c create mode 100644 Incrementing Pointer.c create mode 100644 Larger in 2 number.c create mode 100644 Loop Counter.c create mode 100644 Mallcoc Calloc.c create mode 100644 Matrix Add Mul Using Malloc.c create mode 100644 Matrix Addition.c create mode 100644 Matrix Using Malloc.c create mode 100644 Multiplication.c create mode 100644 Pointer Arithmatic.c create mode 100644 Pointer Basics .c create mode 100644 ROW SUM.c create mode 100644 ROW WIGGLY.c create mode 100644 Realloc Str1 to store in Str 2.c create mode 100644 Reverse an Array.c create mode 100644 SORTING ANTI DIAGONAL.c create mode 100644 SUM OF ANTI DIAGONAL.c create mode 100644 Scalar Product of an Array Using Malloc.c create mode 100644 Scalar Product using VLA.c create mode 100644 Search an Element in an Array.c create mode 100644 Segmentation fault example.c create mode 100644 Simple Malloc.c create mode 100644 Sort Array using Malloc.c create mode 100644 Sort Array using Pointers and Find the Intersection.c create mode 100644 Sort the Array using Pointers.c create mode 100644 Sorting a String.c create mode 100644 Sum Sub Pro .c create mode 100644 Sum and Realloc and Sum again.c create mode 100644 TRANSPOSE.c create mode 100644 UPPER TRIANGULAR.c diff --git a/COLUMN WIGGLY.c b/COLUMN WIGGLY.c new file mode 100644 index 0000000..2562585 --- /dev/null +++ b/COLUMN WIGGLY.c @@ -0,0 +1,88 @@ +#include +int r,c; + +int inp(int arr[r][c],int r,int c) +{ + int i,j; + for(i=0;i=0;i--) + { + if(p==0) + { + for(j=0;j=0;j--) + { + arr1[counter][k]=arr[j][i]; + k++; + } + counter++; + p=0; + } + } + return arr1; +} + +int main() +{ + int i,j; + printf("Enter the number of Rows : "); + scanf("%d",&r); + printf("Enter the number of Columns : "); + scanf("%d",&c); + int arr[r][c],arr1[r][c]; + inp(arr,r,c); + printf("The Matrix is :\n"); + print(arr,r,c); + wiggly(arr,arr1,r,c); + printf("The wiggly Path Array is :\n"); + print1(arr1,r,c); + return 0; +} diff --git a/Clock Function.c b/Clock Function.c new file mode 100644 index 0000000..ed3b0fd --- /dev/null +++ b/Clock Function.c @@ -0,0 +1,43 @@ +#include +#include +#include + +int n; + +double** random(int n) +{ + int i; + int *arr; + arr=(int*) malloc(n*sizeof(int)); + for(i=0;i +#include +#include + +int n; + +double** random(int n) +{ + int i; + int *arr,*pos,max=0; + arr=(int*) malloc(n*sizeof(int)); + for(i=0;imax) + { + max=*(arr+i); + pos=(arr+i); + } + } + printf("The Elements in the Random Function are : "); + for(i=0;i +void copy(int*,int*,int); +void main() +{ + int a[5]={1,2,3,4,5},b[5]; + copy(a,b,5); +} +void copy(int a[5],int b[5],int size) +{ + int i; + int *p=a; + for(i=0;i +#include +int n; + +void dis(int arr1[n],int arr2[n],int n) +{ + int i,diff=0; + for(i=0;i +#include +#include "7.c" +int n; + +void main() +{ + int n=3,i; + int *arr1[n],*arr2[n]; + printf("Array 1 : \n"); + for(i=0;i +#include "7.c" + +void main() +{ + int n=2,i; + int *arr1[n],*arr2[n]; + printf("Array 1 : \n"); + for(i=0;i +#include +#include + +void main() +{ + char *a,*b; + int i=0,j,k=0,c=0,ptr=0,f=0; + a=(char*)malloc(50*sizeof(char)); + b=(char*)malloc(50*sizeof(char)); + printf("Enter the String : "); + fgets(a,50,stdin); + strlwr(a); + while(a[i]!='\0') + { + c=0; + f=0; + if(a[i]>='a' && a[i]<='z') + { + for(j=0;j +void main() +{ + int a[5]={1,2,3,4,5}; + int *p=a,*q=a; + printf("The value of *p++ is %d",*p++); + printf("\nThe value of (*q)++ is %d",(*q)++); +} diff --git a/Larger in 2 number.c b/Larger in 2 number.c new file mode 100644 index 0000000..545f1da --- /dev/null +++ b/Larger in 2 number.c @@ -0,0 +1,17 @@ +#include +int main() +{ + int a=5,b=6; + int *x=&a,*y=&b; + printf("A is %d and B is %d.",*x,*y); + if(*x>*y) + { + printf("\nA is greater with the value of %d",*x); + return 0; + } + else + { + printf("B is greater with the value of %d",*y); + return 0; + } +} diff --git a/Loop Counter.c b/Loop Counter.c new file mode 100644 index 0000000..912f121 --- /dev/null +++ b/Loop Counter.c @@ -0,0 +1,14 @@ +#include + +int main() +{ + int i=1; + int *x=1; + for(i=1;i<5;i++); + printf("The final loop counter value is %d.",i); + for(x=1;x<5;x++); + printf("\nThe final loop counter value of pointer variable is %d.",x); + printf("\nThe address of the pointer variable is %p.",&x); + printf("\nThe address of the variable is %p.",&i); + return 0; +} diff --git a/Mallcoc Calloc.c b/Mallcoc Calloc.c new file mode 100644 index 0000000..57f380b --- /dev/null +++ b/Mallcoc Calloc.c @@ -0,0 +1,22 @@ +#include +#include + +int main() +{ + int *a=malloc(sizeof(int)),i; + *a=10; + printf("Using malloc() - *a = malloc(sizeof(int))"); + printf("\n%d\n",*a); + printf("Using calloc() the memory to store 3 values - a = (int*)calloc(3,sizeof(int))."); + a=(int*)calloc(3,sizeof(int)); + a[0]=1; + a[1]=2; + a[2]=3; + printf("\nThe elements are : "); + for(i=0;i<3;i++) + { + printf("%d ",a[i]); + } + free(a); + return 0; +} diff --git a/Matrix Add Mul Using Malloc.c b/Matrix Add Mul Using Malloc.c new file mode 100644 index 0000000..66035c8 --- /dev/null +++ b/Matrix Add Mul Using Malloc.c @@ -0,0 +1,110 @@ +#include +#include + +int** read(int** a,int r,int c) +{ + int i,j; + for(i=0;i +int r,c; + +int add(int arr1[r][c],int arr2[r][c],int arr3[r][c],int r,int c) +{ + int i,j; + for(i=0;i +#include + +int matrix(int* arr,int r,int c) +{ + int i,j,k=0; + int **a; + a=(int**)calloc(r,sizeof(int*)); + for(i=0;i +int main() +{ + int a=5,b=5; + int *x=&a,*y=&b; + printf("The Multiplication of %d and %d is %d.",*x,*y,(*x)*(*y)); + return 0; +} diff --git a/Pointer Arithmatic.c b/Pointer Arithmatic.c new file mode 100644 index 0000000..ef6e6fa --- /dev/null +++ b/Pointer Arithmatic.c @@ -0,0 +1,35 @@ +#include +int main() +{ + int a[5]={1,2,3,4,5},i; + printf("THE ARRAY IS : "); + for(i=0;i<5;i++) + { + printf("%d ",a[i]); + } + int *p=a,*q=a+1; + printf("\n\nThe pointer Initially points to the First Element of the Array : p is %d.",*p); + p=p+1; + printf("\n\nThe Addition of Pointer and Integer is Valid : (p+1) = %d.",*p); + p=1+p; + printf("\n\nThe Addition of Integer and a Pointer is Valid : (1+p) = %d.",*p); + printf("\n\nNow the pointer p points to the First Element of the Array : p is %d.",*p); + printf("\n\nThe Pointer q points to the second element of the Array : q is %d",*q); + p=a; + printf("\n\nThe Addition of Pointer and a Pointer is Valid Only in an Array : p+q = %d.",(*p)+(*q)); + p=a+2; + printf("\n\nNow The pointer p points to the third Element of the Array : p is %d.",*p); + printf("\n\nThe Subtraction of integer from a Pointer is Valid only in an Array : p-1 is %d.",*p-1); + printf("\n\nNow The pointer p points to the third Element of the Array : p is %d.",*p); + printf("\n\nThe Subtraction of integer from a Pointer is Valid only in an Array : 1-p is %d.",4-*p); + p=a+3; + printf("\n\nNow The pointer p points to the Fourth Element of the Array : p is %d.",*p); + q=a+1; + printf("\n\nNow The Pointer q points to the second element of the Array : q is %d",*q); + printf("\n\nThe Subtraction of Pointer from a Pointer is Valid only in an Array : p-q is %d.",*p-*q); + printf("\n\nNow The Pointer p points to 4 and the Pointer q points to 2."); + printf("\n\nThe Pointer and Pointer comparison is Valid : (p>q) prints True(1) | Answer is %d",*p>*q); + printf("\n\nIt is Not Valid to compare a pointer and a Integer unless the pointer is DeReferenced to get a Value"); + printf("\n\nIt is not valid to compare the Pointer to a 0."); + printf("\n\nIt is valid to compare the Pointer to a null.\n"); +} diff --git a/Pointer Basics .c b/Pointer Basics .c new file mode 100644 index 0000000..6772595 --- /dev/null +++ b/Pointer Basics .c @@ -0,0 +1,14 @@ +#include +int main() +{ + int a=5; + float b=10.1; + double c=10.1; + int *x=&a; + float *y=&b; + double *z=&c; + printf("The value of int is %d.",*x); + printf("\nThe value of float is %f.",*y); + printf("\nThe value of double is %lf.",*z); + return 0; +} diff --git a/ROW SUM.c b/ROW SUM.c new file mode 100644 index 0000000..78b62c7 --- /dev/null +++ b/ROW SUM.c @@ -0,0 +1,74 @@ +#include +int r,c; + +int rowsum(int arr[r][c],int arr1[r],int r,int c) +{ + int i,j,sum=0; + for(i=0;i +int r,c; + +int inp(int arr[r][c],int r,int c) +{ + int i,j; + for(i=0;i=0;j--) + { + arr1[i][k]=arr[i][j]; + k++; + } + } + } + return arr1; +} + +int main() +{ + int i,j; + printf("Enter the number of Rows : "); + scanf("%d",&r); + printf("Enter the number of Columns : "); + scanf("%d",&c); + int arr[r][c],arr1[r][c]; + inp(arr,r,c); + printf("The Matrix is :\n"); + print(arr,r,c); + wiggly(arr,arr1,r,c); + printf("The wiggly Path Array is :\n"); + print1(arr1,r,c); + return 0; +} diff --git a/Realloc Str1 to store in Str 2.c b/Realloc Str1 to store in Str 2.c new file mode 100644 index 0000000..ac624f6 --- /dev/null +++ b/Realloc Str1 to store in Str 2.c @@ -0,0 +1,25 @@ +#include +#include + +int main() +{ + char *p,*q; + int i; + p=malloc(5*sizeof(char)); + printf("Enter the String 1 : "); + scanf("%s",p); + printf("The String 1 is : "); + printf("%s",p); + q=malloc(5*sizeof(char)); + printf("\nEnter the String 2 : "); + scanf("%s",q); + printf("The String 2 is : "); + printf("%s",q); + printf("\nThe String 1 is Re Allocated to store both String 1 & String 2."); + p=realloc(p,10*sizeof(char)); + printf("\nEnter the String : "); + scanf("%s",p); + printf("The String is : "); + printf("%s",p); + return 0; +} diff --git a/Reverse an Array.c b/Reverse an Array.c new file mode 100644 index 0000000..412dab5 --- /dev/null +++ b/Reverse an Array.c @@ -0,0 +1,36 @@ +#include +int n; +void rev(int *arr[n],int n) +{ + int i,*temp; + for(i=0;i<(n/2);i++) + { + temp=*(arr+i); + arr[i]=arr[n-i-1]; + arr[n-i-1]=temp; + } + printf("\nThe Elements are : "); + for(i=0;i +int r,c; + +int bubsort(int arr1[r],int r) +{ + int i,j,temp; + for(i=0;iarr1[j+1]) + { + temp=arr1[j]; + arr1[j]=arr1[j+1]; + arr1[j+1]=temp; + } + } + } + return arr1; +} + +int inp(int arr[r][c],int r,int c) +{ + int i,j; + for(i=0;imax) + { + arr1[i]=arr[i][col]; + } + col--; + } + } + return arr1; +} + +int main() +{ + int i,j,sum=0,count=0; + printf("Enter the number of Rows : "); + scanf("%d",&r); + printf("Enter the number of Columns : "); + scanf("%d",&c); + int arr[r][c],arr1[r],col=c-1; + inp(arr,r,c); + printf("The Matrix is :\n"); + print(arr,arr1,r,c,count); + bubsort(arr1,r); + for(i=0;i +int r,c; + +int inp(int arr[r][c],int r,int c) +{ + int i,j; + for(i=0;imax) + { + max=arr[i][col]; + } + col--; + } + return max; +} + +int main() +{ + int i,j,sum=0,maxi; + printf("Enter the number of Rows : "); + scanf("%d",&r); + printf("Enter the number of Columns : "); + scanf("%d",&c); + int arr[r][c],arr1[r]; + inp(arr,r,c); + printf("The Matrix is :\n"); + maxi=print(arr,r,c,&sum); + printf("The Maximum element in the Anti Diagonal is %d.",maxi); + printf("\nThe sum of the Anti Diagonal elements is %d",sum); + return 0; +} + + diff --git a/Scalar Product of an Array Using Malloc.c b/Scalar Product of an Array Using Malloc.c new file mode 100644 index 0000000..893309c --- /dev/null +++ b/Scalar Product of an Array Using Malloc.c @@ -0,0 +1,46 @@ +#include +#include +int n; + +void pro(int arr1[n],int arr2[n],int n) +{ + int i,prod=0; + for(i=0;i +int n; + +int pro(int arr1[n],int arr2[n],int n) +{ + int i,prod=0; + for(i=0;i +int n; +void search(int *arr[n],int n,int ele) +{ + int i; + for(i=0;i +#include +void populate(int* a) +{ + int *parray=malloc(2*sizeof(int)); + parray[0]=37; + parray[1]=73; + a=parray; +} + +int main() +{ + printf("It gives segmentation fault because the pointer variable is assigned to NULL."); + printf("\n\nWhen the pointer is assigned to NULL and if its attempting to print it will get error because of not a valid location."); + printf("\n\nNow the program is modified such that pointer variable is not pointing to NULL."); + int *a=malloc(2*sizeof(int)); + populate(a); + printf("\n\na[0]=%d and a[1]=%d\n",a[0],a[1]); + return 0; +} diff --git a/Simple Malloc.c b/Simple Malloc.c new file mode 100644 index 0000000..da370b8 --- /dev/null +++ b/Simple Malloc.c @@ -0,0 +1,12 @@ +#include +#include + +int main() +{ + int *ptr; + ptr=(int*)malloc( sizeof(int) ); + *ptr=10; + printf("%d",*ptr); + free(ptr); +} + diff --git a/Sort Array using Malloc.c b/Sort Array using Malloc.c new file mode 100644 index 0000000..6726217 --- /dev/null +++ b/Sort Array using Malloc.c @@ -0,0 +1,41 @@ +#include +#include +#include + +void main() +{ + int n,i,j; + char temp; + printf("Enter the number of elements in the Array : "); + scanf("%d",&n); + char *a; + a=(char*)malloc(n*sizeof(char*)); + for(i=0;ia[j]) + { + temp = a[i]; + a[i] = a[j]; + a[j] = temp; + } + } + } + printf("\nThe Sorted Array is : "); + for(i=0;i +#include +int a; +void sort1(int *arr,int a) +{ + int i,j,temp; + for(i=0;i +#include + +void sort(int *arr,int n) +{ + int i,j,temp; + for(i=0;i +#include +#include + +void main() +{ + int n,len,i,j; + char* temp; + temp=(char*)malloc(len*sizeof(char)); + printf("Enter the number of Elements in the Array : "); + scanf("%d",&n); + printf("Enter the maximum length of the string in the Array : "); + scanf("%d",&len); + char **a; + a=(char**)malloc(n*sizeof(char*)); + for(i=0;i0) + { + strcpy(temp,a[j]); + strcpy(a[j],a[j+1]); + strcpy(a[j+1],temp); + } + } + } + printf("\nThe Elements after sorting : "); + for(i=0;i +int arith(int,int,int*,int*,int*); + +int main() +{ + int a=8,b=4,sum,diff,pro; + arith(a,b,&sum,&diff,&pro); + printf("A is %d and B is %d.",a,b); + printf("\nThe sum of two numbers is %d.",sum); + printf("\nThe difference of two numbers is %d.",diff); + printf("\nThe product of two numbers is %d.",pro); + return 0; +} + +int arith(int a,int b,int* sum,int* diff,int* pro) +{ + *sum=a+b; + *diff=a-b; + *pro=a*b; +} diff --git a/Sum and Realloc and Sum again.c b/Sum and Realloc and Sum again.c new file mode 100644 index 0000000..edb0aa1 --- /dev/null +++ b/Sum and Realloc and Sum again.c @@ -0,0 +1,40 @@ +#include +#include + +int main() +{ + int *p,sum=0,siz,i; + p=malloc(5*sizeof(int)); + printf("Enter the elements : \n"); + for(i=0;i<5;i++) + { + printf("Element %d : ",i+1); + scanf("%d",&p[i]); + sum+=p[i]; + } + printf("The Elements are : "); + for(i=0;i<5;i++) + { + printf("%d ",p[i]); + } + printf("\nThe sum of the 5 Elements is %d.",sum); + printf("\nEnter the Number of elements to Re Allocate : "); + scanf("%d",&siz); + p=realloc(p,siz*sizeof(int)); + printf("Enter the elements : \n"); + sum=0; + for(i=0;i<8;i++) + { + printf("Element %d : ",i+1); + scanf("%d",&p[i]); + sum+=p[i]; + } + printf("The Elements are : "); + for(i=0;i<8;i++) + { + printf("%d ",p[i]); + } + printf("\nThe sum of the 8 Elements is %d.",sum); + free(p); + return 0; +} diff --git a/TRANSPOSE.c b/TRANSPOSE.c new file mode 100644 index 0000000..b3110ad --- /dev/null +++ b/TRANSPOSE.c @@ -0,0 +1,67 @@ +#include +int r,c; + +int trans(int arr[r][c],int r,int c) +{ + int i,j,temp; + for(i=0;ij) + { + temp=arr[i][j]; + arr[i][j]=arr[j][i]; + arr[j][i]=temp; + } + } + } + return arr; +} +int inp(int arr[r][c],int r,int c) +{ + int i,j; + for(i=0;i +int r,c; + +int up(int arr[r][c],int r,int c) +{ + int i,j; + for(i=0;ij) + { + arr[i][j]=0; + } + } + } + return arr; +} +int inp(int arr[r][c],int r,int c,int* sum) +{ + int i,j; + for(i=0;i100)?"Yes":"No"); + return 0; +} + +