Question

In: Computer Science

C++ (cpp) PLEASE Write a complete function (prototype and definition) that takes an integer array and...

C++ (cpp) PLEASE

Write a complete function (prototype and definition) that takes an integer array and the array dimensions as an input. The function should then square each value of the array in-place.

At the completion of the function return a Boolean which indicates the process has completed.

Solutions

Expert Solution

CODE:


#include <iostream>

using namespace std;

//function definition
bool function(int a[],int n){
  
//if no elements in the array
if(n==0){
  
//returning false as no updated done
return false;
}
  
//iterating each elements of the array
for(int i=0;i<n;i++){
//updating each element by its square
a[i] = a[i]*a[i];
}
  
//returning true after updating array
return true;
}

int main()
{
//to store the dimension of array
int n;
  
cout<<"Enter size of array : ";
  
//input of array dimension
cin>>n;
  
//array initialization
int a[10000];
  
cout<<"Enter array elements: ";
  
//input of array elements
for(int i=0;i<n;i++){
cin>>a[i];
}
  
// calling function, if it returns true, then code inside if will execute
if(function(a,n)){
cout<<"Updated array elements are: ";
  
//printing updated array elements
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
}
// if no update is performed
else{
cout<<" Operation not performed ";
}
return 0;
}

OUTPUT:

Enter size of array : 5 Enter array elements: 1 2 3 4 5 Updated array elements are: 1 4 9 16 25 . Program finished with exit code 0 Press ENTER to exit console.


Related Solutions

Given the following C function prototype which accepts an integer argument, complete the implementation of the...
Given the following C function prototype which accepts an integer argument, complete the implementation of the function in C language to return the number of 1’s in the binary representation of the number passed. For example if the number is (011100011 ) then the function should return 5 as the number of 1s. Please remember that an integer is 32 bits long. USE the following function int countOnes(int number) Write a full c program that has the header required and...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes one doyble argument called "num". The function will declare, ask, and get another double from the user. Compare the double entered by the user to "num" and return a 0 if they are the same, a -1 num is less than the double entered by the user and 1 if it is greater.
(C++ only please) Write a function called maximum that takes an array of double values as...
(C++ only please) Write a function called maximum that takes an array of double values as a parameter and returns the largest value in the array. The length of the array will also be passed as a parameter. (Note that the contents of the array should NOT be modified.) Write a function called printReverse that takes an array of characters and the length of the array as parameters. It should print the elements of the array in reverse order. The...
Write a function named hasNValues which takes an array and an integer n as arguments. It...
Write a function named hasNValues which takes an array and an integer n as arguments. It returns true if all the elements of the array are one of n different values. If you are writing in Java or C#, the function signature is int hasNValues(int[ ] a, int n) If you are writing in C or C++, the function signature is int hasNValues(int a[ ], int n, int len) where len is the length of a Note that an array...
(C++) Write a function that takes as an input parameter an integer that will represent the...
(C++) Write a function that takes as an input parameter an integer that will represent the length of the array and a second integer that represents the range of numbers the random number should generate (in other words, if the number is 50, the random number generator should generate numbers between 0 and 49 including 49. The function then sorts the list by traversing the list, locating the smallest number, and printing out that smallest number, then replacing that number...
Write a function in C# that takes an array of double as the parameter, and return...
Write a function in C# that takes an array of double as the parameter, and return the average
C++ 1. Write a function decimalToBinary() that takes in a positive integer as a parameter and...
C++ 1. Write a function decimalToBinary() that takes in a positive integer as a parameter and use as stack to convert the integer to a its corresponding binary representation. Hint: divide the integer by 2. 2. A palindrome is a string of characters (a word, phrase, or sentence) that is the same regardless of whether you read it forward or backward—assuming that you ignore spaces, punctuation, and case. For example, Race car is a palindrome. So is A man, a...
Complete the given C++ program (prob1.cpp) to read an array of integers, print the array, and...
Complete the given C++ program (prob1.cpp) to read an array of integers, print the array, and then find the index of the largest element in the array. You are to write two functions, printArray() and getIndexLargest(), which are called from the main function. printArray() outputs integers to std::cout with a space in between numbers and a newline at the end. getIndexLargest () returns the index of the largest element in the array. Recall that indexes start at 0. If there...
Write a c program Write a function to swap two elements of an integer array. Call...
Write a c program Write a function to swap two elements of an integer array. Call the function to swap the first element, i[0] with last element i[n], second element i[1] with the last but one element i[n-1] and so on. Should handle arrays with even and odd number of elements. Call the swap function with the following arrays and print results in each case before and after swapping. i. int arr1[] = {0, 1, 2, 3, 30, 20, 10,...
C++ Write a function called linearSearch that takes an array as a parameter and search for...
C++ Write a function called linearSearch that takes an array as a parameter and search for a specific value inside this parameter. The function returns the frequency of a specific value in the array. In the main function: 1. Define an array called salaries of length 5. 2. Initialize the array by asking the user to input the values of its elements. 3. Define a variable called key and ask the user to enter a value for this variable. 4....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT