Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Write a function that computes the average value of an array of floating-point data:
- //double average(double* a, int size)
- //In the function, use a pointer variable, not an integer index, to traverse the array
- //elements.
- #include <iostream>
- using namespace std;
- double average(double* a, int size)
- {
- double total = 0;
- double* p = a;
- // p starts at the beginning of the array
- for (int i = 0; i < size; i++)
- {
- total = total + *p;
- // Add the value to which p points
- p++;
- // Advance p to the next array element
- }
- return total / size;
- }
- int main()
- {
- double array[5] = {1,2,3,4,5};
- std::cout << average(array, 5);
- }
- double average(double* a, int size) {
- double total = 0;
- // *(a + i) starts at the beginning of the array
- for (int i = 0; i < size; i++)
- {
- total = total + *(a + i);
- }
- return total / size;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement