+ 1
Write an C program to separate the even and odd elements of an array T[10] into two different arrays
9 Réponses
+ 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
+ 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.
+ 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);
}
+ 1
Thank you guys for the answers. I finished it
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.
0
How much of that can you do?
0
Nothing probably
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?
0
Ahmed we are here to help when you are ready.