Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Pointers, Array and Functions C only!!! ***C++, or any other variation of C, is

ID: 3537254 • Letter: P

Question

Pointers, Array and Functions


C only!!! ***C++, or any other variation of C, is not allowed!!!


This assignment focuses on arrays, the relationship of arrays with pointers and functions.

In this lab you will create a single source code file called arraysPointerFubctions.c that uses the main routine shown below:

1. You will provide the functionality for the functions in bold: revArray(a); *revElement(a); evenOdd(a).

        #include <stdio.h>
        #include <stdlib.h>
        #define N 9

        /* MAIN */
        int main() {

        /* 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(" Reversed: ");
           p = a;
while(p < a + N)
printf("%2d ",*p++);
           printf(" ");

        /* Part B: Return elements in reverse order */
           printf("Original: ");
           for (idx = 0; idx < N; idx++) {
                  printf("%2d ",
*revElement(a));
           }
           printf(" ");
          
        /* 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(" ");
          return 0;
        }

OUTPUT:

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

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

#define N 9

/* reverse function */

void revArray(int *p)

{

int i=0;

int tmp;

for(i=0;i<N/2;i++)

{ tmp=p[i];

p[i]=p[N-1-i];

p[N-1-i]=tmp;

}

}

  

/* even Odd function*/

void evenOdd(int *p)

{

int i=0;

int j=0;

int tmp;

while(j!=N)

{

if(p[j]%2 == 0)

{

tmp=p[j];

p[j]=p[i];

p[i]=tmp;

i++;

}

j++;

}

}


/* MAIN */

int main() {


/* 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(" Reversed: ");

p = a;

while(p < a + N)

printf("%2d ",*p++);

printf(" ");


/* Part B: Return elements in reverse order */

printf("Original: ");

for (idx = 0; idx < N; idx++) {

printf("%2d ",a[N-idx-1]);

}

printf(" ");

  

/* 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(" ");

return 0;

}