Boolean function returns 1iff reversing arrays n elements is same as the original array

Bool isSymmetric(int a[],int n) ;
The function returns true if and only if the array obtained by reversing the first n elements is the same as the original array.For example,if a is{22,33,44,55,44,33,22} then te call isSymmetric(a,7) would return true,but the call isSymmetric (a,4) would return false.
Bool isSymmetric(int a[], int n) ;
The function returns true if and only if the array obtained by reversing the first n elements is the same as the original array. For example,if a is{22,33,44,55,44,33,22} then the call isSymmetric(a,7) would return true,but the call isSymmetric (a,4) would return false.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
bool isSymmetric(int a[], int n)
{
	int i;
	int *b = new int[n]; 
	for(i = 0; i < n; i++)
	{
		b[i] = a[n-i-1];
	}

	for(i = 0; i < n; i++)
	{
		if(a[i] != b[i])
		{
			delete [] b;
			return false;
		}
	}

	delete [] b;
	return true;
}


http://cpp.sh/5umi5
Making a new array is VERY inefficient. You have failed your test.
Last edited on
Making a new array is VERY inefficient.

You mean this?
1
2
3
4
5
6
7
8
9
bool isSymmetric(int a[], int n)
{
	int i;
	for(i = 0; i < n; i++)
	{
		if(a[i] != a[n-i-1]) return false;
	}
        return true;
}


http://cpp.sh/9uyjb

Edit : As expected of my teacher. I already know this already.
Last edited on
sorry i have the first part of question which i understood already,but ı dont know second part is true or not this is the missing first part of question :void reverse(int a[],int n);
The function reverses the first n elements of the array.For example,the call reverse(a,5) would transform the array {22,33,44,55,66,77,88,99} into {66,55,44,33,22,77,88,99}.
The function reverses the first n elements of the array. For example,the call reverse(a,5) would transform the array {22,33,44,55,66,77,88,99} into {66,55,44,33,22,77,88,99}.
1
2
3
4
5
6
7
8
9
10
11
void reverseArray(int a[], int n)
{
	int i;
	int temp;
	for(i = 0; i < n / 2; i++)
	{
	    temp = a[i];
	    a[i] = a[n-i-1];
	    a[n-i-1] = temp;
	}
}

http://cpp.sh/2oga
You mean this?

1
2
3
4
5
6
7
8
9
bool isSymmetric(int a[], int n)
{
	int i;
	for(i = 0; i < n; i++)
	{
		if(a[i] != a[n-i-1]) return false;
	}
        return true;
}


http://cpp.sh/9uyjb

Edit : As expected of my teacher. I already know this already.


Nope, still not the most efficient code. Failed again...
Last edited on
Nope, still not the most efficient code. Failed again...


Of course I know the answer. You think I will fail that easily?
1
2
3
4
5
6
7
8
9
bool isSymmetric(int a[], int n)
{
	int i;
	for(i = 0; i < n / 2; i++)
	{
		if(a[i] != a[n-i-1]) return false;
	}
        return true;
}
After 2 fails, the third try isn't worth much buddy, sorry.

But good for you.
Last edited on
Topic archived. No new replies allowed.