2

I'm working on an example problem and its asking me to check of a user inputted array is symmetric. I've figured out how to do it by creating another array, copying over the first array in reverse order then checking to see if they are equal to each other. As seen in the following code.

#include <stdio.h>

int main(void){

#define NUM_ELEMENTS 12
int userArray[NUM_ELEMENTS];
int userArray2[NUM_ELEMENTS];
int i;
int tempVal = 0;
double sumArray = 0;
double aveArray = 0;


printf("Enter 12 interger numbers (each one separated by a space):\n");
for(i = 0; i < NUM_ELEMENTS; i++){
    scanf_s("%d", &userArray[i]);
}

for(i = 0; i < NUM_ELEMENTS; i++){
    sumArray = sumArray + userArray[i];
}

aveArray = sumArray/NUM_ELEMENTS;

printf("\nAverage of all data points is %.2lf \n",aveArray);
printf("\nAn array in reverse order:\n");

for(i = NUM_ELEMENTS - 1; i >= 0; i--){
    printf("%d ",userArray[i]);
}
printf("\n");

//Used swap values in the array
for(i = 0; i < (NUM_ELEMENTS / 2); i++){
    tempVal = userArray[i];
    userArray2[i] = userArray[NUM_ELEMENTS - 1- i];
    userArray2[NUM_ELEMENTS - 1 - i] = tempVal;
}
if(userArray[i] == userArray2[i])
    printf("\nThis array is symmetric\n");
else
    printf("\nThis array is NOT symmetric\n");

    return 0;
}

So if a user entered 1 2 3 4 5 6 6 5 4 3 2 1 the program return back that the array is symmetric.

I'm just curious if there is a simpler way to do this?

Christopher
  • 137
  • 2
  • 2
  • 6
  • You have the right idea with `NUM_ELEMENTS / 2`. Think about what you could use that for *without* having to copy the array. (The first and the last value must be the same. The second and second to last value must be the same, etc.) – Jon Egeland Mar 08 '15 at 06:12
  • I guess you mean *palindromic*. `HIC` would be symmetric (x-axis) – M.M Mar 08 '15 at 06:32

3 Answers3

3

Just iterate forwards and backwards at the same time:

// i iterates forwards from the start
// j iterates backwards from the end
// once they pass each other, we're done.
for (int i = 0, j = NUM_ELEMENTS - 1; i < j; i++, j--) {
    if (userArray[i] != userArray[j]) {
        printf("\nThis array is not symmetric\n");
        return 0;  // No point in running this function any longer at this point.
    }
}

// If the function didn't return in the for loop, the array is symmetrical.
printf("\nThis array is symmetric\n");
ArtOfWarfare
  • 20,617
  • 19
  • 137
  • 193
0

You could run over the array from both sides and compare the elements:

int i;
int flag = 1;
for (i = 0; flag && i <= (NUM_ELEMENTS - 1) / 2); ++i) {
    if (userArray[i] != userArray[NUM_ELEMENTS - i]) {
        printf ("Array is not symmetric");
        flag = 0;
    }
}
if (flag) {
    printf ("Array is symmetric");
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

Personally, in C, I'd just do it with pointers.

int is_symmetric(const int *s, int num_elements)
{
     const int *begin = s, *end = s + num_elements - 1;
     while (begin < end)
     {
         if (*begin != *end) return 0;
         ++begin; --end;
     }
     return 1;
}
Rob
  • 1,966
  • 9
  • 13