Question

Question 14; Write a C function named isSymmetric, the prototype of which is given below, that...

Question 14;

Write a C function named isSymmetric, the prototype of which is given below, that returns 1 if the elements of an array of integers named myArray of size n are symmetric around the middle. If the array elements are not symmetric, the function should return 0. Both the array and its size are specified as parameters. (10 marks)

Clue: Array myArray of size n is symmetric if myArray[0] is equal to myArray[n-1], myArray[1] is equal to myArray[n-2], and so on. For example, the elements in the following array are symmetric: int list1[] = {1, 4, 5, 4, 1}; But the elements in the following array are not symmetric: int list2[] = {4, 6, 8, 9, 7, 8, 6, 4}; Function prototype: int isSymmetric(int values[], int n);

Write your code inside the box on the next page.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


Answer: here is the answer for your question :

-----------------------------------------------------------------------------------------------------------------------------------------

C CODE:


#include <stdio.h>

int isSymmetric(int values[], int n)
{
   // i used for forward traversal and j for backward traversal
for (int i = 0, j = n - 1; i < j; i++, j--) {
if (values[i] != values[j]) {
  
return 0; // when array is not Symmetric, we return 0
}
}

return 1; // when array is Symmetric, we return 1
  
}

int main()
{
  
int values[11]={1,2,3,4,5,6,5,4,3,2,1};
int n = sizeof (values )/ sizeof (values[0]);

printf(" if array is Symmetric , output will be 1 else 0 :\n");
printf("fucntion returns value as:\n");
printf("%d",isSymmetric(values,n));
return 0;
}

SNIPPET:

main.c 2 #include <stdio.h> 4 int isSymmetric(int values[], int n), 5-{ // i used for forward traversal and j for backward tr

OUTPUT:

if array is Symmetric, output will be 1 else 0 : fucntion returns value as: ... Program finished with exit code o Press ENTER

-----------------------------------------------------------------------------------------------------------------------------------------

I hope this would help you out,If you like my answer , please upvote

If you have any doubt, you can provide comment /feedback below the answer

Thanks

Add a comment
Know the answer?
Add Answer to:
Question 14; Write a C function named isSymmetric, the prototype of which is given below, that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C function named isSymmetric, the prototype of which is given below, that returns 1...

    Write a C function named isSymmetric, the prototype of which is given below, that returns 1 if the elements of an array of integers named myArray of size n are symmetric around the middle. If the array elements are not symmetric, the function should return 0. Both the array and its size are specified as parameters.

  • 1. Write a function named findTarget that takes three parameters: numbers, an array of integers -...

    1. Write a function named findTarget that takes three parameters: numbers, an array of integers - size, an integer representing the size of array target, a number The function returns true if the target is inside array and false otherwise 2. Write a function minValue that takes two parameters: myArray, an array of doubles size, an integer representing the size of array The function returns the smallest value in the array 3. Write a function fillAndFind that takes two parameters:...

  • c++ please no global varaible write a value returning function input data to dynamically allocate a...

    c++ please no global varaible write a value returning function input data to dynamically allocate a short array of size elements and store the input data entered from the disk file into array.Return the base address of the array allocated.Read the data from the text file Hello.txt and store the data in reverse order in the array.the size of the data set will be in the file size 10 and the data was 1 2 3 4 5 6 7...

  • In c++ 1. Write a function named findTarget that takes three parameters - numbers: an array...

    In c++ 1. Write a function named findTarget that takes three parameters - numbers: an array of integers size: an integer representing the size of array target: a number The function returns true if the target is inside the array and false otherwise 2. Write a function min Valve that takes two parameters: myArray an array of doubles - size: an integer representing the size of array The function returns the smallest value in the array 3 Wrile a funcion...

  • In C language Write a program that includes a function search() that finds the index of...

    In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...

  • Homework Question Write a void function called transformArray that takes two parameters - a reference to...

    Homework Question Write a void function called transformArray that takes two parameters - a reference to a pointer to a dynamically allocated array of ints, and the size of that array.  The pointer is passed by referencebecause you want to change the value of the pointer. The function should dynamically allocate an array that is twice as long, filled with the values from the original array followed by each of those values times 2. For example, if the array that was...

  • (C++ program) Can someone help me with this homework? Write a function named selectPositive that given...

    (C++ program) Can someone help me with this homework? Write a function named selectPositive that given a table of n x m integers, copies the elements that are greater than zero ( > 0 ) to a 1D array, column by column. For instance: int table[MAX_ROW][MAX_COL] = { {10, -2, -3, -4, 0, 85}, {-5, -6, 0, 80, -5, 10}, {90, 11, -1, 31, -3, -4}, {41, 51, 6, 71, -7, 19} }; int n = 4, m = 6;  ...

  • Write a function named findTarget that takes three parameters: - numbers, an array of integers -...

    Write a function named findTarget that takes three parameters: - numbers, an array of integers - size, an integer representing the size of array - target, a number The function returns true if the target is inside array and false otherwise

  • in C++ and also each function has its own main function so please do the main...

    in C++ and also each function has its own main function so please do the main function as well. Previously when i asked the question the person only did the function and didn't do the main function so please help me do it. 1-1. Write a function that returns the sum of all elements in an int array. The parameters of the function are the array and the number of elements in the array. The function should return 0 if...

  • Write a function named stats, that takes an array and the number of elements in the...

    Write a function named stats, that takes an array and the number of elements in the array as arguments. It must compute and print the minimum value in the array, maximum value in the array and the average value of all the values inside the array. Your function MUST be named stats Your function has two parameters in the following order: an array of type double The number of elements in the array, type int Your function should NOT return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT