Write a program with a main method that asks the user to enter an array of 10 in
ID: 3767420 • Letter: W
Question
Write a program with a main method that asks the user to enter an array of 10 integers. Your main method then calls each of the three methods described below and prints out the results of the two methods which return values.
printReverse - a void method that prints out the array in reverse of the way it was entered all on one line separated by commas (see sample output below).
getLargest - an int method that returns the largest value in the array.
computeTwice- an method that returns an array of int which contains two times of all the numbers in the array (see the sample output below).
Sample output:
Enter a number:
22
Enter a number:
34
Enter a number:
21
Enter a number:
35
Enter a number:
12
Enter a number:
4
Enter a number:
2
Enter a number:
3
Enter a number:
99
Enter a number:
81
Here are your numbers in reverse:81, 99, 3, 2, 4, 12, 35, 21, 34, 22
The highest number is 99
The array with two times the numbers: [44, 68, 42, 70, 24, 8, 4, 6, 198, 162]
Explanation / Answer
#include <iostream>
using namespace std;
void printReverse(int *arr,int size)
{
cout << "Here are your numbers in reverse:";
for(int i=size-1;i>=0;i--)
cout << arr[i] << " ";
cout << endl;
}
int getLargest(int *arr,int size)
{
int max = 0;
for(int i=0;i<size;i++)
{
if(i==0)
max = arr[i];
else
{
if(arr[i]>=max)
max = arr[i];
}
}
return max;
}
int* computeTwice(int *arr,int size)
{
int *ar = new int[size];
for(int i=0;i<size;i++)
ar[i] = 2*arr[i];
return ar;
}
int main()
{
int size = 10;
int arr[size];
int *arr_twice;
int max;
for(int i=0;i<size;i++)
{
cout<<"Enter a number " << " : " << endl;
cin >> arr[i];
}
printReverse(arr, size);
max = getLargest(arr, size);
arr_twice = computeTwice(arr, size);
cout << "The highest number is " << max << endl;
cout << "The array with two times the numbers: [";
for(int i=size-1;i>=0;i--)
cout << arr_twice[i] << " ";
cout << "]";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.