Question

In: Computer Science

Array & Function - Can I pass a single parameter to a function that takes two...

Array & Function - Can I pass a single parameter to a function that takes two arguments:

data passed from:

arrayOne(4);

to function:

string function (array[], int)

Solutions

Expert Solution

***************Answer with Explnation***************

Yes, you can pass 1 arguement to a function taking two parameters by using default arguement in function parameter...

But there some rules to be followed when using default arguement

1. Once default value is used for an argument in function definition, all subsequent arguments(the arguements defined after the default arguement) to it must have default value.....or the default arguement parameter should be the last parameter in function definition

2.Default arguement value is overwritten when value for that parameter is passed..

3. When calling the function..arguements from calling function to called function are copied from left to right.For example, if function call is total(5,10,15) and function definition is int total(int a,int b,int c,int d=0) then it will assign 5,10,15 to a,b,c respectively and d will be =0 bcoz of default arguement

****************Summary***************

But as given in question you are passing arrayOne(4)..i.e 4 is an integer value ..that means you want to pass the value for the second parameter in function (array[],int) and define array[] as the default parameter...but we cannot do that (1st rule )because it is not the last parameter in function defintion...so you'll have to change sequence of parameter

Assuming that you don't want to give the pass the arguement for array[] parameter...I have given a program below in c++ to explain what exactly you'll have to do...

I hope it works for you :) !!!!

Ask for help in comments if needed

**************Program**************

#include<iostream>  

using namespace std;  

char arr[]={'H','i','t','h','e','r','e'};  //intializing char array..
//can't intialize default values of array directly in braces..
//you can give int,float,string default values in braces

string arrayOne(int n,char array[]=arr)    
//n is normal paremeter and array[] is default arguement
{  
  char str[n];    //array with size n
  for(int i=0;i<n;i++)    //for loop to store store only part of array to str
  {
    str[i]=array[i];
  }    
  return str;    //return str 
}  


int main()  
{  
  int number;    
  char array1[10];    
  
    cout<<"Enter the string you want to trim"<<endl;
    cin>>array1;    //taking array from user
    
    cout<<"How much of the string do you wanna keep"<<endl;
    cin>>number;  //taking value for n in function call
    
    //calling function arrayOne() and printing string
    //here the array is overwrittem by user array becoz we have passed two arguements
    cout<<"The trimmed string is "<<arrayOne(number,array1)<<endl<<endl;
    
    cout<<"How much of the default string do you wanna keep"<<endl;
    cin>>number;    //taking value for n in function
    
    //calling function arrayOne() and printing trimmed default string
    //here we have not passed 2nd arguement so it will take the default value of array specified in function defintion
    cout<<"The trimmed string is "<<arrayOne(number);
   
    return 0;  
}

**************Output****************

Program Screenshot


Related Solutions

Write a function in C# that takes an array of double as the parameter, and return...
Write a function in C# that takes an array of double as the parameter, and return the average
C++ Write a function called linearSearch that takes an array as a parameter and search for...
C++ Write a function called linearSearch that takes an array as a parameter and search for a specific value inside this parameter. The function returns the frequency of a specific value in the array. In the main function: 1. Define an array called salaries of length 5. 2. Initialize the array by asking the user to input the values of its elements. 3. Define a variable called key and ask the user to enter a value for this variable. 4....
.. Write a method called findNums that takes a two-dimension array of integers as a parameter...
.. Write a method called findNums that takes a two-dimension array of integers as a parameter and returns the number of times a two-digit number appears in the array. For example, if the array (as created by the program below) is 10 45 3 8 2 42 3 21 44 The value returned would be 5 (there are 5 two-digit numbers in the array) public class Question2 {    public static void main(String args[]){      int arr[][] = {{10, 45,...
Can someone do this python program? Write a function that takes a number as a parameter...
Can someone do this python program? Write a function that takes a number as a parameter and then prints out all of the factors for that number.
Write a function called swapElements to swap two elements in a character array. This function takes...
Write a function called swapElements to swap two elements in a character array. This function takes two parameters, a character array and input file. The function should read two numbers from a file and it swap the elements stored in the indices read. please code in c++
C++ A void function named NextLeapYear() that takes an int reference parameter. If the parameter is...
C++ A void function named NextLeapYear() that takes an int reference parameter. If the parameter is positive, the function will assign it the next leap year after it; otherwise, the function will assign 4 to it.
Write a method that takes an integer array as its parameter and sorts the contents of...
Write a method that takes an integer array as its parameter and sorts the contents of the array in ascending order using the Insertion Sort algorithm. Call this method after the original array and other stats have been displayed. Once the array has been sorted by your method, display its contents to the screen in the same manner as the original array was displayed. CODE SO FAR: import java.util.*; public class ArrayInteger { public static void getRandomNumber(int A[],int n){ Random...
Write a function pretty_print, which takes one parameter that can be any type of namedtuple. It...
Write a function pretty_print, which takes one parameter that can be any type of namedtuple. It "pretty prints" the contents of the namedtuple, including both the names of its fields and their values. This is subject to a few rules. Each field and its value are displayed on one line, with the name of the field appearing first, followed by a colon and a space, followed by the value of the field converted to a string. The fields should be...
In C++ Write a function which takes two parameters: an array of ints and an int...
In C++ Write a function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. As an example, if the array has the following 10 elements: 2 5 8 9 7 1 0 2 6 3, your function should print out 8 9 7 6. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void...
Define a function called 'filterWords, which takes a parameter. The first parameter, text" is an nltk.text.Text...
Define a function called 'filterWords, which takes a parameter. The first parameter, text" is an nltk.text.Text object. The function definition code stub is given in the editor. Perform the given operation for the 'text' object and print the results: • Filter the words whose length is greater than 15 from the complete set of 'text', and store into "large_words' variable as a list
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT