How can a C++ function return two values to the caller without using a class?

Click For Summary
SUMMARY

This discussion focuses on implementing a C++ function that returns two integer values without using a class. The solution involves using pointers to pass the values back to the caller. The provided code demonstrates how to find a pair of elements in a sorted vector whose product equals a specified constant, utilizing the function findProduct. The implementation effectively uses pointer parameters to modify the values of two integers, allowing the function to return multiple results.

PREREQUISITES
  • C++ programming language fundamentals
  • Understanding of pointers and memory addresses in C++
  • Knowledge of vector data structures in C++
  • Basic algorithm design for searching and comparing values
NEXT STEPS
  • Explore advanced pointer usage in C++, including smart pointers
  • Learn about C++ reference parameters and their advantages over pointers
  • Investigate the implementation of classes for returning multiple values
  • Study sorting algorithms and their impact on search efficiency in vectors
USEFUL FOR

Software developers, particularly those working with C++, who need to understand how to return multiple values from functions and manage memory effectively using pointers.

ineedhelpnow
Messages
649
Reaction score
0
I was given this problem a while ago to complete as a bonus but I wasn't able to do it. I just came across it and would like to know how it should be done.

Given an integer vector "vi" that is already sorted in ascending order (all elements of the vector are positive), write a function that returns the first found pair of elements whose product equals to a constant "Product". For example, if PRODUCT is 32 the product has:
2 4 6 8 13 15
The function should identify 4 and 8 as the pair.

Define your function appropriately so that both values will be passed back to the caller of the function. However, you are not required to implement the function call.
 
Technology news on Phys.org
I assume the due date is past. Here's a complete solution. From your posts, I suggest you try and understand it completely.

Code:
 #include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

int findProduct(const vector<int>& v, int* first, int*second, int product);

int main(int argc, char** argv) {
  vector<int> vec;
  int i;
  for (i = 1; i <= 4; i++) {
    vec.push_back(2 * i);
  }
  vec.push_back(13);
  vec.push_back(15);
  int one, two;
  if (findProduct(vec, &one, &two, 1)) {
    cout << one << " and " << two << '\n';
  } else {
    cout << "not found" << '\n';
  }
  if (findProduct(vec, &one, &two, 32)) {
    cout << one << " and " << two << '\n';
  } else {
    cout << "not found" << '\n';
  }
  return 0;
}

/* Upon entry, v is a sorted vector of positive ints.  If product is the product of
   two elements of v, then the "var" parameters first and second are set
   to the first such values and return is 1 (true).  Otherwise return is 0
   neither *first nor *second is changed
 */

int findProduct(const vector<int>& v, int* first, int*second, int product) {
  int i, j;
  for (i = 0; i < v.size() - 1 && v[i] <= product; i++) {
    for (j = i + 1; j < v.size() && v[i] * v[j] < product; j++) {
      ;
    }
    if (j < v.size() && v[i] * v[j] == product) {
      *first = v[i];
      *second = v[j];
      return (1);
    }
  }
  return (0);
}
 
Last edited:
how do you know that you need to use pointers in this? i don't fully understand them...
 
The specifications were that two int values should be returned to the caller. A C++ function can return only one value. So there are two choices, make a class which can contain two int values and have the return an object (or pointer to) such a class. The second easier way is to have parameters that can be changed by the function and upon function return, these changes are available to the caller.
Now for ordinary parameters (say ints), even if the actual argument is a variable, the function can not change the caller's variable (a copy of the value of the argument is always given to the function).
So in C++, there are two kinds of parameters that allow changes to an argument to be available to a caller.
One is a reference parameter, say <type>parm &. But this is really just a "pointer" parameter. For a pointer parameter, say int* first, what is given to the function when it is called is a copy of the actual argument (which must then be a pointer - address).
Example:
function call: foo(&first);
actual function:
void foo(int* what) {
}
Suppose the address of variable first is 1000; i.e &first==1000. When function foo is called, what has value 1000 (foo could change this to something else, but normally this won't happen). Now the dereferencing operator * is the value found at the pointer. So *what is synonymous with the value at address 1000. Suppose foo has assignment *what=17; Then at address 1000 is the integer value 17. When foo returns (assuming no further change to the value at address 1000), this value is still 17. So when the caller inspects first, what is its value, why 17. So in effect foo does change the value of first.

Addendum: a lot of CS people don't like to equate pointers and addresses, but I think it's the easiest way to understand pointers.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 28 ·
Replies
28
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 3 ·
Replies
3
Views
9K