In: Computer Science
int get_value(int array[], unsigned int index) { ????? } int main() { int data[] = {1, 2, 3, 4}; get_value(data, 2) = 100; } Write ????? that returns the value in the array at index: Question Blank type your answer...
Answer:
return array[index];
Explanation:
Array indexing starts from 0 to n-1 for an array of size n. In order to use access the value stored in a the array the following prototype must be used
Array_name [ Index_value ];
Array_name is the variable name here it is array
Index_value is the integral value of position of the element stored in array.Here it is "index "
We pass array as a paramenter here.
The array parameter passes its starting address to the function header.
The required index is passed through the variable "index" which of type unsigned int.
The value of position which we required ie ("2") passed from main part is copied to the variable index
The function is of the type int Hence it must return some value.
We make use of the return keyword to pass the required value to the calling function.
Since Array indexing starts from 0 to n-1 for an array of size n.
The value in position "2" will be the third element of the array.
int get_value(int array[], unsigned int index)
//return type of the given function is int hence we used the keyword return here.
{
return array[index];
}
int main()
{
int data[] = {1, 2, 3, 4};
get_value(data, 2) = 100;//This is an irrelavent statement will cause error.
}