Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
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)

C#
#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;
Posted
Updated 23-Apr-12 3:17am
v8
Comments
Richard MacCutchan 22-Apr-12 10:06am    
Why do you always return the value 302 from your function that does the calculations?

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
 
Share this answer
 
v5
Comments
Chuck O'Toole 21-Apr-12 17:43pm    
Wow, that way very kind and very helpful. +5
Nelek 21-Apr-12 17:48pm    
Thank you.
As I said in other message. All started once and he at least tried it. I think that deserves an answer.
nv3 21-Apr-12 18:08pm    
Got my 5! Not for the problem of course, which was all too easy, but for your teaching talent.
Nelek 21-Apr-12 19:12pm    
Thank you
Usman Hunjra 22-Apr-12 4:22am    
Thank You Nelek for your response ..
Try this

C++
#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;
    }
}
 
Share this answer
 
v3
Comments
Usman Hunjra 23-Apr-12 10:08am    
In first loop u used POSTFIX INCREMENT But in Main PREFIX INCREMENT???
PLEASE EXPLAIN ?!
Richard MacCutchan 23-Apr-12 10:27am    
It does not matter which, since the resulting values are not used within a for loop.
Usman Hunjra 23-Apr-12 10:38am    
One More thing .. Can I execute the code without creating a For Loop in MAIN .. ????
Richard MacCutchan 23-Apr-12 10:44am    
Yes, of course, you can do it any way you like. I just created a loop for the sake of convenience. Judging by some of your questions here you would do well to spend some more time studying your C++ notes and guides.
Usman Hunjra 23-Apr-12 11:48am    
Thanks Richard 4 Gr8 Help ..
I would write
C++
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];
    }
}
 
Share this answer
 
v2
Comments
Usman Hunjra 23-Apr-12 10:27am    
That's Too Short ..
I want to write the code in PRECEDURE then in MAIN Just Call The FUNCTION.
Usman Hunjra 23-Apr-12 11:48am    
Nice Concept Thanks ..
Two things:

(a) in SumOfColumns your loop should run to i < 4 (not i<= 4); so:

for(i=0; i < 4; ++i)
    z[i] = p[i] + q[i];


(b) you output the return value of SumOfColumns which is always 0; so you will never see the result that you computed in vector z. So how about:

SumOfColumns (p[], q[], z[]);
for (int i = 0; i < 4; ++i)
    cout << z[i];
cout << endl;


Hope that gets it straight, finally.
 
Share this answer
 
Comments
Usman Hunjra 23-Apr-12 10:03am    
I Don't Want To Create A Loop In Main ..
I Just Want To Call The Function. :( :( ??!
Usman Hunjra 23-Apr-12 10:06am    
IS IT NECESSARY TO CREATE A LOOP IN MAIN ..???????
nv3 23-Apr-12 10:56am    
First of, there is no reason to shout at me.

No, it is not necessary to create a loop, but by far the easiest way. To avoid that loop you should:

- define a class for the return value
- overload the << operator of that class to do the formatting; in this overload you would then implement the loop over the elements of the vector.

This is going to take a lot more work and skill that the solution I gave you.
Usman Hunjra 23-Apr-12 11:47am    
nv3 Please accept my apologies if you perceive that SHOUT.
Thanks for helping me..
As I'm a beginner sO it's too hard 4 me ..
Thank You ..
As we're all doing adding arrays using a loop, here's mine:
C++
template<typename T, int N>
void sum_array( T (&p)[N], T (&q)[N], T (&z)[N] )
{
    for( unsigned n = 0; n < N; ++n )
    {
        z[n] = p[n] + q[n];
    }
}

This has got several advantages:
- It doesn't take pointers so you'll not have an excuse to run off the end of your arrays
- It won't compile if you don't feed it three arrays of equal size. This saves you having to pass a size in and makes sure that size is consistent with the size of the arrays
- It works on anything that implements operator+()

There's a couple of downsides:
- The compiler might generate more code for the safety you gain, i.e. for every size of array you get a new function. It won't be a lot of overhead though
- It won't work on arrays that you've allocated with new as they're effectively pointers. There again you shouldn't be creating heap based array though, that's what std::vector's for!

Cheers,

Ash

PS: This isn't really a solution, just hoping to spread the love for modern C++.

Edit: Bloody editor messing up me templates, I despair!
 
Share this answer
 
v2
Comments
Richard MacCutchan 23-Apr-12 12:47pm    
I'd love it a lot more if I could get the syntax locked into my brain.
Aescleal 23-Apr-12 13:08pm    
When you do it everyday it sort of gets welded into your brain. And the scary thing is that even a long period of not doing it hasn't really ejected much.
Usman Hunjra 23-Apr-12 13:05pm    
I Like It ..
True Professional Code ..!!
THANKS 4 SHARING YOUR KNOWLEDGE ..
Usman Hunjra 23-Apr-12 13:08pm    
Yes It's Difficult to understand But I LIKE IT ..
You have received a lot of answers. To be honest, I would not use the solution 5 with the template yet, it is correct yes, but it is too advanced for your actual level.

As you have seen, according to all the answers you got, there are a lot of ways to get to the same solution. Personally I would not use the solutions I am going to post.

I think this is the option that gets closer to what you have been doing on your own until now, and it doesn't use the loop in the main (as you asked several times). The way to do it is using two functions. One to do the addition, the other to print the results.

C++
#include "stdafx.h"
#include "stdafx.h"
#include <iostream>
 
using namespace::std;
 
void SumOfColumns (int p[], int q[], int z[])
{
    int i=0;
    for(i=0; i<4; i++)
    {
        z[i]=p[i] + q[i];
    }
    return;
}

void PrintResults (int z[])
{
    int i = 0;
    for (i=0; i<4; i++)
    {
        cout << "The value of z[" << i << "] is = " << z[i] << endl;
    }
    return;
}

void main()
{
    int p[4]={9,1,5,4};
    int q[4]={3,1,2,2};
    int z[4]={0,0,0,0};
    SumOfColumns(p[], q[], z[]);
    PrintResults(z[]);
}

.
.
.
.
.
Another way to do it, calling the function in the same line as cout (as you have been doing the whole time) would be like the following one, but it must have the loop in the main function (there is no other way to do it at you level):
C++
#include "stdafx.h"
#include "stdafx.h"
#include <iostream>
 
using namespace::std;
 
int SumOfColumns (int p, int q, int z)
{
    z = p + q;
    return z;
}


void main()
{
    int i=0;
    int p[4]={9,1,5,4};
    int q[4]={3,1,2,2};
    for (i=0; i<4; i++)
    {
        cout << "z[" << i << "] is = " << SumOfColumns(p[i], q[i], z[i]) << endl;
    }
}


You can choose.
Loop in main + Function call within cout
No loop in main + cout in other way
 
Share this answer
 
v5
Comments
Nelek 23-Apr-12 17:45pm    
By the way... good luck.
Usman Hunjra 24-Apr-12 4:17am    
Thanks NELEK .. You Helped Me A LOT & I Learns A LOT..
Nelek 24-Apr-12 12:17pm    
You are welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900