C/C++ [C++] How to return and call vectors from functions?

  • Thread starter Thread starter Yohanna99
  • Start date Start date
  • Tags Tags
    Functions Vectors
AI Thread Summary
The discussion focuses on a beginner coder's challenges with using vectors as parameters and return values in C++. The coder seeks clarification on how to properly call functions that return vectors and how to handle vector parameters. Specific code examples are provided, including functions for gathering string inputs and calculating the average of a vector of floats. Key points include the need to output the return values from these functions and the suggestion to initialize variables properly. The response emphasizes that vectors can be returned like any other object, and provides direct code snippets to demonstrate how to output the return values from the defined functions.
Yohanna99
Messages
3
Reaction score
0
Hi, beginner coder here. I have a somewhat solid understanding of both vectors and functions, and have used the two of them many times, but I'm have trouble coding functions that have vectors in their parameters and as their return values.

Another thing I'm having trouble with is calling the return value from a function when the parameter is a single vector. The c++ forum explains how to call the return when the parameters are variables and I get that but there's no example with vectors.

Thanks :)
 
Technology news on Phys.org
Could you give some specific code that you've tried with exactly what you want it to do? There's really nothing special about a vector object that is returned from a function; the only thing possibly is whether it is a reference to the object, pointer to the object or an object itself.
 
This is very rough code just to give you an idea. Ignore things that may make no sense I'll edit it again soon.
Code:
#include <iostream>
#include <string>
#include <vector>

using namespace std;

string get_string_input(vector<string> strings);

float averageFunc(vector<float> floating);int main()
{

	cout << "enter a word. When you're done, enter done." << endl;

	vector<string> strings;

	string input;

	while (input != "done")
	{
		cin >> input;

		if (input == "done")
			break;
		else
			strings.push_back(input);
	
	}	cout << "your sentence is: ";
	for (int i = 0; i < strings.size(); i++) {

	// how to output return val from get_string_input

	

	cout << "enter a num. When you're done, enter 10001." << endl;

	vector<float> floating;

	float num;	while (num != 10001)
	{
		cin >> num;

		if (num == 10001)
			break;
		else
			floating.push_back(num);

	}
	

	// how to output return val from averageFunc	system("pause");
	return 0;
}

/*
The first function will be get_string_inputs, which will return a vector of strings.
This function should continually loop, taking input from cin, and putting that into
the vector that will be returned. It should take one parameter, which is the string
to take in that will denote that input should stop being received. When this input 
is found, the function will return the vector.
*/

string get_string_input(vector<string> strings) {	string s;
	for (int i = 0; i < strings.size(); i++) {

		s = strings[i];
		cout << s << " ";
	}
	

	return s;
}

/*
The second function will be an average function. This function should
take a vector of floats as input, and return a single float value that
is the average of the vector passed in.
*/
float averageFunc(vector<float> floating) {

	float total = 0.0;

	for (int i = 0; i < floating.size(); i++)	{

		total += floating[i];
}
	

	float avrg = total / floating.size(); // average of vector

	return avrg;
}
 
Yohanna99 said:
This is very rough code just to give you an idea. Ignore things that may make no sense I'll edit it again soon.

Hi Yohanna99! Welcome to MHB! (Smile)

It appears you're looking for:
Code:
// how to output return val from get_string_input
cout << get_string_input(strings) << endl;
...
// how to output return val from averageFunc
cout << averageFunc(floating) << endl;
Oh, and [m]num[/m] should really be initialized:
Code:
float num = 0;
 
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...

Similar threads

Replies
10
Views
2K
Replies
23
Views
2K
Replies
39
Views
4K
Replies
19
Views
2K
Replies
18
Views
1K
Replies
52
Views
4K
Back
Top