Returning arrays from a function

In summary, the conversation discusses a C++ assignment involving writing a sub-program to fill two arrays with random numbers and store the answers to 10 arithmetic questions in a third array. The main issue is with calling the function and returning the array from it. There are several errors in the function skeleton, including mismatched return types and parameters, and incorrect indexing of the array. The correct way to return an array in C/C++ is to pass it as a parameter or return a pointer to the array.
  • #1
sdoug041
26
0
hello. I am working on a c++ assignment right now, and my job is to write a sub-program that will fill two arrays with random numbers (1-10), then ask the student 10 arithmetic questions (either multiply or divide depending on a previous choice) and store the answers in a third arrray. I come across problems when trying to call the function and returning the array from the function. (the answers)

in the main () i have something like

int student_answers [10];
student_answers [10] = run_test ();

the above 3 lines are an attempt to call the function but I have limited knowledge about arrays.

in the sub-program itself I have something like:

int run_test (int verification)
... bunch of code... stores answers in answer[10]
return (answers[10]);

is ths the correct way of returning an array?
 
Last edited:
Technology news on Phys.org
  • #2
No. A C/C++ function can't return an entire array. It can return a pointer to an array, or the address of an existing array can be passed as a parameter to the function, and when the function exits, the array will have its values filled in. (BTW, the term subprogram is not usually used in C/C++; the term most often used is function.)

Look at your text and/or notes to see how arrays are passed as parameters in functions.

sdoug041 said:
in the sub-program itself I have something like:

int run_test (int verification)
... bunch of code... stores answers in answer[10]
return (answers[10]);
There are several errors in the function skeleton above.
  1. run_test returns an int, a single integer value. Your intent was to return an entire array (which you can't do), so there is a mismatch between what your function returns and what your previous statement intends to do.
    Code:
    student_answers [10] = run_test ();
  2. In the call to run_test just above, there are no parameters, but in your function definition, there is an int parameter, verification.
  3. Where you said "stores answers in answer[10]" there are two problems: For an array with 10 elements, the last element is answer[9], not answer[10]. Also, you are apparently trying to put all your answers in one element of your array, not in answer[0], answer[1], ..., answer[9].
  4. Your return statement attempts to return a single value in your array, answer[10]. As already noted, this is not an element in your array. Furthermore, it is not an array: it is a single integer value.
 
  • #3


Hello there,

First of all, great job on working on your assignment and trying to understand how to return arrays from a function. Your approach is on the right track, but there are a few things that can be improved.

In your main() function, you are attempting to call the run_test() function and assign the returned array to the student_answers array. However, this line of code is incorrect: student_answers[10] = run_test();. This is because you are trying to access the 11th element of the array (remember that arrays start counting at 0), which does not exist. Instead, you should simply write: run_test(student_answers);. This will pass the student_answers array as a parameter to the run_test() function.

In your run_test() function, you are on the right track by declaring it as an int function and returning an array. However, the way you are returning the array is incorrect. The return statement should simply be: return answers;. This will return the entire array to the caller.

Additionally, since you are passing the student_answers array as a parameter to the run_test() function, you do not need to declare the answers array inside the function. Instead, you can simply use the student_answers array to store the answers.

I hope this helps clarify any confusion you may have had about returning arrays from a function. Keep up the good work!
 

1. What is the purpose of returning an array from a function?

Returning an array from a function allows the function to pass data back to the calling code. This is useful for functions that need to perform operations on a set of data and then return the results.

2. How do you declare a function that returns an array?

To declare a function that returns an array, you can use the following syntax: function functionName() { return arrayName; }. You can also specify the data type of the array being returned, for example: function functionName(): string[] { return arrayName; }.

3. Can a function return multiple arrays?

Yes, a function can return multiple arrays by using the return keyword followed by a comma-separated list of arrays. For example: return arrayOne, arrayTwo;.

4. How do you access the elements of an array returned from a function?

To access the elements of an array returned from a function, you can use the same syntax as accessing elements of any other array. For example, if the function returns an array called myArray, you can access its elements using myArray[index].

5. Can a function modify an array and then return it?

Yes, a function can modify an array and then return it. Arrays in JavaScript are passed by reference, so any changes made to the array within the function will also be reflected in the original array. This can be useful for functions that need to manipulate data within an array and then return the updated version.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
20
Views
1K
Replies
3
Views
766
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
29
Views
1K
  • Programming and Computer Science
7
Replies
235
Views
9K
  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
Back
Top