0% found this document useful (0 votes)
24 views10 pages

Acp Part A Programs

The document contains C code to perform various operations on 1D and 2D arrays: 1) Functions to insert, delete by position, and delete by value in a 1D array. 2) Functions to read, display, calculate row sum, column sum, primary diagonal sum, and secondary diagonal sum of a 2D array. 3) A function to swap two integer values by passing their addresses.

Uploaded by

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

Acp Part A Programs

The document contains C code to perform various operations on 1D and 2D arrays: 1) Functions to insert, delete by position, and delete by value in a 1D array. 2) Functions to read, display, calculate row sum, column sum, primary diagonal sum, and secondary diagonal sum of a 2D array. 3) A function to swap two integer values by passing their addresses.

Uploaded by

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

/*A1.

Write C program to accept and display 1D array


Also write functions.
i)to insert an element at the specified position
ii)to delete element based on the position
iii)to delete based on the value
function should take care of invalid data and accordingly display
appropriate error messages.
*/
#include<stdio.h>
int main()
{
int a[20],n,pos,key,ele,choice;
int read_array(int*);
void display_array(int*,int);
int insert_at_pos(int*,int*,int,int);
int delete_key_element(int*,int*,int,int*);
int delete_at_pos(int*,int*,int,int*);

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;
}

/*function to load array & return the size*/


int read_array(int *pa)
{
int n;
printf("input size?\n");
scanf("%d",&n);
printf("input %d no of elements\n",n);
for(int i=0;i<n;i++)
scanf("%d",(pa+i));
return n;
}

/*function to display array takes array & size as parameter*/


void display_array(int*pa,int n)
{
printf("cell no \t address \t value\n");
for(int i=0;i<n;i++)
printf("a[%d]\t\t %p\t\t %d\n",i,(pa+i),*(pa+i));
}

/*
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

return value: 1 successful insertion 0 failure


*/
int delete_key_element(int*pa,int*pn,int key ,int*ppos)
{ *ppos=-1;
for(int i=0;i<*pn;i++)
{
if(*(pa+i)==key)
{
*ppos=i+1;
break;
}
}

if(*ppos==-1)
return 0;

for ( int j=(*ppos)-1;j<(*pn)-1;j++)


pa[j]=pa[j+1];
(*pn)--;
return 1;
}

/* A2.Write C program to accept and display 2d array of user specified


size.Also write functions to perform the following on the 2d array
I. Function row_sum that takes row number as parameter and returns the
sum of the row
II. Function col_sum that takes column number as parameter and returns
the sum of the column
III. Function secondary _diagonal_sum that returns the sum of
secondary diagonal elements if possible else should return -1
IV. Function primary_diagonal_sum that returns the sum of primary
diagonal elements if possible else should return -1
*/
#include<stdio.h>
int main()
{
void read_array(int[][5],int,int);
void display_array(int[][5],int,int);
int row_sum(int [][5],int,int);
int col_sum(int [][5],int,int);
int sec_diagonal_sum(int [][5],int,int);
int pri_diagonal_sum(int [][5],int,int);
int nr,nc,ch,kr,kc,res;
int a[5][5];
printf("input no of rows & columns < =5\n");
scanf("%d %d",&nr,&nc);
read_array(a,nr,nc);
display_array(a,nr,nc);
while(1)
{
printf("1-rowsum\n2-colsum\n3-principal diagonal sum\n4-
secondary diagonal sum\n 5-display\nanyother terminate\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("input row number of the row to be summed
valid range 1 to %d\n",nr);
scanf("%d",&kr);
if(kr>nr || kr<0)
printf("invalid row no");
else
{
res=row_sum(a,kr-1,nc);
printf("sum of elements of %d row
is=%d\n",kr,res);
}
break;
case 2:printf("input column number of the row to be
summed valid range 1 to %d\n",nc);
scanf("%d",&kc);
if(kc>nc || kc<0)
printf("invalid row no");
else
{
res=col_sum(a,kc-1,nr);
printf("sum of elements of %d col
is=%d\n",kc,res);
}
break;
case 3: res=pri_diagonal_sum(a,nr,nc);
if(res==-1)
printf("not square matrix\n");
else
printf("sum of principal diagonal elements
is=%d\n",res);
break;
case 4: res=sec_diagonal_sum(a,nr,nc);
if(res==-1)
printf("not square matrix\n");
else
printf("sum of secondary diagonal elements
is=%d\n",res);
break;
case 5:display_array(a,nr,nc);
break;
default: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
*/
void read_array(int a1[][5],int nr1,int nc1)
{
for(int row=0;row<nr1;row++)
{
printf("input %d no of elements of %d row\n",nc1,row);
for(int col=0;col<nc1;col++)
scanf("%d",&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
nr1:number of rows
nc1:number of columns
returns row sum or -1
*/

void display_array(int a1[][5],int nr1,int nc1)


{
printf("contents are \n");
for(int row=0;row<nr1;row++)
{

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;
}

/*A3.Swap of two integers*/

#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;
}

void swap(int *pa,int *pb)


{
printf("within function before exchange\n");
printf("a=%d\tb=%d\n",*pa,*pb);
int t;
t=*pa;
*pa=*pb;
*pb=t;
printf("within function after exchange\n");
printf("a=%d\tb= %d\n",*pa,*pb);
}

/*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);
}

/*A5. Write C program to store


information(name,employee_id,designation,date of birth,stay details)
about set of employees in a company. Here
designation is string that can take one of these values {md,
manager,clerk,peon}
date_ of_ birth is a structure for holding birth date with fields
day,month,year
stay_detail is a structure that contains street number and sector
number and house number details.
Write separate functions to accept & display the employees*/
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
typedef struct
{
int year,month,day;
}date;

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));

You might also like