44.3k views
2 votes
Write a squareNreverse function that takes an integer array and its length as arguments. Your function should first square each element of the array and then it should reverse the contents of the array, leaving the final output in the original array, and return nothing. Demonstrate the function in a main program that asks the user to enter 5 integers, stores them in an integer array, calls the squareNreverse function on this array, and then outputs the content of the array

User Jevonne
by
4.8k points

1 Answer

7 votes

Answer:

Following are the code to this question:

#include <iostream>//defining header file

using namespace std;//use package

void squareNreverse(int arr[], int size)//defining a method squareNreverse

{

int i,t,j;//defining integer variable

for (i = 0; i < size; i++)//defining loop for calculate square of array

{

arr[i] = arr[i] * arr[i];//calculate square and store its value

}

i = 0;//assign value 0 in i variable

j = size - 1;// hold size value

while (i < j)//defining while loop for calculate reverse value

{

t = arr[i];// store array value in t variable

arr[i] = arr[j];//defining arr[i] to store in arr[j]

arr[j] = t;//assign a[j] in t

i++;//increment i value

j--;//decrease j value

}

cout << "Displaying the Reveersed Array after Squared :" << endl;//print message

for (i = 0; i < size; i++)//defining for loop

{

cout << arr[i] << "\t";//print array value

}

}

int main()//defining main method

{

int size = 5;//defining size variable that hold value 5

int arr[size];//defining arr as an integer array

cout << "Enter elements :"; //print message

for (int i = 0; i < size; i++)//defining for loop for input value

{

cin >> arr[i];//input value

}

squareNreverse(arr, size);//call method squareNreverse

return 0;

}

Output:

Enter elements :2

3

4

5

6

Displaying the Reveersed Array after Squared :

36 25 16 9 4

Step-by-step explanation:

In the given code a method, "squareNreverse" is defined that accepts an array and a size variable as a parameter, and inside the method, three integer variable "i,j, and t" is defined, in which i and j are used in the loop for calculating the square value and t is used for reverse the array value, and the print method is used for print reverse array value.

In the main method, an array "arr" and size is defined, which use for loop for input value from the user end, and at the last, we call the method "squareNreverse" bypassing array and size value.

User Nickle
by
4.8k points