home
Learn
screen_rotation
#include <stdio.h> #include <stdlib.h> #include <conio.h> void main(){ int n1,n2,i,*ptr,sum=0,newSum=0; clrscr(); printf("\nEnter size of array: "); scanf("%d",&n1); ptr=(int*)malloc(n1 * sizeof(int)); if(ptr==NULL){ printf("\nSorry! Number of element has not given by you."); exit(0); } for(i=0;i< n1;i++){ printf("\nEnter elements of array: "); scanf("%d",ptr+i); } for(i=0;i< n1;i++){ printf("\n given element: %d",*(ptr+i)); sum = sum + *(ptr+i); } printf("\n Sum=%d",sum); printf("\nEnter new size of array: "); scanf("%d",&n2); ptr = (int *)realloc(ptr, n2); for(i=0;i< n2;i++){ printf("\nEnter new elements of array: "); scanf("%d",ptr+i); } for(i=0;i< n2;i++){ printf("\n given new element: %d",*(ptr+i)); newSum = newSum + *(ptr+i); } printf("\n New Sum=%d",newSum); free(ptr); getch(); }
Program Output
Enter size of array: 5 Enter elements of array: 10 Enter elements of array: 20 Enter elements of array: 30 Enter elements of array: 40 Enter elements of array: 50 given element: 10 given element: 20 given element: 30 given element: 40 given element: 50 Sum=150 Enter new size of array: 7 Enter new elements of array: 10 Enter new elements of array: 20 Enter new elements of array: 30 Enter new elements of array: 40 Enter new elements of array: 50 Enter new elements of array: 60 Enter new elements of array: 70 given new element: 10 given new element: 20 given new element: 30 given new element: 40 given new element: 50 given new element: 60 given new element: 70 New Sum=280