Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Programming C++ programming C++ Programming Pointer Arithmetic Write a funct

ID: 3588881 • Letter: C

Question

C++ Programming

C++ programming

C++ Programming

Pointer Arithmetic Write a function that passes an array address to a function as a pointer. Using pointer arithmetic, the function determines the average, high, and low value of the array. Your function should have this header: void pointerArithmetic(int "array, int size, double &average;, int &high;, int &low;) Parameter size is the number of elements in array. You may not use the subscript notation inside your function-instead, use pointer arithmetic. Write a main program to demonstrate this function. Challenge If you finish early, modify your function and main program to work with an array of objects Create a class Student with a member variable score. Your function should now have a parameter which points to an array of student objects. Within your function, determine average, high, and low scores over all students, using pointer arithmetic

Explanation / Answer

#include <iostream>
using namespace std;

// function that calculates average
void pointerArithmetic(int *array, int size, double &average, int &high, int &low) {
// initializing min and max with first elemnt in array and average with 0
high = *array;
low = *array;
average = 0;

// looping for size times to get each element in array
for (int i = 0; i < size; ++i) {

// for maximum
if (high < *(array+i))
high = *(array+i);
// for minimum
if(low > *(array+i))
low = *(array+i);
// for average
average += *(array+i);
}

// so far, we have sum only. So dividing it by number of elements in array to get average
average = average/size;
}

int main() {
// sample run
int size = 7, h, l;
double ave;
int a[7] = {4,5,6,3,7,2,4};
pointerArithmetic(a, size, ave, h, l);

cout << "High is " <<h << endl;
cout << "Low is " << l << endl;
cout << "Average is " << ave << endl;
}

/* SAMPLE OUTPU
High is 7
Low is 2
Average is 4.42857
*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote