Question

In: Computer Science

c++ please 1. Write and test the function maximum that is passed an array of n...

c++ please

1. Write and test the function maximum that is passed an array of n pointers to integers and returns the maximum value among the n integers. The function must use the travellingpointer(1stversion) notation to traverse the array. The function has the following prototype.

int maximum ( int *p [ ], int n);

2. Implement the function psum( )that is passed an array of n floats and returns a pointer to the sum of such an array. Print the address of the returned sum and its value in the main program.

float *psum (float *p, int n);  // function prototype

void main ( ){

            float pfs[3] = {5.5,  10.5, 20.3};

            for (int j = 0; j <  3; j++)

                        cout << pfs[j] << endl;

            float* rp = psum (pfs, 3);

            cout << “The address of the sum: “ << rp << endl;

            cout << “The value of the sum: “<< (*rp) << endl;

}

float* psum (float* p , int n){  // A function that returns a pointer

}

Solutions

Expert Solution

1.

#include<iostream> // C++ Standard Header file

int maximum ( int *p[ ], int n); //Function Prototype

using namespace std;

int main()
{
   int arr[]={1,2,3,4,5};
  
   int *p = arr; //Pointer to array of n integers.
  
   int max,n=5;
  
   cout<<"Elements in the array: ";
   for(int i=0;i<n;i++)
       cout<<p[i]<<"\t";
  
   cout<<endl;  
  
   max=maximum(&p,n); //Function call, remember, int *p[] is converted into int **p at compile-time
     
   cout<<"\n\nmaximum value of integers is: "<<max;

   return 0;
  
}


int maximum ( int *p[ ], int n) //Function Definition
{

   int max = *p[0]; //Initialising max with first array element
  
   for(int i=0;i<n;i++)
   {

       if(max < *p[i]) //Condition to find the maximum element
           max = *p[i];
   }
  
   return max;
  
}

2.

#include<iostream>

float *psum (float *p, int n); // function prototype

using namespace std;

int main ( )
{

float pfs[3] = {5.5, 10.5, 20.3};   //An array of float values

for (int j = 0; j < 3; j++)   //Displaying the elements in the array
       cout << pfs[j] << endl;

float* rp = psum (pfs, 3);   //Function call

cout << "The address of the sum: " << rp << endl;

cout << "The value of the sum: "<< (*rp) << endl;
  
return 0;

}

float* psum (float* p , int n){ // Function Definition

   static float sum=0;       //A static variable to store sum, as sum will be used further in the program
     
   for(int i=0;i<n;i++)   //Loop to calculate the sum of elements in the array
       sum += p[i];

   return &sum;   //Returning the address of sum
}

Sample Run:


Related Solutions

(C++ only please) Write a function called maximum that takes an array of double values as...
(C++ only please) Write a function called maximum that takes an array of double values as a parameter and returns the largest value in the array. The length of the array will also be passed as a parameter. (Note that the contents of the array should NOT be modified.) Write a function called printReverse that takes an array of characters and the length of the array as parameters. It should print the elements of the array in reverse order. The...
Write a C function that finds and displays the maximum value ina two-dimensional array of...
Write a C function that finds and displays the maximum value in a two-dimensional array of integers. The array should be declared as a 10-row-by-20-column array of integers in main (), and the starting the address of the array should be passed to the function. Modify the function so that it also displays the rows and columns number of the element with the maximum value
Write a code using c# Maximum Sub Array.
Write a code using c# Maximum Sub Array.
Write a C++ program that has a function which given n>=0, create an array length n*n...
Write a C++ program that has a function which given n>=0, create an array length n*n with the following pattern, shown here for n=3 : {0, 0, 1, 0, 2, 1, 3, 2, 1} (spaces added to show the 3 groups) generateGroups(3) → [0, 0, 1, 0, 2, 1, 3, 2, 1] generateGroups(2) → [0, 1, 2, 1] generateGroups(4) → [0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1]
1- Write it with C++ program §Write a function Rotate that rotates an array of size...
1- Write it with C++ program §Write a function Rotate that rotates an array of size n by d elements to the left §Use array as argument §In the main function, call the function Rotate and show the rotated array §Test your code For example: Input: [1 2 3 4 5 6 7], n = 7, d = 2 Output: [3 4 5 6 7 1 2] 2- Write it in C++ §Search Insert Position •Given a sorted array in...
3. Array Operations 3-1 Write a function, equalsArray that when passed two int arrays of the...
3. Array Operations 3-1 Write a function, equalsArray that when passed two int arrays of the same length that is greater than 0 will return true if every number in the first array is equal to the number at the same index in the second array. If the length of the arrays is less than 1 the function must return false. For example, comparing the two arrays, {1,2,3,4,5} and {1,2,3,4,5} would return true but the two arrays {3,7} and {3,6}...
Using Python C++ Purpose: Write and test a non-trivial 2-d array function. Write a user-defined function...
Using Python C++ Purpose: Write and test a non-trivial 2-d array function. Write a user-defined function that, given an arbitrary 2-d array as input, returns its perimeter sum, which is defined as the sum of all the elements along its perimeter. You must name your function perimeter_sum
Write and test a C implementation of the Mobius function M(n) defined as follows M(n) =...
Write and test a C implementation of the Mobius function M(n) defined as follows M(n) = 1 if n = 1           = 0 if any prime factor is contained in N more than once = (‐1)p if n is the product of p different prime factors Examples M(78) = ‐1     78 = 2 * 3 * 13                   M(34) = 1      34 = 2 * 17                M(45) = 0       45 = 3 * 3 * 5 The first values of M(n)...
In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
Write a function, hexDigits that when passed an int array of any length greater than 0...
Write a function, hexDigits that when passed an int array of any length greater than 0 will print the corresponding hex digit for each int, one per line. The corresponding hex digit should be determined using a switch statement. The hex digits must be printed in uppercase and in order starting with the first entry. Each line of output should start with the array index of the number being written out, followed by a space, then the number, then another...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT