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..
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 ?
}
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
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.
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.
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.