"Before proceeding, new students should understand that programming practice is not limited to only a single file. When you are developing large program, functions can be written in one or more programs and in main program we are using that function by using #include "headerfile.h" on the top of file. Following example is not a large program but is used to demonstrate formerly mentioned idea."
"array.h" (header file)
#include<stdio.h>
#include "array.h"
#include<stdlib.h>
int cnt=0;
unsigned N;
void delete(int ar[],int pos)
{
int i;
for(i=pos;i<N;i++)
{
ar[i]=ar[i+1];
}
cnt++;
}
int main()
{
printf("\nHow many nos. you want to enter:");
scanf("%u",&N);
int *ar;
ar=(unsigned *)malloc(N*sizeof(unsigned));
int ask=1;
unsigned pos;
printf("\nEnter the elements of array:\n");
input(ar,N);
while(ask==1)
{
if(cnt==N)
{ printf("\nNo elements to delete");
break;
}
printf("\nEnter the position-index of array at which you want to delete the entry:");
scanf("%u",&pos);
if(pos>=N-cnt)
printf("\nOut of range");
else
delete(ar,pos);
printf("\n\n");
display(ar,N-cnt);
printf("\nDo you want to delete more elements(1/0):");
scanf("%d",&ask);
}
return 0;
}
"array.h" (header file)
void input(int *arr,int N)
{
int i;
for(i=0;i<N;i++)
{
printf("arr[%d]:",i);
scanf("%d",&arr[i]);
}
}
void display(int *arr,int N)
{
int i;
printf("\n Elements of array are :");
for(i=0;i<N;i++)
{
printf(" arr[%d]:%d" ,i,arr[i]);
printf("\n");
}
}
{
int i;
for(i=0;i<N;i++)
{
printf("arr[%d]:",i);
scanf("%d",&arr[i]);
}
}
void display(int *arr,int N)
{
int i;
printf("\n Elements of array are :");
for(i=0;i<N;i++)
{
printf(" arr[%d]:%d" ,i,arr[i]);
printf("\n");
}
}
"entry_delete.c" (main file)
#include "array.h"
#include<stdlib.h>
int cnt=0;
unsigned N;
void delete(int ar[],int pos)
{
int i;
for(i=pos;i<N;i++)
{
ar[i]=ar[i+1];
}
cnt++;
}
int main()
{
printf("\nHow many nos. you want to enter:");
scanf("%u",&N);
int *ar;
ar=(unsigned *)malloc(N*sizeof(unsigned));
int ask=1;
unsigned pos;
printf("\nEnter the elements of array:\n");
input(ar,N);
while(ask==1)
{
if(cnt==N)
{ printf("\nNo elements to delete");
break;
}
printf("\nEnter the position-index of array at which you want to delete the entry:");
scanf("%u",&pos);
if(pos>=N-cnt)
printf("\nOut of range");
else
delete(ar,pos);
printf("\n\n");
display(ar,N-cnt);
printf("\nDo you want to delete more elements(1/0):");
scanf("%d",&ask);
}
return 0;
}





0 comments