// pointers converted from array // convert this program from using arrays to us
ID: 3571335 • Letter: #
Question
// pointers converted from array
// convert this program from using arrays to using pointers to arrays
#include <iostream>
#include <iomanip>
using namespace std;
// Function prototypes
double averageNumber(double [], int);
const int SIZE = 4;
int main()
{
double average = 0;
double numbers[SIZE];
// Fill the array with numbers.
for (int i = 0; i < SIZE; i++)
{
// Get a number.
cout << "Enter number "
<< (i + 1) << ": ";
cin >> numbers[i];
}
// Get the average of the test nums.
average = averageNumber(numbers, SIZE);
// Display the resulting data.
cout << " Average score: " << setprecision(2)
<< fixed << average << endl << endl;
return 0;
}
double averageNumber(double nums[], int SIZE)
{
double total = 0;
// Calculate the total of the nums.
for (int k = 0; k < SIZE; k++)
total += nums[k];
// Return the average score.
return (total / SIZE);
}
Explanation / Answer
Here is the pointer version for you:
// pointers converted from array
// convert this program from using arrays to using pointers to arrays
#include <iostream>
#include <iomanip>
using namespace std;
// Function prototypes
double averageNumber(double *, int);
const int SIZE = 4;
int main()
{
double average = 0;
double numbers[SIZE];
// Fill the array with numbers.
for (int i = 0; i < SIZE; i++)
{
// Get a number.
cout << "Enter number "
<< (i + 1) << ": ";
cin >> *(numbers + i);
}
// Get the average of the test nums.
average = averageNumber(numbers, SIZE);
// Display the resulting data.
cout << " Average score: " << setprecision(2)
<< fixed << average << endl << endl;
return 0;
}
double averageNumber(double *nums, int SIZE)
{
double total = 0;
// Calculate the total of the nums.
for (int k = 0; k < SIZE; k++)
total += *(nums + k);
// Return the average score.
return (total / SIZE);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.