Now this is rough fix for ur program
#include <stdio.h>
#include <iostream>
using namespace std;
unsigned i;
int main(){
/* Array`s inputs */
int *A = new int[3];
for( i = 3 ; i-- ; ){
cout << "Insert number for [A]: " << endl;
scanf("%d",&A[i]);
}
printf("\n");
int* B = new int[3];
for( i = 3; i-- ; ){
cout << "Insert number for [B]: " << endl;
scanf("%d",&B[i]);
}
int* C = new int[3];
// Pointers
int *punt_A, *punt_B, *punt_C;
punt_A = A;
punt_B = B;
punt_C = C;
// Addition
for( i = 3; i--; ){
*punt_C = *punt_A + *punt_B;
punt_A++;
punt_B++;
punt_C++;
}
for(int i = 0; i < 3; i++)
{
cout << *C << endl;
C++;
}
delete [] A;
delete [] B;
delete [] C;
return 0;
}
The problem was:
You initialize an array of shorts. Size of short is 2 bytes. However, you are loading number, expecting integer %d wich is 4 bytes. So, you load in the 2 bytes, and the other two bytes just ommited.
I compile it WITHOUT -Wall -pedantic flags and still get warning:
main.cpp: In function ‘int main()’:
main.cpp:17:21: warning: format ‘%d’ expects argument of type ‘int*’, but argument 2 has type ‘short int*’ [-Wformat=]
scanf("%d",&A[i]);
^
main.cpp:28:21: warning: format ‘%d’ expects argument of type ‘int*’, but argument 2 has type ‘short int*’ [-Wformat=]
scanf("%d",&B[i]);
Now to fix this, make them all ints. But you asked for better solution.
If you are given two arrays, and you have to sum them, and since you are using c++11, for you, it might be easier to use a container.
# include <stdio.h>
# include <iostream>
#include <vector>
#define COUNT 3
using namespace std;
unsigned i;
int main()
{
/* Array`s inputs */
vector<int> A, B;
int worker;
for( i = COUNT ; i-- ; )
{
cout << "Insert number for [A]: " << endl;
cin >> worker;
A.push_back(worker);
}
for( i = COUNT ; i-- ; )
{
cout << "Insert number for [B]: " << endl;
cin >> worker;
B.push_back(worker);
}
for(int i = 0; i < COUNT; i++)
{
A[i] = A[i] + B[i];
}
cout << "summed up" << endl;
for(vector<int>::iterator it = A.begin(); it != A.end(); it++)
{
cout << *it << endl;
}
return 0;
}
Now notice, i made a define for your count. Moreover, I used aray A again for output, saving space. The problem is, that vector by itself has O(n) complexity for push_back() (unless you resrve the size, but that requires knowing exact number of inputs, wich might not be in future a case, you might load until EOF). To eliminate this and make the program faster, a list might be used for better performance.
But if you WANT to use pointers, use iterators. :]
(please notice that my code is not protected for fails, meaning that if you put in a letter, it will do strange thing, but i outlined the idea :]