0% found this document useful (0 votes)
24 views

Array-Pro Datastructure

The document discusses several C programs related to arrays: 1. A program that reads and writes elements to an array and passes the array to a function to calculate the sum. 2. A program that inserts an element into an array at a given position by shifting other elements down. 3. A program that finds the minimum and maximum values in an array by passing the array to functions using pointers.

Uploaded by

wtxhawks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Array-Pro Datastructure

The document discusses several C programs related to arrays: 1. A program that reads and writes elements to an array and passes the array to a function to calculate the sum. 2. A program that inserts an element into an array at a given position by shifting other elements down. 3. A program that finds the minimum and maximum values in an array by passing the array to functions using pointers.

Uploaded by

wtxhawks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

//Reading and Writing of Array 2 //pass Array to function and find Sum of Array
Element
----------------------------------------------
-------------------------------------------
#include<stdio.h>
#include<stdio.h>
#include<conio.h>
#include<conio.h>
void main()
int funsum(int a[],int);
{
int a[10],len,sum,i;
int a[10],i,len;
void main()
clrscr();
{
printf("\n Enter length of Array \n");
clrscr();
scanf("%d",&len);
printf("\n Enter lengeth of array");
printf("\n Enter the Array element \n");
scanf("%d", &len);
for(i=0;i<=len-1;i++)
printf("\n Enter Array element \n");
{
for(i=0;i<=len-1;i++)
scanf("%d", &a[i]);
{
}
scanf("%d", &a[i]);
printf("\n The entered Array is");
}
for(i=0;i<=len-1;i++)
sum=funsum(a,len);
{
printf("\n Sum of Array element is =%d",sum);
printf("\n a[%d]=%d",i,a[i]);
}
getch();
getch();
}
int funsum(int a[],int len)
}
{
int add=0;
for (i=0;i<=len-1;i++)
{
add=add+a[i];
}
return add;
}
3./Insert an element in an Array --pos;
#include<stdio.h> funarray(a,len,pos,num);
#include<conio.h> getch();
void funarray(int a[],int,int,int); }
int a[10],len,pos,num,i; void funarray(int a[],int len,int pos, int num)
void main() {
{ for(i=len; i>=pos; i--)
clrscr(); {
printf("\n enter length of array"); a[i+1]=a[i]; //Shift down by 1 position
scanf("%d", &len); }
printf("\n Enter the array element \n"); a[pos]=num;
for(i=0;i<=len-1;i++) len=len+1;
{ printf("\n New Array");
scanf("%d",&a[i]); for(i=0;i<=len-1;i++)
} {
printf("\n Enter Number to be inserted"); printf("\n %d", a[i]);
scanf("%d",&num); }
printf("\n Enter Position to be inserted"); }
scanf("%d",&pos);

1
/4*Write a Array Programme using Dynamic //5.Write a programe to print sum and Average
Memory allocation */ #include<stdio.h>
#include<stdio.h> #include<conio.h>
#include<conio.h> #include<alloc.h>
#include<alloc.h> void main()
void main() {
{ //int *a, n, i,sum=0,avg=0;
int *a,n,i; int a[10], n,i,sum=0,avg=0;
clrscr(); clrscr();
printf("\n Enter the number of Element"); printf("\n Enter number of element \n");
scanf("%d", &n); scanf("%d",&n);
a=(int *) malloc(n * sizeof(int)); //a=(int *) malloc(n * sizeof(int));
printf("\n Enter the array element \n"); printf("\n Enter Array element \n");
for(i=0;i<n;i++) for(i=0;i<n;i++)
{ {
scanf("%d", (a+i)); scanf("%d",&a[i]);
} }
printf("\n Display Array element"); printf("\n Display Sum \n");
for(i=0;i<n;i++) for(i=0;i<n;i++)
{ {
printf("\n a[%d]=%d", i,*(a+i)); sum=sum+ a[i];
} //sum=sum+ *(a+i);
getch();
} }
avg=sum/n;
printf("\n SUM=%d \t AVG=%d",sum,avg);
getch();
}

2
/6*Write a Array Programme to find MIN and /7* Algoritham:-Insert an element in Array
Max Value 1. Initialize the value of i, Set i=len
#include<stdio.h> 2. shift the element down by 1 position
#include<conio.h> using for loop
#include<alloc.h> set a[i+1]=a[i]
void main() 3. Insert element at requird position
{ set a[pos]=num
int *a, n,i,min,max; 4. increse length
clrscr(); set len=len+1
printf("\n Enter number of element \n"); 5. Display new list of Array
scanf("%d",&n); 6. END
a=(int *) malloc(n * sizeof(int)); */
printf("\n Enter array Element \n"); #include<stdio.h>
for(i=0;i<n;i++) #include<conio.h>
{ void main()
scanf("%d", (a+i)); {
} int a[100],i,len,pos,num;
min= *(a+0); clrscr();
max= *(a+0); printf("\n Enter number of element in Array\n
for(i=0; i<n;i++) ");
{ scanf("%d",&len);
if(*(a+i)< min) printf("\n Enter array element");
{ for(i=0;i<len;i++)
min= *(a+i); {
} scanf("%d",&a[i]);
else }
{ printf("\n Enter POS of element \n ");
if(*(a+i)> max) scanf("%d",&pos);
{ --pos;
max= *(a+i); printf("\n Enter the number do you want to
} insert \n ");
} scanf("%d",&num);
printf("shift element one position down");
} for(i=len;i>=pos;--i )
printf("\n Min Value is %d", min); { a[i+1]=a[i];
printf("\n Max Value is %d", max); }
getch(); a[pos]=num;
} if(pos>len)
{
printf("\n Insertion outside the array");
}
len=len+1;
printf("\n display array element");
for(i=0;i<len;i++)
{
printf("\n %d",a[i]);
}
getch();

3
/8*Pointer*/ /9*Function Return More then one
#include<stdio.h> Value*/
#include<conio.h> #include<stdio.h>
void main() #include<conio.h>
{ void area(int ,float *, float *);
int i=10, *j, **k; void main()
clrscr(); {
j=&i; int r=1;
k=&j; float a,p;
printf("\n valuse of i=%d and Address of i=%u",i, &i); clrscr();
printf("\n value of i using j is %d and Address of i= area(r,&a,&p);
%u",*j, j); printf("\n area=%f \t para=%f", a,p);
printf("\n value of i using k is %d and Address of i= getch();
%u",**k, *k); }
printf("\n Valuse of k=%u", k); void area(int r,float *x, float *y)
getch(); {
*x=3.14*r*r;
} *y=2*3.14*r;
}
/10*SWAP using Pointer*/
#include<stdio.h>
#include<conio.h>
void swap(int *, int *);
void main()
{
int a=2, b=5;
clrscr();
printf("\n Before swap");
printf("a=%d \t b=%d", a,b);
printf("\n after swap");
swap(&a,&b);
printf("a=%d \t b=%d", a,b);
getch();
}
void swap(int *x, int *y)
{
*x= *x + *y;
*y= *x - *y;
*x= *x - *y;
}

You might also like