Friday, 22 June 2012

write a c program to accept m element from user and store those element into array. extend the size of an array to n, accept n-m element from the user and store that element into an array and display the complete array (using malloc() and realloc())


/*write a c program to accept m element from user and store those element
into array. extend the size of an array to n, accept n-m element from the
user and store  that element into an array and display the complete array
(using malloc() and realloc())*/
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void main()
{
int *p,m,n,i,j;
clrscr();
printf("enter how many size:-");
scanf("%d",&m);
p=(int*)malloc(m * sizeof(int));
printf("\nenter the element:\n");
for(i=0;i<m;i++)
scanf("%d",p+i);
printf("\nenter upto how many size you want to extend:-");
scanf("%d",&n);
if(n<m)
printf("\nplease enter size more than %d and try again",m);
else
{
m=m+(n-m);
p=(int *)realloc(p,m);
for(j=i;j<m;j++)
{
printf("\nenter the element:-");
scanf("%d",p+j);
}
printf("display of complete array:-\n");
for(i=0;i<m;i++)
printf("%d\n",*(p+i));
}
getch();
}
/*
enter how many size:-3
                                                                               
enter the element:                                                            
4                                                                              
8                                                                              
1                                                                              
                                                                               
enter upto how many size you want to extend:-4                                
                                                                               
enter the element:-3                                                          
display of complete array:-                                                    
4                                                                              
8                                                                              
1                                                                              
3
*/
/*
enter how many size:-4
                                                                               
enter the element:                                                            
4                                                                              
5                                                                              
6                                                                              
1                                                                              
                                                                               
enter upto how many size you want to extend:-2                                
                                                                               
please enter size more than 4 and try again
*/

No comments: