Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
for example an array a[] contain integer element if i want to search for the integer value=1 from a[] and store in new array b[]. Can anyone tell me how to do in c?
Posted

I am assuming that this is the same problem you asked about before: how to get specific element content in an array an store to another array ?[^]
In which case what you are trying to store is the index to the elements, not the elements themselves (which would make no sense at all, since the new array would be all "1"s).

This isn't difficult, in fact it's almost certainly your homework - so I won't give you the code!
Implement this:
1) Create a new integer array, the same size as the input array. Call it "indexes".
2) Create an integer, call "nextIndex", set it to zero.
3) Use a for loop to run through all elements of your inputArray, which a loop variable called "i".
3.1) If input array element "i" is equal to the value you are looking for ("1" in your example), then store the value of "i" into "indexes" at element "nextIndex" and increment "nextIndex" by one.
4) After the loop, "nextIndex" is the number of values you found, and "indexes" contains the indexes to the values in the input array.

Give it a try. This isn't difficult! :laugh:
 
Share this answer
 
Comments
mybm1 15-Jan-15 6:10am    
@OriginalGriff
ya u r right the previous question itself has the same logic but dude i tried that option an its running wen i applied the for loop till the size of input.for example input size =20 and their content four 1.
i want the new array to only of size 4 not the same size of the input.
OriginalGriff 15-Jan-15 6:18am    
You can't change the array size once you have created it - so if you want the indexes array to be the same size as the number of indexes, then you have to know how many elements it is going to contain *before* you create it.

Which means running the loop I describe twice: the first time just to count the elements, and the second to actually insert them after you create the array.
OriginalGriff 15-Jan-15 6:54am    
Sorry? That makes no sense to me!
All I get is:

for(thresholdcount=0;thresholdcount

mybm1 16-Jan-15 1:25am    
i tried still it is not working ?
OriginalGriff 16-Jan-15 4:52am    
Try editing your question and adding it there: then use the "encode HTML" and code block features to format it.

It's probably that you have a less than character in there, and it's being treated as an HTML tag, so we can't see it.
Hi,

As describe above you dont know the size of new array in that case you can use below,
Create an arraylist then converting that arraylist into array of type integer again.

Example:

// let assume you got a number from your previous array, That is 100

x=a[3];

// add this fetched value arraylist

ArrayList arrlist=New ArrayList();

//arrList.Add(a[3]); Or below
arrList.Add(x);
// after adding required elements to arraylist, You need to convert them to array of integers.

int[] arrResult=arrList.ToArray((TypeOf(int))) as int[];

//This arrResult array contains your value..

Thanks...Hope this solve your problem...
 
Share this answer
 
for(thresholdcount=0;thresholdcount<sample_length;thresholdcount++)>
{
 if(thxcross_pos[thresholdcount]==1)
	{
	//PZLoctn[thresholdcount]=thxcross_pos[thresholdcount];
	
	PZLoctn[thresholdcount]=thresholdcount;
	//printf("pzloctn:%f\n",PZLoctn[thresholdcount]);
	PZLoctn_counter=PZLoctn_counter+1;	
	}

   else if(thxcross_pos[thresholdcount]==-1)
        {
	//NZLoctn[thresholdcount]=thxcross_pos[thresholdcount];
	NZLoctn[thresholdcount]=thresholdcount;
       // printf("%f\n",NZLoctn[thresholdcount]);	
	NZLoctn_counter= NZLoctn_counter+1;
	}
}
printf("PZLoctn_counter:%d\tNZLoctn_counter: %d\n",PZLoctn_counter,NZLoctn_counter);
for(count=0;count<pzloctn_counter;count++)>
{
printf("\nnew array:%f",PZLoctn[count]);
}
 
Share this answer
 

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