C++: Can a function return two values?
-
Help, please! I am trying to get TWO values from this function. My assignment is to create a function that searches an array for a TARGET number, then return the FIRST INDEX that contains that number. It has to ALSO return number of the LAST INDEX that contains that same number. My program is: #include <iostream> using namespace std; int search (int a[], int size, int target); int search (int a[], int size, int target) { bool found = false; int index = 0; while ((found == false) && (index < size)) { if (a[index] == target) { int j = index; found = true; } else { index++; } } if ((found == false)) { index = -1; } return index; } int main() { int size = 10; int a[10] = {1, 2, 4, 6, 9, 10, 14, 9, 19, 27}; int target = 9; cout << search (a, 10, 9) << endl; } Of course, it's retuning only one value. Please help!
-
Answer:
Many ways one can return more than one value. refer www.ritchcenter.com/nbv I use reference variables here. int search (int a[], int size, int target, int &A, int &B); int search (int a[], int size, int target, int &A, int &B) { A=-1; B=-1; for(int i=0;i<size;i++) if (a[i]==target) { A=i; break; } for(int i=size-1;i>=0;i--) if (a[i]==target) { B=i; break; } } int main() { int size = 10, L, R; int a[10] = {1, 2, 4, 6, 9, 10, 14, 9, 19, 27}; int target = 9; search (a, 10, target,L,R); cout<<L<<endl<<R << endl; return 0; }
IAmFine at Yahoo! Answers Visit the source
Other answers
You cannot return two values in C++. Instead, you can return a pointer to an array, or alternatively change the parameters using references. Alternative 1: int* search (int a[], int size, int target) { int* result = new int[2]; //your code result[0] = firstIndex; result[1] = secondIndex; return result; } Alternative 2: void search(int a[], int size, int target, int &firstIndex, int &secondIndex) { //your code firstIndex = index; //your code secondIndex = index; } If you have additional questions let me know.
therandomer
You can return an array of 2 elements or a struct location { int firstIndex; int lastIndex;}. You can also use reference and pass in firstIndex and lastIndex as reference then modify their values inside you function and they value will be preserved after the function execution.
AirMc
No. There are a gajillion ways to get the extra value back out of the function. You could put it in a global variable, or a data location for which the reference gets passed into the function, or even cram two 32-bit integers into a single 64-bit integer if you're feeling especially hacky today. However, you are guaranteed that on any given call to a function, no more than one return statement will be executed, and it will return a single value of the specified type.
green meklar
I agree with the answer posted by 'husoski'.
Aaren
There are a couple of ways to do this. 1) return a struct that contains both numbers as public (by default for a struct) fields. struct search_result { int first, last; }; search_result search(...etc.) { search_result result = {-1,-1}; ...etc. storing match index values in result.first and result.last, and finally return result; } Then call with: search_result sr = search(...etc.); if (sr.first == -1) { ... not found ... } 2) Same idea as above, but use the pair<> class from #include <utility>: pair<int,int> search(...etc.) { int first=-1, last=-1; ...do your search, storing index values in first and last return pair<int,int>(first,last); } ...then call with: pair<int,int> sr = search(...etc.); if (sr.first == -1) {...not found...} if (sr.first == sr.second) {...only one match found...} Note that the pair<> fields are first and second, not first and last. 3) Use a pointer or reference argument to return the last index. int search(int a[], int size, int target, int &last) { int first=-1; last = -1; ... do your search, setting first and last return first; } Then call with int last; int first=search(a, 10, 9, last); I omitted prototypes, and didn't retype or paste your code, but I hope you get the ideas involved. Like the Perl folks' motto says: There Is More Than One Way To Do It.
husoski
Related Q & A:
- How can I pass global variables into a function?Best solution by Stack Overflow
- Can a MongoDB key have two arrays?Best solution by Stack Overflow
- Can a non-profit charity based in India get 301 (c) tax exemption in the US?Best solution by Yahoo! Answers
- How can I correct sent emails from showing a wrong return address?Best solution by Yahoo! Answers
- Can a person have more than two alleles for a single gene?Best solution by answers.yahoo.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.