Taking values out of a function to use it in another?

Hello , I am new to programming and i'm really stuck!
Our teacher gave us a problem , i'm so close to finishing it , but the last part says:
- "Create a function max_vector and a function sum_vector , from what you have typed in read_vector , find the max vector and the sum of the values of the vector".
P.S. i can't link the whole problem because she said if she sees it on the internet we will get a very bad note.

So i will try to post the best as i can..:

void read_vector()
{
//here you get the values that you type , as well as how many values your vector will have ( example: i typed 1,2 and 3 but note that there can be even 5 values in the vector , like for example: 1,2,4,5 depending on how many you want to type)
}
void max_vector()
{
//search the highest value of the vector that you typed ( example: 3 for us )
}
int sum_vector()
{
//make a sum of any typed numbers ( 1+2+3 = 6 ).
}
int main()
{
cout<<"...";
read_vector();
max_vector();
sum_vector();
}

I only need max_vector and sum_vector

to note that my vector si v[20] and i have the variables a and i.
P.S.S. I also can't copy paste because it's a very big code , and many parts not in english , except for the programming(cout, naming of vectors etc).
Also i know how to to all those , but i do not know how to take the values out of the read_vector(); function , we can't have stable values , the user can type what numbers he/she wishes to , he/she must still get the highest a number and the b sum.


TL;DR: how do i take the values i typed in the function x and add it to y and z ( for y i need to search the highest number in x and for z to make the sum of x elements ).
Please help..
Last edited on
1) When posting code, please use code tags to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2) You can pass values into functions, and back out again, as arguments to the function.

You can also return a value from a function.

Your textbook should explain all of this in the section on functions.
Also, DON'T POST MULTIPLE THREADS FOR THE SAME QUESTION. It wastes everyone's time.

(Duplicate thread: http://www.cplusplus.com/forum/general/237939/ )
Last edited on
Then don't waste my time with your useless replies , read what i wrote then reply.
Deleting the duplicate was justified , please stop replying if you do not have a correct answer with examples.
ALSO I CAN SEE WHAT YOU ARE TYPING , YOU DON'T NEED TO CAPS LOCK , ANNOYING ISN'T IT ?
Last edited on
I've answered your question. If there's something further you need to know, then by all means ask further questions about it.
void read_vector()
{
int n,answer;
cout<<"\n\tn=";
cin>>n;
for(i=0;i<n;i++)
cout<<a<<" ";

for(i=0;i<n;i++)
{
cout<<"\n\tv["<<i<<"]=";
cin>>v[i];

}
cout<<"\n\tPrint elements from the vector after typing its values"<<endl<<endl;
for(i=0;i<n;i++)
cout<<v[i]<<" ";


}
void max_vector()
{

}
void sum_vector()
{

}
Okay so i posted half of my code , i need to see my vector's max value now , i can type anything into read_vector(); so i need to take the values from it and somehow search t he max_vector and after make a sum of vectors.
I'd gladly appreciate your help.

P.S. what has not been declared here was in the whole code , so don't worry about that , i just want to know the thing about max and sum _vectors
Last edited on
If your teacher is requiring you to use global variables, then I think you have a bad teacher, but nevertheless... here's an example of manipulating a vector.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Example program
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>

void foo()
{
    
    // Create vector
    std::cout << "How many values in your vector do you want: ";
    size_t vec_size;
    std::cin >> vec_size;
    std::vector<int> vec(vec_size);
    
    // Populate vector
    for (size_t i = 0; i < vec.size(); i++)
    {
        std::cout << "Enter value for index " << i << ": ";
        std::cin >> vec[i];
    }
    
    // Display vector
    std::cout << "Your entered { ";
    for (size_t i = 0; i < vec.size(); i++)
    {
        std::cout << vec[i] << " ";
    }
    std::cout << "}\n";
    
    // Other measurements on vector
    int sum = std::accumulate(vec.begin(), vec.end(), 0);
    std::cout << "Sum = " << sum << "\n";
    
    std::cout << "Average = " << static_cast<double>(sum) / vec.size() << "\n";
    
    std::cout << "Max element = " << *std::max_element(vec.begin(), vec.end()) << "\n";

}

int main()
{
    foo();
}
Last edited on
Has your teacher not taught you how to get user input? Or write to an array/vector index?
If your teacher is requiring you to use global variables, then I think you have a bad teacher, but nevertheless...

Unfortunately not ...

That's a very nice code , thank you very much , but one thing is that my teacher will notice that it's not made by me , i actually am the best in class at c++ and yet she will easily notice i didin't make this , regardless i thank you for this beatiful code , will be a little hard have a grasp of your code.

One more thing @Ganando i asked for the highest number in the vector (just for it to show on screen), not the average of them.
Last edited on
the standard hand-rolled algorithm for highest is just this simple loop. Theres a built in way to get it too, but you probably will not want that yet.

highest = first;
for(..)
if(next value > highest)
highest = next value

one simple way to return a bunch of stuff from a free floating function is just to wrap all the values into a struct.

struct s
{
int a;
double d;
}

s foo()
{
s tmp;
tmp.a = 2;
tmp.d = 3.14;
return tmp;
}

later, classes (and better written structs) will keep the data and functions together making this sort of code unnecessary, but its handy for simple programs and early learning.



Topic archived. No new replies allowed.