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

AI Thread Summary
The discussion revolves around a programming problem that requires writing a function to find a pair of integers in a sorted vector whose product equals a specified constant. The provided C++ solution demonstrates how to implement this using pointers to return two values, as C++ functions can only return one value directly. The function iterates through the vector, checking pairs of integers to find a match for the product. If found, it updates the values pointed to by the provided pointers and returns true; otherwise, it returns false. The conversation also clarifies the use of pointers, explaining that they allow modifications to the original variables passed to the function, contrasting them with regular parameters that only work with copies of values. This understanding of pointers is essential for effectively implementing the function as specified.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top