两个向量的总和.. !! [英] Sum Of Two Vectors ..!!
本文介绍了两个向量的总和.. !!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写代码,即两个向量的和.我全力以赴,失败了,
你能帮我吗..?! (重新更新)
I''m trying to write a code i.e sum of two vectors. I''ve put my all efforts and fail,
Will You Please Help Me .. ?! (RE-UPDATED)
#include "stdafx.h"
#include "stdafx.h"
#include <iostream>
using namespace::std;
int SumOfColumns (int p[], int q[], int z[])
{
int i=0;
for(i=0; i<=4; i++)
{
z[i]=p[i] + q[i];
}
return 0;
}
void main()
{
int p[4]={9,1,5,4};
int q[4]={3,1,2,2};
int z[4]={0,0,0,0};
cout<<SumOfColumns(p[], q[], z[])<< endl;
//cout<<z[0];
//cout<<z[1];
//cout[2];
//cout<<z[3]<<endl;
推荐答案
让我们看看.
您具有接受三个向量并只返回一个整数的函数SumOfColumns
,但是当您调用该函数时,您只是给出(4,4,4)
,而这些都是纯整数,根本不是向量.
在第二学期,您不会在任何地方声明z[]
.您应该在使用它之前声明它.
然后,您要制作一个遍历数组的双循环,如果要添加像p[4][4]
这样的向量,则将需要一个双遍循环,但是您的向量只是一维,所以这是错误的.
然后,您声明sum
并在z[sum]=...
中使用它,这是错误的,因为z[]
将具有4个元素,就像其他两个元素一样.您只需要使用与读取p[]
和q[]
的值相同的位置,所以将sum
用作位置会给您带来问题,因为您正尝试访问一个不存在的位置. br/> 另一个问题是,您要返回302
作为函数的返回值,因此cout
的输出将仅为302
,而无需注意z[]
的值.如果要打印总和,则需要在此处使用z[]
.
另外,请访问 http://www.cplusplus.com/doc/tutorial/arrays/ [^ ]
对我来说,向您发布执行该操作的代码本来会更容易,但是那样一来,您仅复制解决方案就不会学到任何东西.请阅读我的评论,然后尝试更正您的代码.如果您更改代码后仍然遇到问题,请使用改进问题"来更改您的信息并提供新代码.然后使用有问题或评论?"说我你改变了它.我会检查的.
问题发布后的新内容
好吧,让我们看看.
现在,添加p[]
和q[]
到几乎是正确的:您已经将 z
声明为int*
,但是p
和q
不是int*
.由于您已经开始,所以我建议您一步一步地进行操作(换句话说,将指针留给以后使用).您可以使用与p
和q
相同的方式声明z
,但是将0
作为所有4个组件的初始值. 然后,您仍然会返回302作为该函数的返回值,并直接在cout
处调用该函数,因此您不会在任何地方看到z[]
的内容.您可以通过许多不同的方式解决此问题:
--- a)您将SumOfColumns
声明为void
,不返回任何return
.然后,在将数组声明为独立行之后调用函数,然后手动进行cout
传递z[]
的组成部分(这意味着4次,对于z []的每个组成部分一次)
--- b)将cout
的行写在循环内(添加组件时),而忘记了main
中的那一行.
--- c)创建一个函数来打印z []的内容,并在调用SumOfColumns之后调用它(请看一下我第一次给您的链接说明的末尾,他们有一个例子在那里,数组作为参数"部分)
---(其他可能性)
告诉我你什么时候完成
最后评论后的新内容
如果您仔细阅读并分析所做的更改,那么您已经拥有获取解决方案所需的所有信息.在函数中返回302
之前:没有出现现在的错误,但是在cout
中打印302(=函数的返回值).
-如果不要求在与cout
相同的行中调用该函数,则只需要在声明数组之后调用该函数,然后像在c中所做的那样打印z[i]
的值您的最新版本.
-但是,如果您必须在与cout
相同的行中调用该函数,则可以按以下步骤进行操作.不必在函数内部进行循环以仅通过调用一次添加所有4个组件,而必须在main
中进行循环,并仅提供p[]
和q[]
的实际组件作为参数,然后return
值的加法.为此,您需要声明该函数以返回一个int并接受两个int作为参数(而不是整个数组).
再试一次,告诉我什么时候完成
Let''s see.
You have the functionSumOfColumns
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 declaringz[]
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 likep[4][4]
but yours are just one dimension, so that''s wrong.
Then you are declaringsum
and using it inz[sum]=...
, that is false becausez[]
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 ofp[]
andq[]
, so usingsum
as position will give you problems, because you are trying to access to a non existant position.
Other issue is, you are giving302
back as return value of the function, so the output ofcout
will be just302
, without paying atention to the values ofz[]
. If you want to print out the sum, you will need to usez[]
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.
New content after the edition of the question
Ok, let''s see.
The addition ofp[]
andq[]
to givez[]
is now almost correct: You have declaredz
asint*
butp
andq
are notint*
. Since you are starting I recommend you go one step after another (in other words, leave pointers for later). You can declarez
the same way you did withp
andq
, but giving0
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 atcout
, so you are not going to see the content ofz[]
anywhere. You can solve this in many different ways:
--- a) You declareSumOfColumns
asvoid
, giving noreturn
back. Then you call the function after the declaration of the arrays as stand alone line, and then you docout
passing the components ofz[]
manually (it means 4 times, one for each component of z[])
--- b) You write the line of thecout
inside the loop (when you are adding the components) and forget about the one inmain
.
--- 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
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 returning302
in your function: You didn''t got the error you have now but you were printing 302 in thecout
(= the return value of the function).
- If it is not a requirement that you call the function in the same line ascout
, then you just have to call the function after the declaration of the arrays alone, and then print values ofz[i]
as you have done in your last version.
- But... if you have to call the function in the same line ascout
... 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 inmain
and give only the actual component ofp[]
andq[]
as parameter, and thenreturn
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
尝试一下
Try this
#include "stdafx.h"
#include <iostream>
using namespace::std;
void SumOfColumns (int *p, int *q, int *z)
{
for(int i = 0; i < 4; i++)
{
z[i] = p[i] + q[i];
}
}
void main()
{
int p[4]={9, 1, 5, 4};
int q[4]={3, 1, 2, 2};
int *z = new int[4];
SumOfColumns(p, q, z);
for (int i = 0; i < 4; ++i)
{
cout << i << ": " << z[i] << endl;
}
}
我会写
void SumOfColumns (int p[], int q[], int z[], size_t size)
{
int i;
for(i=0; i<size; i++)
{
z[i] = p[i] + q[i];
}
}
这篇关于两个向量的总和.. !!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文