Question

please use c ++ language and write the comment too. thanks Pointer Arithmetic Write a function...

please use c ++ language and write the comment too. thanks

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
using  namespace std;

void pointerArithmetic(int *array, int size, double &average, int &high, int &low) {
    int sum = 0; //a temporary variable in which sum of all element of array will we kept
                    //and after that average will be calculated as sum / size;
    //PROGRAM LOGIC
    // we will iterate whole array and find the total sum
    //by doing so we will also try to find what is highest value and what is lowest value alongside

    // How we will find highest value ?
    //      - we will compare each element of array with highest
    //      -if any array element is higher then current high, then that element is our new highest
    //      but what will be our initial value of high ( because c++ compiler initialise every variable
    //      with some random data)
    //      if random data is higher then all element of array then we will never find the highest
    //example - {10,20,30,40} is our array and let initial value of high = 132656565 ( some random value)
    //then our program code will go to first array element 10 and comapre it with current high 132656565
    //and found that element is lower
    //similarly for 2nd
    //and third
    //and fourth
    //and last it will return 132656565 as high

    // similarly we can find low
    //So my suggestion is to always initialise a variable with some initially value before using them
    //also u can do the same problem with other many way


    //let's go as i mentioned above


    // in this question we are not allowed to use [] operator
    // this can be achieved by pointer arithematic as
    // a[10] is same as *(a + 10)
    // a[0] is same as *(a)  or *(a + 0)

    for(int i=0;i<size;++i){
        sum += *(array + i);    //finding sum

        if(high < *(array + i)){    //finding high
            high = *(array + i);
        }

        if(low > *(array + i)){ //finding low
            low = *(array + i);
        }
    }

    average = sum * (1.0) / size;

    //NOTE sum * 1.0 will convert the int sum to a double type
}

int main() {
    int arr[10] = {10,20,30,40,50,6,0,145,15};
    double average = 0;
    int high = -1212122;
    int low  = 4152121;
    //NOTE : above are initialised with that date which should not present as element of array

    //calling function
    pointerArithmetic(arr,10,average,high,low);

    cout<<"high    = "<<high<<endl;
    cout<<"low     = "<<low<<endl;
    cout<<"average = "<<average<<endl;
    return 0;
}

//Sample output

Run CPROJECT C:\Users\VIREN CLionProjects\CPROJECT\cmake-build-debug CPROJECT.exe high 145 average = 31.6 Process finished wi

Add a comment
Know the answer?
Add Answer to:
please use c ++ language and write the comment too. thanks Pointer Arithmetic Write a function...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi!, having trouble with this one, In this class we use visual studio, C++ language --------------------------------------------------------------...

    Hi!, having trouble with this one, In this class we use visual studio, C++ language -------------------------------------------------------------- Exercise #10 Pointers - Complete the missing 5 portions of part1 (2 additions) and part2 (3 additions) Part 1 - Using Pointers int largeArray (const int [], int); int largePointer(const int * , int); void fillArray (int * , int howMany); void printArray (const char *,ostream &, const int *, int howMany); const int low = 50; const int high = 90; void main()...

  • average Function Write a function that finds an average of the array using only pointers (and...

    average Function Write a function that finds an average of the array using only pointers (and pointer arithmetic) to move through the array. Test the function in a driver program. Function header: double average(int* array, int size)

  • Rewrite the program you submitted for A8 to use pointers, pointer notation and arithmetic, etc. as...

    Rewrite the program you submitted for A8 to use pointers, pointer notation and arithmetic, etc. as follows: If you did not complete A8, you will need to start with those instructions, and then, change your code based on the instructions here The main() function should: Create a fixed or dynamic array, e.g., double grade[SIZE] or double *grade = malloc(...) or calloc(...) Concerning the function call for getData(), pass the address of the array and its size to pointers; depending on...

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • in c++ please Assignment Instructions: MAIN FUNCTION: Ask the user how many students were surveyed.   Call...

    in c++ please Assignment Instructions: MAIN FUNCTION: Ask the user how many students were surveyed.   Call a function called makeArray to define an array of integers with the number of elements equal to the number of students surveyed. Call a function called getStudentData to allow the user to enter the number of hours each student spent watching Netflix into the array. Call a function called getAverage to calculate and display the average of the hours entered. Call a function called...

  • solve it in c+* Part II: Dynamic Arrays and Pointer Arithmetic Q5: Implement a subset function...

    solve it in c+* Part II: Dynamic Arrays and Pointer Arithmetic Q5: Implement a subset function for a dynamic array which returns a new dynamic array that is a subset of the original. (15pt) input parameters: array - (the array and any related parameters) start - index of the first element end - index of the last element This function returns a new dynamic array containing the elements from the start thru the end indices of the original array. All...

  • Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory...

    Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int * ) named ptr_1. Use ptr_1 to assign the number 7 to that dynamically allocated integer, and in another line use printf to output the contents of that dynamically allocated integer variable. Write the code to dynamically allocate an integer array of length 5 using calloc or malloc and have it pointed...

  • Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics and...

    Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics and dereferencing. b) Write a function “void computeMinMax(double arr[], int length, double * min, double * max)” that finds the minimum and the maximum values in an array and stores the result in min and max. You are only allowed to use a single for loop in the function (no while loops). Find both the min and the max using the same for loop. Remember...

  • Write a c program that finds the uncommon elements from two array elements using pointers only...

    Write a c program that finds the uncommon elements from two array elements using pointers only . For example : Enter the length of the array one : 5 Enter the elements of the array one: 9 8 5 6 3 Enter the length of the array two: 4 Enter the elements of the array two: 6 9 2 1 output: 8 5 3 2 1 void uncommon_ele(int *a, int n1, int *b, int n2, int *c, int*size); The function...

  • IN C PROGRAMMING, NOT C++ Rewrite the program you submitted for A8 to use pointers, pointer...

    IN C PROGRAMMING, NOT C++ Rewrite the program you submitted for A8 to use pointers, pointer notation and arithmetic, etc. as follows: If you did not complete A8, you will need to start with those instructions, and then, change your code based on the instructions here The main() function should: Create a fixed or dynamic array, e.g., double grade[SIZE] or double *grade = malloc(...) or calloc(...) Concerning the function call for getData(), pass the address of the array and its...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT