Hi Everyone,
I need some help. So I am given this code and I have to make the functions for revArray, *reveElement, and evenOdd. So far I have figured out how to reverse the Array and print it. But I'm having some trouble sorting the array. I'm supposed to put the even numbers first then the odd numbers last in the array. But i can't figure out how... Any tips, hints, and help would be much appreciated. Thank you!
Given:
#include <stdio.h>
#include <stdlib.h>
#define N 9
/* MAIN */
int main(int argc, char **argv)
{
/* Load the array with numbers */
int a[N];
int idx;
int *p = a;
while(p < a + N) *p++ = a + N - p;
printf("Original: ");
p = a; while(p < a + N) printf("%2d ",*p++);
/* Part A: Reverse the array */
revArray(a);
printf("\nReversed: ");
p = a; while(p < a + N) printf("%2d ",*p++);
printf("\n");
/* Part B: Return elements in reverse order */
printf("Original: ");
for (idx = 0; idx < N; idx++) {
printf("%2d ",*revElement(a));
}
printf("\n");
/* Part C: Put even numbers first, odd numbers last in the array. Order of
the elements is not important as long as all evens are before first odd */
printf("Even: ");
evenOdd(a);
p = a; while(p < a + N) printf("%2d ",*p++);
printf("\n");
system("pause");
exit(0);
}
My Code:
#include <stdio.h>
#include <stdlib.h>
#define N 9
void revArray(int *p);
int *revElement(int *p);
void evenOdd(int *p);
/* MAIN */
int main(int argc, char **argv)
{
/* Load the array with numbers */
int a[N];
int idx;
int *p = a;
while(p < a + N) *p++ = a + N - p;
printf("Original: ");
p = a; while(p < a + N) printf("%2d ",*p++);
/* Part A: Reverse the array */
revArray(a);
printf("\nReversed: ");
p = a; while(p < a + N) printf("%2d ",*p++);
printf("\n");
/* Part B: Return elements in reverse order */
printf("Original: ");
for (idx = 0; idx < N; idx++) {
printf("%2d ",*revElement(a));
}
printf("\n");
/* Part C: Put even numbers first, odd numbers last in the array. Order of
the elements is not important as long as all evens are before first odd */
printf("Even: ");
evenOdd(a);
p = a; while(p < a + N) printf("%2d ",*p++);
printf("\n");
system("pause");
exit(0);
}
void revArray(int *p)
{
int arr[N], i;
int *s = p;
for(i = N-1; i >=0; --i)
{
arr[i] = *p++;
}
for(i = 0; i < N; ++i)
{
*(s+i) = arr[i];
}
return;
}
int *revElement(int *p)
{
static int idx2 = N;
idx2 = --idx2;
return p + idx2;
}
/*Sorting Array putting even numbers first, then odd. I can't figure out how to do this???*/
void evenOdd(int *p)
{
int b[N],j=0,c[N],k=0;
int arr[N], i;
for(i = 0; i < N; ++i)
{
if (arr[N] %2==0)
{
b[j]=arr[i];
j++;
}
else if (arr[N] %2 !=0)
{
c[k]=arr[i];
k++;
}
}
}