0

array symmetric

bool isSymmetric(int a[], int n); void showarray( int a[],int n); int main() { int a[]= {22,33,44,55,44,33,22}; int array_size=7; cout<<"The array is: "<<endl; showarray(a,array_size); int n; cout<<"\nEnter n you want to reverse: "; cin>>n; isSymmetric(a,n); cout<<"\nThe array is after reverse: "<<endl; showarray(a,n); } void showarray( int a[],int n) { for(int i=0; i<n; i++) { cout<<a[i]<<","; } } bool isSymmetric(int a[], int n) { for(int i=0; i<n/2; i++) { if(a[i]!= a[n-1-i]) { return false; } else { return true; } } } Write and test the following function: 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. Warning: The function should leave the array unchanged. Is it correct according to the question?

24th Apr 2021, 6:55 AM
Ramisa Fariha
Ramisa Fariha - avatar
16 odpowiedzi
+ 2
Your code is almost correct, but right now you are deciding the symmetry based on the first and last element of the range only, as either the if or the else statement will be executed. What you want is to break from the test if a non-conforming pair has been found, and only return true if all tests have been correct. I modified your code to reflect this: https://code.sololearn.com/caI8DHrDZjk9/?ref=app Also, when you are supposed to test a function, it would make sense to verify the results somehow, for example by printing them. Calling isSymmetric() on its own doesn't do anything, hence I also changed the output a little.
24th Apr 2021, 10:07 AM
Shadow
Shadow - avatar
+ 1
thank you once again for your help..I am really grateful to you..@Martin Taylor...just asking out of curious,are you a teacher or is your profession related to computer science or something?Don't have to answer this question if you don't want to
24th Apr 2021, 8:59 PM
Ramisa Fariha
Ramisa Fariha - avatar
+ 1
@Martin Taylor cool...I am also studying in the same field i.e. Electrical Enginnering..At first I wanted to choose Computer Science and Engineering..Thanks to the Almighty I didn't coz I've realized that coding is definitely not for me
24th Apr 2021, 9:26 PM
Ramisa Fariha
Ramisa Fariha - avatar
+ 1
Martin Taylor are you have any official website or something like.
25th Apr 2021, 11:25 PM
Mehran
Mehran - avatar
0
thank you so much @Shadow
24th Apr 2021, 12:59 PM
Ramisa Fariha
Ramisa Fariha - avatar
0
Write and test a function that creates Pascal’s Triangle in the square matrix that is passed to it. For example, if the two-dimensional array a and the integer 4 were passed to the function, then it would load the following into a: 10000 11000 12100 13310 14641 void build_pascal(int p[][SIZE], int n); void showmatrix(int p[][SIZE], int n); const int SIZE=100; int main() { int n; cout<<"Enter the number of rows for pascal triangle size: "; cin>>n } void build_pascal(int p[][SIZE], int n) { assert(n>0 && n<SIZE); for(int i=0; i<SIZE; i++) { for(int j=0; j<SIZE; j++) { if(i>n || j>i) { p[i][j] = 0; } else if(j==0 || j==i) { p[i] [j] =1; } else { p[i][j] = p[i-1][j-1]+p[i-1][j]; } } } showmatrix(p,n); } void showmatrix(int p[][SIZE], int n) { for(int i=0; i<SIZE; i++) { for(int j=0; j<SIZE; j++) { cout<<p[i][j]<<"\t"; } cout<<endl; } cout<<endl; } I've done the code up to this and it's not completed .How can I write the matrix and relate the integer to it?
25th Apr 2021, 2:54 PM
Ramisa Fariha
Ramisa Fariha - avatar
0
I can't..solo learn has banned me temporarily from asking questions..I didn't know that asking questions was limited here @Martin Taylor Can you please check your inbox kindly
25th Apr 2021, 5:16 PM
Ramisa Fariha
Ramisa Fariha - avatar
0
OK, Sir no problem. I have tried again but it seems to me that I can't ask any further questions and I don't know for how many days it will continue.
25th Apr 2021, 7:00 PM
Ramisa Fariha
Ramisa Fariha - avatar
0
@Martin Taylor so I was going through the exercise of chapter 7 and it is written that the solution from 7.7 to 7.24 is given on project.euclid.net and I searched as instructed in the book but wasn't able to find them. Do you have any idea about projectEuclid.net
25th Apr 2021, 8:48 PM
Ramisa Fariha
Ramisa Fariha - avatar
0
@Martin Taylor ok thank you.I 'll give it a try tomorrow.
25th Apr 2021, 9:55 PM
Ramisa Fariha
Ramisa Fariha - avatar