#include<stdio.h> #include<conio.h> #include<stdlib.h> void Union(int *a,int *b,int m,int n) { int *c,i,j,k=0,flag=0; c=(int *)malloc(sizeof(int)*m); for(i=0;i<m;i++) { c[k]=a[i]; k++; } for(i=0;i<n;i++) { flag=0; for(j=0;j<m;j++) { if(b[i]==c[j]) { flag=1; break; } } if(flag==0) { c[k]=b[i]; k++; } } printf("\nElement of resultant set\n\n"); for(i=0;i<k;i++) { printf("\t%d",c[i]); } } int main() { int *a,*b,m,n,i,j; int ch; //clrscr(); printf("\nEnter the number of elements in first set:\n"); scanf("%d",&m); a=(int *)malloc(sizeof(int)*m); printf("\nEnter the elements:\n"); for(i=0;i<m;i++) { scanf("%d",&a[i]); } printf("\nElement of First set:\n\n"); for(i=0;i<m;i++) { printf("%d\t",a[i]); } printf("\nEnter the number of elements in second set:\n"); scanf("%d",&n); b=(int *)malloc(sizeof(int)*n); printf("\nEnter the elements:\n"); for(i=0;i<n;i++) { scanf("%d",&b[i]); } printf("\nElement of second set\n"); for(i=0;i<n;i++) { printf("%d\t",b[i]); } Union(a,b,m,n); for(i=0;i<m;i++) free(a++); for(i=0;i<m;i++) free(b++); free(a); free(B)/>; return 0; getchar(); }
Logical error Problem! Pointers 2d array
Page 1 of 14 Replies - 1008 Views - Last Post: 17 January 2013 - 10:46 PM
#1
Logical error Problem! Pointers 2d array
Posted 31 December 2012 - 03:48 AM
Replies To: Logical error Problem! Pointers 2d array
#2
Re: Logical error Problem! Pointers 2d array
Posted 31 December 2012 - 04:15 AM
Well, when you say 'logical error'...what do you mean by it...is it something to do with erroneous output?
It would certainly help if you could post a sample of an input-output pair so that we can see where exactly your program is going wrong.
regards,
Raghav
It would certainly help if you could post a sample of an input-output pair so that we can see where exactly your program is going wrong.
regards,
Raghav
#3
Re: Logical error Problem! Pointers 2d array
Posted 31 December 2012 - 06:05 AM
#4
Re: Logical error Problem! Pointers 2d array
Posted 31 December 2012 - 06:38 AM
First find an indentation style you like and use it consistently, it will make reading your program much easier.
Your problem is that you are misusing the free() function in the following snippet:
First how many times do you think you need to free memory you allocate with malloc?
Next why are you using the increment operator on a freed pointer?
You don't need either of these loops so just get rid of them. Also you are not freeing the memory you allocated in your function.
Jim
Your problem is that you are misusing the free() function in the following snippet:
for(i=0; i<m; i++) free(a++); for(i=0; i<m; i++) free(b++); free(a); free(B)/>;
First how many times do you think you need to free memory you allocate with malloc?
Next why are you using the increment operator on a freed pointer?
You don't need either of these loops so just get rid of them. Also you are not freeing the memory you allocated in your function.
Jim
#5
Re: Logical error Problem! Pointers 2d array
Posted 17 January 2013 - 10:46 PM
Thank you sir for giving your time.
Page 1 of 1