Question

In: Computer Science

int get_value(int array[], unsigned int index) { ????? } int main() { int data[] = {1,...

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...

Solutions

Expert Solution

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.

}


Related Solutions

class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};   ...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};        //Complexity Analysis //Instructions: Print the time complexity of method Q1_3 with respect to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //TODO }    public static void Q1_3(int[] array){ int count = 0; for(int i = 0; i < array.length; i++){ for(int j = i; j < array.length;...
Can anyone translate this to flowgorith. #include<stdio.h> int main()    {    int seatingChart[10];       //array...
Can anyone translate this to flowgorith. #include<stdio.h> int main()    {    int seatingChart[10];       //array for seating chart       int fc = 0, ec = 5,i;       //starting positions for first class and economy class       printf("Welcome to the Airline Reservation System (ARS)\n\n");       for(i=0;i<10;i++)       //initializing the array with zero        {        seatingChart[i] = 0;        }    char choice;       do        {        int ch;   ...
What is an array data structure? What is an array index? What are the benefits of...
What is an array data structure? What is an array index? What are the benefits of array structures? What are the drawbacks of array structures? What is a grid structure? Give examples of when an array could be used. Give examples of when a grid could be used.
class ArrayReverse1{ public static void reverse(int[] a, int index) { if (index >0) { index= index...
class ArrayReverse1{ public static void reverse(int[] a, int index) { if (index >0) { index= index - 1; // Decrementing the index System.out.printf("%d%n", a[index]); reverse(a, index); // Recursive call } return; } public static void main (String args[]) { int [] array = { 1, 2, 3, 4, 5 }; int n=array.length; reverse(array,n); // function call } } Write a generic version of the corrected recursive reverse method that could be used to print any of the following arrays (or...
class Main { public static void main(String[] args) { int[] array = {1,2,3,4,5}; //Complexity Analysis //Instructions:...
class Main { public static void main(String[] args) { int[] array = {1,2,3,4,5}; //Complexity Analysis //Instructions: Print to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //code here } public static void (int[] array) { int count = 0; for(int i = 0; i < array.length; i++) { for(int j = i; j < array.length; j++) { for(int k = j; k < array.length; k++)...
Java try and catch problem public void method1(){ int[] array = new int[1]; try{ array[2] =...
Java try and catch problem public void method1(){ int[] array = new int[1]; try{ array[2] = 0;} catch(ArithmeticException e){array[2] = 0} // ? finally{ return 0;}} Could you please tell me why the line with the question mark will not report an error?
Write a program in C that declares the following array: int. array[] = { 1, 2,...
Write a program in C that declares the following array: int. array[] = { 1, 2, 4, 8, 16, 32 } Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index". Write some code that retrieves the item specified by the index, like this: int item = array[index]; Then write code that outputs the corresponding array entry based on the number the user entered. Example output: The...
Using the following array: //may be declared outside of the main function const int NUM_Games =4;...
Using the following array: //may be declared outside of the main function const int NUM_Games =4; //may only be declared within the main function int scores[NUM_GAMES] = {122, 76, 92, 143}; Write a C++ program to run a menu-driven program with the following choices: 1) Display the scores 2) Change a score 3) Display game with the highest score 4) Display a sorted list of the scores 5) Quit Write a function called getValidScore that allows a user to enter...
convert this string type   program into interger data type #include "stdafx.h" #include #include unsigned int putIntoHashTable(char...
convert this string type   program into interger data type #include "stdafx.h" #include #include unsigned int putIntoHashTable(char *ptrInputData, unsigned int bufferLength); // function to add to hash table unsigned int getFromHashTable(char *ptrOutputData, unsigned int bufferLength); // function to retrieve data from hash table #define INPUT_BUFFER_SIZE 200 // local buffer used for adding data to the hash table (there is no reason in this assignment to change this value) #define HASH_SIZE 100 // size of hash table to be used (for testing...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1;...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1; hour <= 12; hour++)     {         for (min = 0; min <= 59; min++)         {             cout << hour << ":" << min << "AM" << endl;         }     }       return 0; } 1.      Type in the above program as time.cpp. Add a comment to include your name and date. Compile and run. 2.      What is the bug or logic error in the above program? Add the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT