1

I am new to c++ and I am trying to create a function to return a value from an array. Here are the instructions for the assignment:

In this exercise, you will create a function to return a value from an array. If the index is out of range, return 0.

Function Name: read01

Parameters: (data,size,index)

1) data: An array of constant int's

2) size: An int, the number of slots in data

3) index: An int, the desired position in data

Return Value: An int, the value of position index in data, or 0.

Here is what I tried so far:

int read01(int data[], int size, int index){
    return data[index];
}

I know how to do the "if" statement so that's not an issue, but where I am confused is how to define the size parameter as the size of data[]. I know you cannot put "int size" into the data parameter like so, "int data[int size]", but I cannot figure out another way to do it. Any help would be appreciated!

10
  • You should format the assignment as a blockquote, not as code (since it is not code). Commented Sep 5, 2017 at 0:43
  • int data[], is correct already Commented Sep 5, 2017 at 0:45
  • @M.M then what's the point of having a size parameter if it's not being used to define the size of data? Commented Sep 5, 2017 at 0:48
  • Are you asking how to set the length of the array via the size? Something like, size = 4, so The array size should be of length 4? Commented Sep 5, 2017 at 0:49
  • 2
    @BrockMorrison the "user" defining the size is not your function. The array is created outside your function and your function receives a view on it, and the length of the view is the size parameter. Your function does not create or resize any arrays, it reads a value out of an array that already exists Commented Sep 5, 2017 at 1:14

2 Answers 2

4

We can define size of array like:

int Data[5]; //here 5 is size of array or

int Data[] = {1, 2, 3, 4, 5}; //In this number of elements describe the size.

To get size of array use(standard c way: to get size of static array):

sizeof(Data) / sizeof(Data[0]);

you can also try Distance method to get size:

distance(begin(Data),end(Data));

Edit: You cant make array user defined because size of array needs to compile at compile time. So options are you can use vectors or pointers.

But in case of c++ use std::vector instead of arrays:

std::vector<int> vc;
vc.push_back(1);
vc.push_back(2);
vc.push_back(3);
std::cout << vc.size() << std::endl; // it will print 3(size of vector)

If you wanna define size dynamically then try something like:

int size;   
std::cin >> size;
int *A = new int[size];//dynamically assign memory to pointer A 
/*your code here*/
delete[] A;

Hope it will help you..

Sign up to request clarification or add additional context in comments.

5 Comments

I don't see how any of this answers the OP's question.
@user4581301 if i get the question he wanna define size of array or i am missing something
@Preet I am asking how to set the length of the array via the size parameter... so when the the size parameter is defined as size = 4, the array size should be 4. As I stated in the comments above, this is being entered into a unit test where I have no idea what is being used as parameters in my code.
@BrockMorrison you cannot change the size of an array, nor does the assignment appear to be asking you to do so. My suspicion is you are expected to test if index is less than size ( and return the value in data at index) or return 0 if index is not less than size. Also watch out for "data: An array of constant int's" because you have not specified data to be constant in int read01(int data[], int size, int index). Read up on the const keyword.
sorry to miss the concept, Actually array size needs to define at compile time so you cant change it as @user4581301 said. if you wanna do this you can use std::vector
1

I believe the answer would be

#include <iostream>
int read01(const int data[], int size, int index) {
    if ( index >= size ) {
        return 0;
    } else {
        return data[ index ];
    }
}

1 Comment

This worked perfectly! I had it returning 0 outside of the if statements and it kept throwing me an error. Thank you @mychsmit!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.