Let's see.
You have the function
SumOfColumns
that accept three vectors and give back just an int, but when you call the function you are just giving
(4,4,4)
and those are plain integers, not vectors at all.
On second term, you are not declaring
z[]
anywhere. You should declare it before using it.
Then you are making a double loop to go through the arrays, a double loop would be needed if you are adding vectors like
p[4][4]
but yours are just one dimension, so that's wrong.
Then you are declaring
sum
and using it in
z[sum]=...
, that is false because
z[]
is going to have 4 elements just as the other two. You just need to use the same position that you are using to read the values of
p[]
and
q[]
, so using
sum
as position will give you problems, because you are trying to access to a non existant position.
Other issue is, you are giving
302
back as return value of the function, so the output of
cout
will be just
302
, without paying atention to the values of
z[]
. If you want to print out the sum, you will need to use
z[]
there.
As addition, have a look to
http://www.cplusplus.com/doc/tutorial/arrays/[
^]
It would have been easier for me to post you the code to do it, but then you won't have learn anything just copying the solution. Please read my comments and try to correct your code. If you change your code and still gives you problems, then please use "improve question" to change your message and give the new code. Then use "have a question or comment?" to say me that you changed it. I will check it.
EDIT: New content after the edition of the question
Ok, let's see.
The addition of
p[]
and
q[]
to give
z[]
is now
almost correct: You have declared
z
as
int*
but
p
and
q
are not
int*
. Since you are starting I recommend you go one step after another (in other words, leave pointers for later). You can declare
z
the same way you did with
p
and
q
, but giving
0
as initial value for all 4 components.
Then you are still giving back 302 as return value of the function, and calling the function directly at
cout
, so you are not going to see the content of
z[]
anywhere. You can solve this in many different ways:
--- a) You declare
SumOfColumns
as
void
, giving no
return
back. Then you call the function after the declaration of the arrays as stand alone line, and then you do
cout
passing the components of
z[]
manually (it means 4 times, one for each component of z[])
--- b) You write the line of the
cout
inside the loop (when you are adding the components) and forget about the one in
main
.
--- c) You create a function to print the content of z[] and you call it after the call of SumOfColumns (take a look at the end of the explanation of the link I gave you the first time, they have an example there, Section "array as parameter")
--- (other possibilities)
Tell me when you are done
EDIT: New content after last comments
If you read carefully and analyze the changes you have done, you already have all the information you need to get the solution. Before you were returning
302
in your function: You didn't got the error you have now but you were printing 302 in the
cout
(= the return value of the function).
- If it is not a requirement that you call the function in the same line as
cout
, then you just have to call the function after the declaration of the arrays alone, and then print values of
z[i]
as you have done in your last version.
- But... if you have to call the function in the same line as
cout
... you can do it as follows. Instead of making the loop inside the function to add all 4 components with just one call of the function, you have to make the loop in
main
and give only the actual component of
p[]
and
q[]
as parameter, and then
return
the addition of the values. In order to do that you need to declare the function to give an int back and to accept two ints as parameter (not the full arrays).
Try it another time and tell me when you are done