Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include<stdio.h>
int count;
void create(float seta[],int n1,float tempa[] );
int main()
{ int i;
float *seta; float *setb; float *tempa; int n1,n2;
printf("enter the elements of set 1:\n");
printf("enter the number of elements in set 1:\n");
scanf("%d",&n1);
seta=(float*)malloc(n1*sizeof(float));
for(i=0;i<n1;i++)
{
scanf("%f",seta+i);


}

printf("enter the elements of set 2:\n");
printf("enter the number of elements in set 2:\n");
scanf("%d",&n2);
setb=(float*)malloc(n2*sizeof(float));
for(i=0;i<n2;i++)
{
scanf("%f",setb+i);


}
create(seta,n1,tempa);
for(i=0;i<count;i++)
printf("%d",*(tempa+i));


return 0;
}
void create(float a[20],int n1,float tempa[20])
{int count=1,i,j,countverify;a[0]=tempa[0];
for(i=0;i<n1;i++)
{countverify=0; for(j=0;j<count;j++)
if(tempa[j]!=a[i]) {countverify++;}
if(countverify==count)
{
tempa[count]=a[i]; count++; }
}


} /*the set b can be removed if you want.it has been declared to do furthur set opwrations at later stages but first let me know what the error is .my compiler stopped after scanning the elements*/
Posted
Updated 13-Mar-15 8:52am
v2
Comments
nv3 13-Mar-15 18:40pm    
And what is your question?
Member 11324568 14-Mar-15 0:04am    
Please read till the end to find it.
nv3 14-Mar-15 4:30am    
I did. But you neither took the time to put your code in <pre> tags, nor to explain exactly what you want to know. "the compiler stopped after scanning the elements" doesn't make much sense.

1 solution

You malloc seta and setb - but you don;t allocate anything to tempa

And it's not "your compiler" that stopped - the compiler just converts your C code into an executable application. Once that's complete, your app is on it's own; the compiler in not involved any further.
Instead of just going "the compiler stopped after...", use the debugger. Set a break point and follow the code through to find out exactly what is happening. It's a skill, and it';s well worth developing when you are still working on small projects.

And BTW: it's good practice to free all memory you malloc, even if you are going to exit the program. If you get into the habit, you don't start getting horrible memory leak problem in more complex projects.
 
Share this answer
 
Comments
Member 11324568 14-Mar-15 0:09am    
That doesn't stop my app from crashing.and even though we don't allocate space we can assign few values because the addresses are assigned randomly.i.e by allocating space for 1 float value we can store 10 floats or by allocating 0 floats we can store 10 floats.that's not the error in the program.
OriginalGriff 14-Mar-15 3:19am    
:sigh:

Let me put it like this: if you want to store information on a filling cabinet, you put it in a particular folder in a particular drawer, yes? That way you know where it is going, and that it's a sensible place to store it.

Your approach is to store it randomly somewhere within the building and hope that it doesn't get treated as part of something totally different.

You have to allocate memory correctly, or you will get unpredictable effects which very very often involve your all crashing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900