In: Computer Science
(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 function should not return anything. (Note that the contents of the array should NOT be modified.)
Write a function called triple that takes an array of integers as a parameter as well as the length of the array. It should triple each value in the array. The function should not return anything. (Note that the contents of the array WILL be modified.)
Write a program (i.e. a main function) that simply calls each of the 3 functions above. For each function, initialize an array with arbitrary values, then call the corresponding function and display the result. For the maximum function, you will initialize an array of double values. Then you will call the maximum function, passing that array of doubles, and print the result. For the printReverse function, you will initialize an array of characters. Then you will call the printReverse function to print the array of characters in reverse order. Finally, you will initialize an array and call the triple function. For the triple function, you will need to print the entire array after the function has been called to show that each element was indeed tripled. (Hint: Your main function should consist of about 8 lines of code.)
I WROTE THE CODE ALONG WITH THE COMMENTS
CODE:
#include <iostream>
#include <cmath>
using namespace std;
int maximum(int arr[],int len)
{
int i;
int max=arr[0];
for(i=1;i<len;i++) //for loop used to traverse each
element.
{
if(max<arr[i]) //condition for max element.
max=arr[i];
}
return max;
}
void printReverse(char arr[],int len)
{
int i;
cout<<"Reverse order of array: ";
for(i=len-1;i>=0;i--) //for loop used to traverse each
character.
cout<<arr[i]<<" ";
}
void triple(int arr[],int len)
{
int i;
for(i=0;i<len;i++) //for loop used to traverse each
element.
{
arr[i]=pow(arr[i],3); //pow function used triple the value.
}
}
int main()
{
int max,i;
int arr1[8]={23,45,67,2,99,69,9,78};
max=maximum(arr1,8); //calling maximum() function.
cout<<"Maximum number is :
"<<max<<endl<<endl;
char arr2[5]={'h','e','l','l','o'};
printReverse(arr2,5); //calling printReverse() function.
cout<<endl<<endl;
int arr3[6]={3,4,6,8,9,12};
triple(arr3,6); //calling triple() function.
cout<<"Triple values of array: ";
for(i=0;i<6;i++) //print array.
cout<<arr3[i]<<" ";
return 0;
}
OUTPUT:
SCREENSHOT OF THE CODE: