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

write a program in C Write a function that is passed an array of characters containing...
write a program in C Write a function that is passed an array of characters containing letter grades of A, B, C, D, and F, and returns the total number of occurrences of each letter grade. Your function should accept both lower and upper case grades, for example, both 'b' and 'B' should be bucketed into your running total for B grades. Any grade that is invalid should be bucketed as a grade of 'I' for Incomplete. You must use...
(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]
(8%) Write a C/C++ program that takes an input (array) from 1 to n (say n...
(8%) Write a C/C++ program that takes an input (array) from 1 to n (say n = 50) and displays the string representations of those numbers with following conditions If the current number is divisible by 2, then print CSU If the current number is divisible by 5, then print SB If the current number is divisible by both 2 and 5, then print CSUSB If the number is neither divisible by 2 nor 5, then print the number Example:...
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...
write a recursive algorithm to find the maximum element in an array of n elements and...
write a recursive algorithm to find the maximum element in an array of n elements and analyze its time efficiency. (I am using c++ programming language)
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}...
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)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT