How to split an array into two arrays, one for even and one for odd in C
Un-initialized variables can contain anything, it is better to start of by initializing.
This
int array100[100];
int arraypar[100];
int arrayimpar[100];
Should be:
int array100[100] = {0};
int arraypar[100] {0};
int arrayimpar[100] = {0};
And since C uses 0 base array indexing, you are probably seeing a run time error; Dereference of out-of-bounds pointer
(or similar) if the code shown in your post is the actual code you are using.
This:
for(i=1;i<=100;i++){
^ ^^
printf("%i ", arraypar[i]);
}
Should be this:
for(i=0;i<100;i++){
^ ^
printf("%i ", arraypar[i]);
}
Same for the next for loop.
Also, a suggestion to help you troubleshoot. I would start off by initializing array100
with a known set of sequential values to easily test whether the values are being split properly:
int array100[100] = {1,2,6,4,5,6,7,8,9,10,
11,12,16,14,15,16,17,18,19,
20,21,22,26,24,25,26,27,28,29,
60,61,62,66,64,65,66,67,68,69,
40,41,42,46,44,45,46,47,48,49,
50,51,52,55,54,55,56,57,58,59,
60,61,62,66,64,65,66,67,68,69,
70,71,72,77,74,75,77,77,78,79,
80,81,82,88,84,85,88,87,88,89,
90,91,92,99,94,95,99,97,98,99,100};
Now run your program with the rand()
section commented out to see what you get. If there are problems, they will be easier to see then looking at a collection of randomly generated numbers.
Divide array into two arrays and set values to odd and even
You need to check the value, not the index.
if (arrayMain[i] % 2 == 0) {
// ^^^^^^^^^^ ^
Split array into two arrays by index even or odd
One solution, using anonymous functions and array_walk
:
$odd = array();
$even = array();
$both = array(&$even, &$odd);
array_walk($array, function($v, $k) use ($both) { $both[$k % 2][] = $v; });
This separates the items in just one pass over the array, but it's a bit on the "cleverish" side. It's not really any better than the classic, more verbose
$odd = array();
$even = array();
foreach ($array as $k => $v) {
if ($k % 2 == 0) {
$even[] = $v;
}
else {
$odd[] = $v;
}
}
split an array into two arrays based on odd/even position
You could try:
var Arr1 = [1,1,2,2,3,8,4,6],
Arr2 = [],
Arr3 = [];
for (var i=0;i<Arr1.length;i++){
if ((i+2)%2==0) {
Arr3.push(Arr1[i]);
}
else {
Arr2.push(Arr1[i]);
}
}
console.log(Arr2);
JS Fiddle demo.
How to split an Array into Odd Array and Even Array
You've to use charCodeAt method because allData is an array of string not array of numbers.
It's the solution of your problem:
if ( ( allData[i].charCodeAt(0) % 2 ) === 0) { your code }
=================
as Thomas Scheffer mentioned it seems u want to sort based on character index not its value .
so if u want to sort based on index u have to write :
if ( ( i % 2 ) === 0) { your code }
it converts like this :
allData = ["b","f","z","w"] => { odd=["f","w"], even=["b","z"] }
but if u want to sort based on character value u have to write :
if ( ( allData[i].charCodeAt(0) % 2 ) === 0) { your code }
it converts like this :
allData = ["b","f","z","w"] => { odd=["w"] , even=["b","f","z"] }
Divide/Split an array into two arrays one with even numbers and other with odd numbers
Your error is in if
condition, you want to check if the number is odd or even, you have to use modulus %
operator. So your code becomes like this
<?php $array = array(1,2,3,4,5,6);
$length = count($array);
$even = array();
for($i=0; $i < $length; $i++){
if($array[$i]%2 == 0){
$even[] = $array[$i];
}
else{
$odd[] = $array[$i];
}
}
print_r($even);
echo "<br/>";
print_r($odd);
Objective-C Split an array into two separate arrays based on even/odd indexes
There are following ways you can achieve that:-
The first and second one solution are already mentioned by the above two. Below are the implementation of the same:-
//First Solution
NSArray *ar=@[@"1",@"2",@"3",@"4",@"5"];
NSMutableArray *mut1=[NSMutableArray array];
NSMutableArray *mut2=[NSMutableArray array];
[ar enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) {
if (idx%2==0)
{
[mut1 addObject:object];
}
else
{
[mut2 addObject:object];
}
}];
//Second Solution
NSMutableIndexSet *idx1 = [NSMutableIndexSet indexSet];
NSMutableIndexSet *idx2 = [NSMutableIndexSet indexSet];
for (NSUInteger index=0; index <ar.count(); index++)
{
if(index%2==0)
{
[idx1 addIndex:index];
}
else{
[idx2 addIndex:index];
}
}
NSArray *evenArr=[ar objectsAtIndexes:idx1];
NSArray *oddArr=[ar objectsAtIndexes:idx2];
NSLog(@"%@",evenArr);
NSLog(@"%@",oddArr);
C Array Split By Odd Even Indexes Gives Garbage Values
Any access to a[i]
or b[i]
will be out of bounds when i
is greater than 2. You should be using the aa
and bb
indices when filling the a[]
and b[]
arrays.
Also, the test for even or odd indices can be simplified since 0 is even.
Here is the fixed version of the array splitting loop:
for (i = 0; i < n; i++)
{
if (i % 2 == 0)
{
// even
a[aa] = arr[i];
// For debug purpose
printf("a[%d]=%d, arr[%d]=%d, aa=%d\n", aa, a[aa], i, arr[i], aa);
aa++;
}
else
{
// odd
b[bb] = arr[i];
// For debug purpose
printf("b[%d]=%d, arr[%d]=%d, bb=%d\n", bb, b[bb], i, arr[i], bb);
bb++;
}
}
Related Topics
Generating Random Numbers from Skewed Normal Distribution
Converting an Array from One to Multi-Dimensional Based on Parent Id Values
How to Put JavaScript Variable in PHP Echo
Mysqli_Stmt::Execute() Expects Exactly 0 Parameters, 1 Given
How to Remove the Leading Character from a String
Correct Indentation of HTML and PHP Using Vim
Codeigniter: Try Catch Is Not Working in Model Class
Php:Binary Image Data, Checking the Image Type
Column Count of MySQL.Proc Is Wrong. Expected 20, Found 16. the Table Is Probably Corrupted
Understanding Imagemagick's Convert and Translating to Ruby Rmagick
Changing Woocommerce Cart Item Names
How to Create Codeigniter Language Files from Database
Codeigniter Sessions Not Working After Migration
Resetting MySQL Root Password with Xampp on Localhost
Symfony2 Routing - Route Subdomains