The question is to - Suppose you have a main() with three local arrays, all the same size but type (say float). The first two are already initialized to values. Write a function called addarrays() that accepts the address of three arrays as arguments add them and put their sum in the third array. The fourth argument in the function can be the size of the array. and use pointer notation through out.

include<iostream>
#include<conio.h>
using namespace std;

int num1, num2, num3; // i declares by array name and type
int*ptr1,*ptr2,*ptr3; // i delacred the type of the pointers 

void addingarray (int a, int b, int c, int size) // this is my function
{
	*ptr1=a; // i tried to assign the contents of the arrays to the variables
	*ptr2=b;
	*ptr3=c;
	
	size=sizeof(num1); // here i tried to sum the arrays
	for(int j=0;j<size;j++)
		for(int i=0;i<size;i++)
		 c[i][j]=a[i][j] + b[i][j]; // and assign the sum to third empty array
	cout<<c;
}
 int main ()
 {
	 
	 num1 [5] ={1,2,3,4,5}; // i initialized my array
	 num2 [5] ={2,5,4,6,4};
	 num3 [5];
	 *ptr1=num1; // i equated the content of the array to the pointer
	 *ptr2=num2;
	 *ptr3=num3;
	 
	 cout<< "the result is" << addingarray;
    
   getch();
	 }

For your function addingarray, try using actual pointers for parameters...

void addingarray( int* a, int* b, int* c, int size){ 

//...

}

Also you are trying to push a void function into the outputstream which I don't think the compiler will like. Omit that.

In the function addingarray, assuming you've made the above fixes, the line(s)--

*ptr1=a; // i tried to assign the contents of the arrays to the variables
	*ptr2=b;
	*ptr3=c;

--do not make sense. Omit that--

// here i tried to sum the arrays
	for(int j=0;j<size;j++)
		for(int i=0;i<size;i++)
		 c[i][j]=a[i][j] + b[i][j]; // and assign the sum to third empty array

For the sake of simplicity leave your declarations/definitions in main. Omit--

int num1, num2, num3; // i declares by array name and type
int*ptr1,*ptr2,*ptr3; // i delacred the type of the pointers

--and instead declare the types in main then assign them--

int main ()
 {
	 
	 int num1 [5] ={1,2,3,4,5}; // i initialized my array
	 int num2 [5] ={2,5,4,6,4};
	 int num3 [5];
	 int* ptr1=num1; // i equated the content of the array to the pointer
	 int* ptr2=num2;
	 int* ptr3=num3;
        
//...
}

EDIT: I just noticed you're iterating through your arrays as if they are 2Dimensional. You're only dealing with 1 row of ints so you need not iterate through array[j] but jut array--


// here i tried to sum the arrays
	for(int i=0;j<size;i++)
		 c[i]=a[i] + b[i]; // and assign the sum to third empty array

There are many other things wrong but let's start with those fixes first.

Okay I did the changes you asked me to

#include<iostream>
#include<conio.h>
using namespace std;


int addingarray (int *a, int *b, int *c, int size) // this is my function
{
	
	size=sizeof(*a); // here i tried to sum the arrays
	
	cout<<c;
}
 int main ()
 {
	 
	 int num1 [5] ={1,2,3,4,5}; // i initialized my array
	 int num2 [5] ={2,5,4,6,4};
	 int num3 [5];
	 int *ptr1=num1; // i equated the content of the array to the pointer
	 int *ptr2=num2;
	 int *ptr3=num3;
	
	 for(int i=0;i<size;i++)
		 c[i]=a[i] + b[i]; // and assign the sum to third empty array
	 
		
	 cout<< "the result is" << addingarray;
    
   getch();
	 }

leave the code --

// here i tried to sum the arrays
	for(int i=0;j<size;i++)
		 c[i]=a[i] + b[i]; // and assign the sum to third empty array

in the addingarray function because that's where the parameters size, c, a and b exist.

Omit the sizeof(*a), you don't need this because you're pasing the size to the function with the parameter 'size'--

// This is OKAY
int addingarray (int *a, int *b, int *c, int size) // this is my function
{
	 for(int i=0;i<size;i++)
		 c[i]=a[i] + b[i]; // and assign the sum to third empty array

         return 0;// not sure why you changed it to return int but make up for it here
}

After making the changes the code does work with me, it worked like this before too,
and the result it gave me was 00411258
but this is not what i want i wanted the sum of arrays which should be [3,7,8,10,9]

I'm assuming you're outputting the address of the array (maybe) and not the value of the array.

In this case print the contents of the array.

for(int i = 0; i < 5; i++)
   cout << ptr3[i] << flush;

Okay i tried doing that, I am so sorry i dont understand what is wrong with the code
now it gives me a completely different answer
it tells shows me this number 858993460

Please post your newest code

I was redoing the code and I think I have made some more changes
but here it is

#

include<iostream>
#include<conio.h>
using namespace std;


void addingarray (int *a, int *b, int *c, int size) // this is my function
{
	for(int i=0;i<size;i++)
		 c[i]=a[i] + b[i];

	for(int j=0; j<5; j++)
		 cout<<*(c+j)<<endl;
	
	
}
 int main ()
 {
	 
	 int num1 [5] ={1,2,3,4,5}; // i initialized my array
	 int num2 [5] ={2,5,4,6,4};
	 int num3 [5];
	 //int *ptr1=num1; // i equated the content of the array to the pointer
	 //int *ptr2=num2;
	 //int *ptr3=num3;	
	 
	 cout<<addingarray; 
    
   getch();
	 }

You were almost there. Try this code--

#include<iostream>
#include<conio.h>
using namespace std;


void addingarray (int *a, int *b, int *c, int size) // this is my function
{
	for(int i=0;i<size;i++)
		 c[i]=a[i] + b[i];

	for(int j=0; j<5; j++)
		 cout<<*(c+j)<<endl;
	
	
}
 int main ()
 {
	 
	 int num1 [5] ={1,2,3,4,5}; // i initialized my array
	 int num2 [5] ={2,5,4,6,4};
	 int num3 [5];
	 int *ptr1=num1; // i equated the content of the array to the pointer
	 int *ptr2=num2;
	 int *ptr3=num3;	
	 
	 addingarray(ptr1, ptr2, ptr3, 5); 
    
   getch();
	 }

Remember when you are calling a function you have to supply the function with the parameter requirements. In this case ptr1, ptr2, ptr3 and 5 were passed into the function addingarray (because they fulfilled the function parameter requirements) to be used for the calculation.

Oh thank you so much you know what I actually tried it, and it was working but I had a problem with the result it was displaying me a 0 in the end with my answer, here was the last code that i did try

#include<iostream>
#include<conio.h>
using namespace std;


int addingarray (int *a, int *b, int *c, int size) // this is my function
{
	
	for(int i=0;i<size;i++)
		 c[i]=a[i] + b[i];
    
	for(int j=0; j<5; j++)
     cout<<*(c+j)<< endl;
 return 0;
}
 int main ()
 {
	 
	 int num1 [5] ={1,2,3,4,5}; // i initialized my array
	  int num2 [5] ={2,5,4,6,4};
	  int num3 [5];
	 
	int sum= addingarray(num1,num2,num3,5);
	cout<<sum<< endl;
    
   getch();
	 }

Anyways I am really glad, Thanks a MILLION !!!!!!!!!

That's because your function returns 0 and when you say cout << sum << endl, sum was passed 0 from the function and then it was pushed to the output stream.

Your function doesn't really need to return anything. You can make it void and simply call the function. There is no need to cout << sum << endl;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.