Write an C program to separate the even and odd elements of an array T[10] into two different arrays | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Write an C program to separate the even and odd elements of an array T[10] into two different arrays

22nd Apr 2022, 11:04 PM
Ahmed
9 Answers
+ 3
Create T, odd, even arrays. Create o, e as zero Read or assign T Sort T Read T is: For i in zero to nine Assign input to T[i] Sort T is: For i in zero to nine If odd (T[i]%2 == 1) Assign T[i] to odd[o] Increment o Else Assign T[i] to even[e] Increment e
22nd Apr 2022, 11:42 PM
John Wells
John Wells - avatar
+ 1
If the original array (T) is not meant to be maintained further, one can simply boil down the question to extracting elements of one parity (either even or odd, your choice ) from the array to another.
23rd Apr 2022, 6:30 AM
Arsenic
Arsenic - avatar
+ 1
Hopefully Ahmed did not give up. Here is some code to review. #include <stdio.h> #include <stdlib.h> int main(void){ int t[10] = {1,2,3,4,5,6,7,8,9,10}; int *even, ecount = 0; int *odd, ocount = 0; even = malloc(sizeof (int) * 10); odd = malloc(sizeof (int) * 10); for(int i = 0; i < 10; i++){ if(t[i] % 2 == 0){ even[ecount] = t[i]; ecount++; } else { odd[ocount] = t[i]; ocount++; } } even = reallocarray(even, ecount, sizeof (int)); odd = reallocarray(odd, ocount, sizeof (int)); puts("Even Numbers:"); for(int i = 0; i < ecount; i++) printf("%i\n", even[i]); puts("Odd Numbers:"); for(int i = 0; i < ocount; i++) printf("%i\n", odd[i]); exit(EXIT_SUCCESS); }
23rd Apr 2022, 1:24 PM
William Owens
William Owens - avatar
+ 1
Thank you guys for the answers. I finished it
23rd Apr 2022, 7:51 PM
Ahmed
0
I would malloc two arrays of size ten, then iterate over the original array separating the values into the new arrays by coping the values. Once all 10 values have been separated I would realloc the malloc'd arrays to their current size.
22nd Apr 2022, 11:24 PM
William Owens
William Owens - avatar
0
How much of that can you do?
22nd Apr 2022, 11:25 PM
William Owens
William Owens - avatar
0
Nothing probably
22nd Apr 2022, 11:32 PM
Ahmed
0
include <some standard input/output file> _start(void){ int t[10] = {1,2,3,4,5,6,7,8,9,10} int *even; int *odd; I did not write exact C code to see where you are... How do you rewrite this to start the program?
22nd Apr 2022, 11:37 PM
William Owens
William Owens - avatar
0
Ahmed we are here to help when you are ready.
22nd Apr 2022, 11:52 PM
William Owens
William Owens - avatar