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

In summary: Some languages (e.g. Java) have a "generic pointer" which is just a pointer that does not have a particular type attached to it. So in Java, for example, foo(&x) is the same as foo(x). */In summary, the function finds a pair of integers in a vector that have a product that equals a constant. If the product is found, the function returns the two integers. If the product is not found, the function returns "not found".
  • #1
ineedhelpnow
651
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
  • #2
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:
  • #3
how do you know that you need to use pointers in this? i don't fully understand them...
 
  • #4
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.
 
  • #5


There are a few different approaches that could be used to return two values from a C++ function without using a class. One option would be to use pointers as parameters in the function, and update the values of those pointers within the function. For example:

void findProductPair(const vector<int>& vi, int product, int* first, int* second){
// loop through the vector, checking for pairs whose product equals the given constant
for(int i = 0; i < vi.size(); i++){
for(int j = i+1; j < vi.size(); j++){
if(vi * vi[j] == product){
// update the values of the pointers to the pair that was found
*first = vi;
*second = vi[j];
return;
}
}
}
}

In this approach, the caller of the function would need to pass in the addresses of two variables to store the values of the pair that was found.

Another option would be to use a reference parameter in the function, which would also allow the function to update the values of the variables passed in by the caller. For example:

void findProductPair(const vector<int>& vi, int product, int& first, int& second){
// loop through the vector, checking for pairs whose product equals the given constant
for(int i = 0; i < vi.size(); i++){
for(int j = i+1; j < vi.size(); j++){
if(vi * vi[j] == product){
// update the values of the reference variables to the pair that was found
first = vi;
second = vi[j];
return;
}
}
}
}

Similar to the first approach, the caller of the function would need to pass in two variables to store the values of the pair that was found.

Overall, both of these approaches involve passing in variables or pointers by reference, which allows the function to update their values and return them to the caller.
 

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

1. What is C++ programming?

C++ is a high-level, general-purpose programming language that was developed by Bjarne Stroustrup in 1983. It is an extension of the C programming language and is commonly used for developing operating systems, games, and other complex software applications.

2. What are the benefits of using C++?

C++ offers several benefits, including high performance, portability, and a wide range of libraries and tools. It also allows for low-level memory manipulation and supports object-oriented programming, making it suitable for various types of applications.

3. What are some common problems encountered in C++ programming?

Some common problems in C++ programming include memory leaks, dangling pointers, and buffer overflows. These issues can lead to program crashes, unexpected behavior, and security vulnerabilities.

4. How can I learn C++ programming?

There are various ways to learn C++ programming, such as online tutorials, books, and courses. It is recommended to start with the basics of programming before diving into C++. Practice and hands-on experience are crucial for mastering the language.

5. What are some tips for solving C++ programming problems?

Some tips for solving C++ programming problems include breaking down complex problems into smaller, manageable chunks, using a debugger to identify and fix errors, and writing clean and organized code. It is also helpful to collaborate with other programmers and seek help when needed.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
28
Views
3K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
9
Views
4K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
Back
Top