Question

In: Computer Science

1) Write a function that receives a pointer to an array along with the size of...

1) Write a function that receives a pointer to an array along with the size of the array, it returns the largest number in the array.

2) Write a function that receives a string and returns the length of it.

Solutions

Expert Solution

1. Function to find the largest number in an array

int largest(int arr[],int size)

//arr is the pointer to the first element of the array & size is the number of elements in the array

{

//max initially stores the minimum value possible. If there are negative elements also in the array, then please //initialize max=INT_MIN and include the header file which contains INT_MIN

int max=-1;

for(int i=0;i<size;i++)

{

// if an element in the array is greater than max, that element is copied to max

if(arr[i]>=max)

max=arr[i];

}

return max; //max stores the largest element in the array and it is returned

}

2. Function to receive a string and return its length

int string_length_func(string str)

{

int count=0,i=0;

while(str[i]!='\0'){ // '\0' is null character which is used to terminate all strings

count++;

i++;

}

// the while loop will run and keep increasing count value till the null character,which is the last character, is not //encountered in the string. As soon as null character is encountered , control exits the loop and returns the //value of count

return count;

}

Please note that these are just user defined functions. You need to write a main function and call these user defined functions in order to execute the program and get the output.

Since no specific programming language was specified in the question, I have written the code using very simple syntax which should work in almost all programming languages.

Here is the complete code in C++.

#include <iostream>
using namespace std;
int largest(int arr[],int size)
{
    int max=-1;
    for(int i=0;i<size;i++)
     {
           if(arr[i]>=max)
                max=arr[i];
      }
      return max;  
}

int string_length_func(string str)
{
    int count=0,i=0;
    while(str[i]!='\0'){
         count++;
         i++;
      }
      return count;
}

int main()
{
    int n;
    cout<<"Enter no of elements in array"<<endl;
    cin>>n;
    cout<<"Enter the elements of array"<<endl;
    int arr[n];
    for(int i=0;i<n;i++)
    cin>>arr[i];
    cout<<largest(arr,n)<<endl;
    string str;
    cout<<"Enter  a string"<<endl;
    cin>>str;
    cout<<string_length_func(str)<<endl;
    return 0;
}

Related Solutions

Create a function output() in C that takes the pointer to the array and its size...
Create a function output() in C that takes the pointer to the array and its size and prints the arrays' contests. (function prototype : void output(int *arrayPtr, int size);
1) Write a function searchValue that accepts an array of integers, the size of the array,...
1) Write a function searchValue that accepts an array of integers, the size of the array, and an integer. Find the last occurrence of the integer passed in as an input argument in the array. Return the index of the last occurrence of the value. If the value is not found, return a -1 2) Write the line of code to call the previous function assuming you have an array vec with length n, and are looking for the number...
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 Python function that accepts three arguments: an array, the size of the array, and...
Write a Python function that accepts three arguments: an array, the size of the array, and a number n. Assume that array contains integers. The function should display all integers in the array that are greater than the number n. Test your function.
In C++ Write a function that took in SIZE and array [size] and counted the number...
In C++ Write a function that took in SIZE and array [size] and counted the number of elements >100
Reimpelment a function called bubble_sort that has the following prototype. bubble_sort(int *array, int size, pointer to...
Reimpelment a function called bubble_sort that has the following prototype. bubble_sort(int *array, int size, pointer to a function) Pre condition array - a pointer to a an array of size element. pointer to function - a pointer to a function that compares two values (depending on sorting in ascending order or descending order) Post condition Sort the array in ascending or descending based on the the pointer to a function. Call the function bubble_sort to sort the array in ascending...
Javascript 1. Write a function which receives four arguments (array, start, end, delimiter). Copy the array...
Javascript 1. Write a function which receives four arguments (array, start, end, delimiter). Copy the array values from the start to end index separated by delimiter\'s value. function([1,2,3,4],1,3,'*') // returns 2*3*4 as string 2. Write a function to change the case of all the characters in string based on their position which matches the value of the pos parameter passed to function(str, pos [even|odd]). Example: function(‘abCd’, ‘odd’) // returns Abcd 3 Write a function which receives two arguments (array, oddOrEven)...
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...
Suppose a function receives a pointer as an argument. Explain how this function is declared within...
Suppose a function receives a pointer as an argument. Explain how this function is declared within its calling function. In particular, explain how the data type of the pointer argument is represented. C++
Write a function that accepts an int array and the array's size as arguments.
Write a function that accepts an int array and the array's size as arguments. The function should create a copy of the array, except that the element values should be reversed int the copy. The function should return a pointer to the new array. Demonstrate the function in a complete program.  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT