Even / Odd Help

The program works except when I place the evenOdd function in it. I get a bunch of errors. I need to get the program to display the even numbers first, then the odd following...I am stuck. This is what the output is supposed to look like:

Original: 9 8 7 6 5 4 3 2 1
Reversed: 1 2 3 4 5 6 7 8 9
Original: 9 8 7 6 5 4 3 2 1
Even: 2 4 6 8 5 3 7 1 9

This is what I have so far:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define N 9
/* MAIN */

void revArray(int arr[])
{
int temp[N];
int i;
for(i = 0; i < N; ++i)
{
temp[i] = arr[N - 1 -i];
}
for(i = 0; i < N; ++i)
{
arr[i] =temp[i];
}
}
int *revElement(int arr[])
{
static int idx = N;
idx = --idx;
return arr + idx;
}
void evenOdd(int *arr[])
{
int arr[N];
int i;
for(i = 0; i < N; ++i)
{
if (arr[N] %2 == 0)
*arr;
}
}

int main(int argc, char **argv)
{

int a[N];
int idx;
int *p = a;

printf("Enter %d numbers: ", N);
scanf("%d", a);
for (p = a; p < a + N; p++)
{

printf("In reverse order:");
}
for (p = a + N - 1; p >= a; p--)
{
printf(" %d", *p);
printf("\n");
}
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");
printf("Original: ");
for (idx = 0; idx < N; idx++) {
printf("%2d ",*revElement(a));
}
printf("\n");
printf("Even: ");
evenOdd(a);
p = a; while(p < a + N) printf("%2d ",*p++);
printf("\n");
system("pause");
}
}






A quick list of problems I can see in evenOdd:

you've reused the variable name arr - if you just rename one of them the compiler will be a little bit happier

the function should probably take int [] as its argument rather than int *[] - it doesn't need a pointer to a pointer and that'll also make your call in main () legit: it currently should be evenOdd(&a);

then, in the if statement: what is this trying to do? *arr is not a command or an operation, you have to actually tell it to do something. also, shouldn't it be talking about arr[i] rather than arr[N]?

because of that if statement, your actual algorithm is unclear, but my suggestion would be this: have two arrays - one for even numbers and one for odd, sort arr into those two and then concatenate them at the end
I am unsure how to do that
closed account (ETAkoG1T)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <stdio.h> 
#include <stdlib.h>
#include <math.h>

#define N 9 
/* MAIN */ 

void revArray(int arr[])
{
int temp[N];
int i;
for(i = 0; i < N; ++i)
{
temp[i] = arr[N - 1 -i];
}
for(i = 0; i < N; ++i)
{
arr[i] =temp[i];
}
}
int *revElement(int arr[])
{
static int idx = N;
idx = --idx;
return arr + idx;
}
void evenOdd(int *arr[])
{
int arr[N]; 
int i;
for(i = 0; i < N; ++i)
{
if (arr[N] %2 == 0)
*arr;
}
}

int main(int argc, char **argv) 
{ 

int a[N]; 
int idx; 
int *p = a; 

printf("Enter %d numbers: ", N);
scanf("%d", a);
for (p = a; p < a + N; p++)
{

printf("In reverse order:");
}
for (p = a + N - 1; p >= a; p--)
{
printf(" %d", *p);
printf("\n");
}
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");
printf("Original: ");
for (idx = 0; idx < N; idx++) {
printf("%2d ",*revElement(a));
}
printf("\n");
printf("Even: ");
evenOdd(a);
p = a; while(p < a + N) printf("%2d ",*p++);
printf("\n");
system("pause");
}
}

Remember the code tags
It keeps giving me this error:
declaration of 'int arr[9]' shadows a parameter

what does that mean??
If s is a string and x= 1/2* length(s) the following will rearrange s so that odd digits/chars are at one end and even digits/chars are at the other. Remember
C delights in the use of pointers.

1
2
3
4
5
6
7
for(int i=0;i<x;i++) 
   {    
    if(*p1%2==1)*p1++ ;//advance p1 if not pointing to even number
    if(*p2%2==0)*p2-- ;//reversse p2 if not pointing at odd number
    if(*p1%2==0 && *p2%2==1)swap(*p1,*p2);//only swap p1 even and p2 odd
    
  }
I have edited the program, and this is what I have so far:
(Everything works, except there is something wrong with the even/odd calculations...it gives weird numbers) Any ideas??


#include <stdio.h>
#include <stdlib.h>
#define N 9 /* MAIN */


void revArray(int arr[])
{
int temp[N];
int i;
for(i = 0; i < N; ++i)
{
temp[i] = arr[N - 1 -i];
}
for(i = 0; i < N; ++i)
{
arr[i] =temp[i];
}
}
int *revElement(int arr[])
{
static int idx = N;
idx = --idx;
return arr + idx;
}
void evenOdd(int odd[])
{
int i;
int evenTemp[N/2];
int oddTemp[(N-1)/2];

char tempOdd;
for(i=0; i<(N-1)/2; i++)
{
if(odd[i]%2 == 0) /* if even integer*/
evenTemp[i] = odd[i]; /*store even numbers temporary */
else
oddTemp[i]= odd[i]; /*store odd numbers temporary */
}

for(i=0; i<(N-1)/2; i++)
{
odd[i]=evenTemp[i]; /*swap even numbers first in the array*/
odd[N-1-i]=oddTemp[i]; /*swap odd numbers last in the array */

}
}



int main(int argc, char **argv)
{


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");
}

Topic archived. No new replies allowed.