Adding Two Arrays with different sizes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <array>
#include <sstream>
#include <iomanip>

using namespace std;

//Function Prototypes
void input(int&,int&);
void printResults(int&, int&);
void close(char&);
void allocation(int*, int*, int**, int***);


// Begins Program
int main() 
{

	//Declare Local Variables
	int n, n1;
	char ch;
	int *arr = NULL, *x = NULL, **y = NULL, ***z = NULL; // Declare & Initialize pointer variables to NULL
	allocation(arr, x, y, z); // Calls function to Dynamically Allocate memory
	input(n,n1); // Calls function to Input Numbers and Validate Non-Negative Numbers
	close(ch); // Calls function to terminate program



}

//Dynamically Allocates Array and Initializes Pointers 
void allocation(int* arr, int* x, int** y,int*** z)
{
	//Declare Arrays and Pointers
	arr = new int[3];
	x = arr;
	y = &x;
	z = &y;
//	q = &z; 
	
	
}

// prompts the user for a non-negative numbers (>= 0)
// reads in the a number and checks
// keeps re-prompting user if the input is invalid (negative)
void input(int& n, int& n1)
{	
	do
	{
		cout << "Please enter a valid non-negative integer:\n";
		cout << "1.";
		cin >> n;
		cout << "2.";
		cin >> n1;
	} while (n <= 0 || n1 <= 0);
	

	//Stores User inputed integer into a string
	//Determines String Size (Digit Count)
	//Dynamically allocates (vertical arrays) based on string size
	stringstream s, ss; 
	s << n;
	ss << n1;
	int SIZE = s.str().size();
	int SIZE1 = ss.str().size();
	int *vArr;
	vArr= new int[SIZE];
	int *vArr1;
	vArr1 = new int[SIZE1];


	// For Loop to Populate Vertical Array with 1st inputed number
	for (int i = 0; i < SIZE; ++i)
	{
		vArr[i] = s.str().at(i) - '0';
		cout << vArr[i] << setw(5);
	}
	
	cout << " " << endl;

	//For Loop to Populate Vertical Array with 2nd inputed number
	for (int k = 0; k < SIZE1; ++k)
	{
		vArr1[k] = ss.str().at(k) - '0';
		cout << vArr1[k] << setw(5);
	}
	
	cout << "" << endl;

	// Allocates 3rd Array based on the larger array.
	int *vArr2;
	if (vArr[SIZE] >= vArr1[SIZE1])
	{
		vArr2 = new int[SIZE];
	} else {
		vArr2 = new int[SIZE1];
	}

	for (int i = 0; i <= SIZE; ++i)
	{
		vArr2[i] = vArr[i] + vArr1[i];
		cout << vArr2[i] << setw(5);

	}

}

// Function to terminate program if enter is pressed.
void close(char& ch) 
{
	//Program Termination

	cout << "\n Program Over \n" << endl;
	cout << "Press Enter to end -->" << endl;

	char c1, c2; // Variables to store user input
	c1 = getchar(); // get first input
	c2 = getchar(); // get second input
	if (c1 == '\n' && c2 == '\n') // if post inputs are enter
	exit(1); // exit
}

Hey guys im a little stuck on this bug and can't figure it out. I need to add thevArr[I] and vArr1[I] and store in vArr2[I] but I cant figure out how to drop the value or replace with a 0 if vArr is bigger than vArr1.
IE: 1st number: 123 2nd number: 4567

Ill get something like this:
1 2 3
4 5 6 7
5 7 9 -827349
seansabour wrote:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <array>
#include <sstream>
#include <iomanip>

using namespace std;

//Function Prototypes
void input(int&,int&);
void printResults(int&, int&);
void close(char&);
void allocation(int*, int*, int**, int***);


// Begins Program
int main() 
{

	//Declare Local Variables
	int n, n1;
	char ch;
	int *arr = NULL, *x = NULL, **y = NULL, ***z = NULL; // Declare & Initialize pointer variables to NULL
	allocation(arr, x, y, z); // Calls function to Dynamically Allocate memory
	input(n,n1); // Calls function to Input Numbers and Validate Non-Negative Numbers
	close(ch); // Calls function to terminate program



}

//Dynamically Allocates Array and Initializes Pointers 
void allocation(int* arr, int* x, int** y,int*** z)
{
	//Declare Arrays and Pointers
	arr = new int[3];
	x = arr;
	y = &x;
	z = &y;
//	q = &z; 
	
	
}

// prompts the user for a non-negative numbers (>= 0)
// reads in the a number and checks
// keeps re-prompting user if the input is invalid (negative)
void input(int& n, int& n1)
{	
	do
	{
		cout << "Please enter a valid non-negative integer:\n";
		cout << "1.";
		cin >> n;
		cout << "2.";
		cin >> n1;
	} while (n <= 0 || n1 <= 0);
	

	//Stores User inputed integer into a string
	//Determines String Size (Digit Count)
	//Dynamically allocates (vertical arrays) based on string size
	stringstream s, ss; 
	s << n;
	ss << n1;
	int SIZE = s.str().size();
	int SIZE1 = ss.str().size();
	int *vArr;
	vArr= new int[SIZE];
	int *vArr1;
	vArr1 = new int[SIZE1];


	// For Loop to Populate Vertical Array with 1st inputed number
	for (int i = 0; i < SIZE; ++i)
	{
		vArr[i] = s.str().at(i) - '0';
		cout << vArr[i] << setw(5);
	}
	
	cout << " " << endl;

	//For Loop to Populate Vertical Array with 2nd inputed number
	for (int k = 0; k < SIZE1; ++k)
	{
		vArr1[k] = ss.str().at(k) - '0';
		cout << vArr1[k] << setw(5);
	}
	
	cout << "" << endl;

	// Allocates 3rd Array based on the larger array.
	int *vArr2;
	if (vArr[SIZE] >= vArr1[SIZE1])
	{
		vArr2 = new int[SIZE];
	} else {
		vArr2 = new int[SIZE1];
	}

	for (int i = 0; i <= SIZE; ++i)
	{
		vArr2[i] = vArr[i] + vArr1[i];
		cout << vArr2[i] << setw(5);

	}

}

// Function to terminate program if enter is pressed.
void close(char& ch) 
{
	//Program Termination

	cout << "\n Program Over \n" << endl;
	cout << "Press Enter to end -->" << endl;

	char c1, c2; // Variables to store user input
	c1 = getchar(); // get first input
	c2 = getchar(); // get second input
	if (c1 == '\n' && c2 == '\n') // if post inputs are enter
	exit(1); // exit
}

Hey guys im a little stuck on this bug and can't figure it out. I need to add thevArr[I] and vArr1[I] and store in vArr2[I] but I cant figure out how to drop the value or replace with a 0 if vArr is bigger than vArr1.
IE: 1st number: 123 2nd number: 4567

Ill get something like this:
1 2 3
4 5 6 7
5 7 9 -827349 


See this post:
http://www.cplusplus.com/forum/general/100823/
Last edited on
Lets look at your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	int *vArr= new int[SIZE];
	int *vArr1 = new int[SIZE1];

	int *vArr2;
	if (vArr[SIZE] >= vArr1[SIZE1])  // Error 1
	{
		vArr2 = new int[SIZE];
	} else {
		vArr2 = new int[SIZE1];
	}

	for (int i = 0; i <= SIZE; ++i) // Error 2
	{
		vArr2[i] = vArr[i] + vArr1[i];
	}

Error 1:
You dereference pointers. You get values from memory addresses that are one-past the area allocated.
1
2
auto size2 = std::max( SIZE, SIZE1 );
int *vArr2 =  new int [size2];


Error 2:
You assign SIZE elements to vArr2 regardless of what its actual size is. You have to treat separately the range that you can add and the range that comes from one array only.


You should re-examine the rest of the code too. With careful thought.
Topic archived. No new replies allowed.