Acp Part A Programs
Acp Part A Programs
n=read_array(a);
printf("Elements are\n");
display_array(a,n);
while(1)
{
printf("1-display\n2-insert\n3-delete based on position\n4-delete
key element\ndefault stop\n");
printf("input choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("Elements are\n");
display_array(a,n);
break;
case 2:printf("input element & position of insertion starting
from 1 upto %d\n", n);
scanf("%d %d",&ele,&pos);
if(!insert_at_pos(a,&n,pos,ele))
printf("sorry invalid position\n");
else
printf("successfull insertion\n");
break;
case 3:printf("input position of deletion starting from 1 upto
%d\n", n);
scanf("%d",&pos);
if(!delete_at_pos(a,&n,pos,&ele))
printf("sorry invalid position\n");
else
printf("successfully deleted %d element at %d
position\n",ele,pos);
break;
case 4:printf("input key element to be deleted\n") ;
scanf("%d",&key);
if(!delete_key_element(a,&n,key,&pos))
printf("sorry %d do not exist\n",key);
else
printf("successfully deleted %d element at position
%d\n",key,pos);
break;
default:return(0);
}
return 0;
}
/*
task:to insert an element at the specified position
pa: beginning address of array
pn :points to location(n) containing no of elements
pos:position of insertion
ele:element to be inserted
return value: 1 successful insertion 0 failure
*/
int insert_at_pos(int*pa,int*pn,int pos,int ele)
{
if(pos>(*pn)+1|| pos<=0)
return 0;
else
{
for ( int j=*(pn)-1;j>=pos-1;j--)
{printf("***%d",j);
pa[j+1]=pa[j];
}
pa[pos-1]=ele;
(*pn)++;
printf("***%d",*pn);
return 1;
}
}
/*
task:to delete an element at the specified position
pa: beginning address of array
pn :points to location(n) containing no of elements
pos:position of deletion
pe:deleted element
return value: 1 successful insertion 0 failure
*/
int delete_at_pos(int*pa,int*pn,int pos,int*pe)
{
if(pos>*pn|| pos<=0)
return 0;
else
{
*pe=pa[pos-1];
for ( int j=pos-1;j<*pn-1;j++)
pa[j]=pa[j+1];
(*pn)--;
return 1;
}
}
/*
task:to delete key element
pa:beginning address of array
pn :points to location(n) containing no of elements
key:key element to be deleted
ppos:pointer to location(pos)where position(index) of deleted element
is placed
if(*ppos==-1)
return 0;
}
/* returns row sum of specified row , if square matrix else returns -1
a1:2d array
kr1:row number to be summed
nr1:number of rows
nc1:number of columns
returns row sum or -1
*/
for(int col=0;col<nc1;col++)
printf("%d\t",a1[row][col]);
printf("\n");
}
}
/* returns row sum of specified row , if square matrix else returns -1
a1:2d array
kr1:row number to be summed
nc1:number of columns
returns row sum or -1
*/
int row_sum(int a1[][5],int kr1,int nc1)
{
int rsum=0;
for(int col=0;col<nc1;col++)
rsum+=a1[kr1][col];
return rsum;
}
/* returns column sum of specified column, if square matrix else returns
-1
a1:2d array
kc1:column number to be summed
nr1:number of rows
returns column sum or -1
*/
int col_sum(int a1[][5],int kc1,int nr1)
{
int csum=0;
for(int row=0;row<nr1;row++)
csum+=a1[row][kc1];
return csum;
}
/* returns secondary diagonal sum,if square matrix else returns -1
a1:2d array
nr1:number of rows
nc1:number of columns
returns secondary diagonal sum or -1
*/
int sec_diagonal_sum(int a1[][5],int nr1,int nc1)
{
int row,col,ssum=0;
if(nr1==nc1)
{
for(row=0,col=nc1-1;(row<nr1 && col>=0);row++,col--)
ssum+=a1[row][col];
return ssum;
}
else
return -1;
}
/* returns principal diagonal sum,if square matrix else returns -1
a1:2d array
nr1:number of rows
nc1:number of columns
returns principal diagonal sum or -1
*/
int pri_diagonal_sum(int a1[][5],int nr1,int nc1)
{
int psum=0;
if(nr1==nc1)
{
for(int row=0;row<nr1;row++)
psum+=a1[row][row];
return psum;
}
else
return -1;
}
#include<stdio.h>
int main()
{
int a,b;
void swap(int*,int*);
printf("input two integers\n");
scanf("%d %d",&a,&b);
printf("before swap values of a &b are\n");
printf("a=%d\tb=%d\n",a,b);
swap(&a,&b);
printf("after swap values of a &b are\n");
printf("a=%d\tb=%d\n",a,b);
return 0;
}
/*A4.Write program to accept and display(both forward & reverse) and sum
array elements using pointer arithmetic
* Write C program to accept and display 1d array.Use external pointer to
process the array.Use separate functions to
• Accept the array elements
• Display the array elements in forward direction
• Display the array elements in reverse diretion
• To compute the average of the elements in the array*/
#include<stdlib.h>
#include<stdio.h>
int main()
{
int a[10],n;
int accept_array(int*);
void display_forward(int*,int);
void display_reverse(int*,int*);
float average(int*,int);
int *pb=a,*pe;
n=accept_array(pb);
display_forward(pb,n);
pe=a+n-1;
display_reverse(pe,pb);
printf("average=%f\n",average(pb,n));
return 0;
}
int accept_array(int*pb)
{
int n;
printf("size<10?\n");
scanf("%d",&n);
/*accepting values*/
printf("input %d integers\n",n);
for (int i=0;i<n;i++)
scanf("%d",(pb+i));
return n;
}
void display_forward(int*pb,int n)
{
int*pe=pb+n-1,*pw;
printf("array elements in forward direction\n");
/*print forward direction using external pointer*/
for(pw=pb;pw<=pe;pw++)
printf("address= %p\tvalue= %d\n",pw,*pw);
}
void display_reverse(int*pe,int*pb)
{
/*reverse printing*/
printf("array elements in reverse order\n");
for(int*pw=pe;pw>=pb;pw--)
printf("%d\n",*pw);
}
float average(int*pb,int n)
{
int sum=0;
/*summing array elements*/
for (int i=0;i<n;i++)
sum+=*(pb+i);
return(sum/n);
}
typedef struct
{
int street_no,sector_no,house_no;
}address;
typedef struct
{
int emp_id;
char name[15];
char desig[15];
address con_add;
date dob;
}employee;
int main()
{
employee emp[20];
int n;
int accept_details(employee[]);
void display_details(employee[],int);
n=accept_details(emp);
display_details(emp,n);
return 0;
}
int accept_details(employee e[])
{
int i,n,d;
printf("input total number of employees\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("input employee id & name\n");
scanf("%d %s",&(e[i].emp_id),e[i].name);
printf("input 1 for md\n2-for manager\n3-for clerk\n4-
peon\n");
scanf("%d",&d);
switch(d)
{
case 1:strcpy(e[i].desig,"md");
break;
case 2:strcpy(e[i].desig,"manager");
break;
case 3:strcpy(e[i].desig,"clerk");
break;
case 4:strcpy(e[i].desig,"peon");
}
printf("contact address details\n");
printf("street_no\tsector_no\thouse no\n");
scanf("%d %d
%d",&(e[i].con_add.street_no),&(e[i].con_add.sector_no),&(e[i].con_add.ho
use_no));
printf("birth date details \n");
printf("day\tmonth\tyear\n");
scanf("%d %d
%d",&(e[i].dob.day),&(e[i].dob.month),&(e[i].dob.year));
}
return n;
}
void display_details(employee e[10],int n)
{
int i;
printf("employees details\n");
for(i=0;i<n;i++)
{
printf("___________________________________________________________
________\n");
printf("employee id=%d\t name=
%s\n",(e[i].emp_id),e[i].name);
printf("designation =%s\n",(e[i].desig));
printf("contact address
details\nstreet_no,sector_no,house_no\n");
printf("street_no=%d\tsector_no= %d\t,house_no=
%d\n",(e[i].con_add.street_no),(e[i].con_add.sector_no),(e[i].con_add.hou
se_no));
printf("birth date details day month year\n");
printf("day=%d
\tmonth=%d\tyear=%d\n",(e[i].dob.day),(e[i].dob.month),(e[i].dob.year));