Help using atoi with arrays

Having a problem with this code where I have to extract a string of the format (x^4+3x^3+6)+(2x^5- 4x^3+8) from a file and add (or subtract/multiply) the two polynomials together. I've gotten to the point where I need to convert my string array of Coefficients and Exponents for each side into an int array (7x2 is what I'm working with). Having trouble using atoi with it though.

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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <iostream>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <algorithm> 
#include <string>
#include <cctype>
#include <cstring>
#include <sstream>

using namespace std;

//#define MAX = 10;

int** getPolynomial(string polynomial, int &count, string Exponent[], string Coefficient[], int polyArray[][2]){
	for (int i, i = 0; i<100; i++){
		for (int n, n = 0; n<100; n++){
			Exponent[n] = atoi(polyArray[n][1]);
		}
		Coefficient[i] = atoi(polyArray[i][0]);
	}
}

int** add(int **left, int leftCount, int **right, int countRight, int &countResult);
int** subtract(int **left, int leftCount, int **right, int countRight, int &countResult);
int** mutliply(int **left, int leftCount, int **right, int countRight, int &countResult);

int split(string polynomial, char delim, string *&newPoly){
	int wordCount = 0;
	int start = 0;
	int pos = polynomial.find(delim, start);
	newPoly = new string[20];
	while (pos != string::npos){
		newPoly[wordCount++] = polynomial.substr(start, pos - start);
		start = pos + 1;
		pos = polynomial.find(delim, start);
	}//end while
	newPoly[wordCount++] = polynomial.substr(start, polynomial.length());


	return wordCount;
}
//
//The following void function searches for all instances of subtraction within the split left/right polynomial and replaces 
//them with the addition of a negative.  This is to simplify the code for splitting the polynomial into its components
//(so as to eliminate the need to search for both addition and subtraction).
//
//This also should allow the program to easily determine and split negative coefficients
//and not erroneously convert them to positives
//
//This does not alter the ORIGINAL polynomial string, so as to keep the addition/subtraction functions separate
//
void replaceMinus(string &polynomial, string minus, string add){
	int start = 0;
	int pos = polynomial.find(minus, start);
	while (pos != string::npos){
		polynomial.replace(pos, minus.length(), add);
		start = pos + minus.length();
		pos = polynomial.find(minus, start);
	}
}

int main(){
	//ifstream in;
	//in.open("polynomial.txt");
	string thepolynomial = "(x^4+3x^2+6)+(2x^4- 3x^3+8)";
	//in >> thepolynomial;
	string *poly1;
	cout << "poly" << thepolynomial;
	int count = split(thepolynomial, ')', poly1);
	poly1[0] = poly1[0].substr(1, poly1[0].length());
	poly1[1] = poly1[1].substr(2, poly1[1].length());

	//cout << poly1[0] << endl;
	//cout << poly1[1] << endl;
	//
	//The above commands are for testing purposes

	string leftPoly = poly1[0];
	string rightPoly = poly1[1];
	replaceMinus(leftPoly, "- ", "+-");
	replaceMinus(rightPoly, "- ", "+-");
	string *poly2;
	string *poly3;
	int countLeftSecond = split(leftPoly, '+', poly2);
	int countRightSecond = split(rightPoly, '+', poly3);

	//The below section of 'cout' commands is for testing purposes.  
	//
	//cout << "leftPoly:" << poly2[0] << endl;
	//cout << poly2[1] << endl;
	//cout << poly2[2] << endl;
	//cout << "RightPoly:" << endl;
	//cout << poly3[0] << endl;
	//cout << poly3[1] << endl;
	//cout << poly3[2] << endl;
	//cout << poly3[3] << endl;

	//
	//lt# = left term #; rt# = right term #; lc# = left coefficient #; rc# = right coefficient #
	//Up to 7 terms currently coded for
	//
	string *lt0;
	string *lt1;
	string *lt2;
	string *lt3;
	string *lt4;
	string *lt5;
	string *lt6;
	string *lt7;
	string *rt0;
	string *rt1;
	string *rt2;
	string *rt3;
	string *rt4;
	string *rt5;
	string *rt6;
	string *rt7;
	string *lc0;
	string *lc1;
	string *lc2;
	string *lc3;
	string *lc4;
	string *lc5;
	string *lc6;
	string *lc7;
	string *rc0;
	string *rc1;
	string *rc2;
	string *rc3;
	string *rc4;
	string *rc5;
	string *rc6;
	string *rc7;

	//This could probably be a for/while loop
	//However, I have no idea how to go about structuring one for this purpose. 
	//
	int countLeftThird = split(poly2[0], '^', lt0);
	int countLeftFourth0 = split(lt0[0], 'x', lc0); //Splits the x from the term
	int countRightThird = split(poly3[0], '^', rt0);
	int countRightFourth0 = split(rt0[0], 'x', rc0); //Splits the x from the term
	int countLeftThird1 = split(poly2[1], '^', lt1);
	int countLeftFourth1 = split(lt1[0], 'x', lc1); //Splits the x from the term
	int countRightThird1 = split(poly3[1], '^', rt1);
	int countRightFourth1 = split(rt1[0], 'x', rc1); //Splits the x from the term
	int countLeftThird2 = split(poly2[2], '^', lt2);
	int countLeftFourth2 = split(lt2[0], 'x', lc2); //Splits the x from the term
	int countRightThird2 = split(poly3[2], '^', rt2);
	int countRightFourth2 = split(rt2[0], 'x', rc2); //Splits the x from the term
	int countLeftThird3 = split(poly2[3], '^', lt3);
	int countLeftFourth3 = split(lt3[0], 'x', lc3); //Splits the x from the term
	int countRightThird3 = split(poly3[3], '^', rt3);
	int countRightFourth3 = split(rt3[0], 'x', rc3); //Splits the x from the term
	int countLeftThird4 = split(poly2[4], '^', lt4);
	int countLeftFourth4 = split(lt4[0], 'x', lc4); //Splits the x from the term
	int countRightThird4 = split(poly3[4], '^', rt4);
	int countRightFourth4 = split(rt4[0], 'x', rc4); //Splits the x from the term
	int countLeftThird5 = split(poly2[5], '^', lt5);
	int countLeftFourth5 = split(lt5[0], 'x', lc5); //Splits the x from the term
	int countRightThird5 = split(poly3[5], '^', rt5);
	int countRightFourth5 = split(rt5[0], 'x', rc5); //Splits the x from the term
	int countLeftThird6 = split(poly2[6], '^', lt6);
	int countLeftFourth6 = split(lt6[0], 'x', lc6); //Splits the x from the term
	int countRightThird6 = split(poly3[6], '^', rt6);
	int countRightFourth6 = split(rt6[0], 'x', rc6); //Splits the x from the term
	int countLeftThird7 = split(poly2[7], '^', lt7);
	int countLeftFourth7 = split(lt7[0], 'x', lc7); //Splits the x from the term
	int countRightThird7 = split(poly3[7], '^', rt7);
	int countRightFourth7 = split(rt7[0], 'x', rc7); //Splits the x from the term

	string *leftArray[7]; //The array containing the Coefficients of the Left
	string *leftArrayEx[7]; //Array containing the Exponents of the Left
	string *rightArray[7]; //Contains Coefficients of the Right
	string *rightArrayEx[7]; //Contains Exponents of Right
	leftArray[7] = lc0;
	leftArray[6] = lc1;
	leftArray[5] = lc2;
	leftArray[4] = lc3;
	leftArray[3] = lc4;
	leftArray[2] = lc5;
	leftArray[1] = lc6;
	leftArray[0] = lc7;
	leftArrayEx[7] = lt0;
	leftArrayEx[6] = lt1;
	leftArrayEx[5] = lt2;
	leftArrayEx[4] = lt3;
	leftArrayEx[3] = lt4;
	leftArrayEx[2] = lt5;
	leftArrayEx[1] = lt6;
	leftArrayEx[0] = lt7;
	//
	//Above sets up arrays for the Coefficients and Exponents of the Left
	//Below does the same for the Right
	//Sets up four total arrays: RightCoeff, Left Coeff, Right Expon, Left Expon
	//
	rightArray[7] = rc0;
	rightArray[6] = rc1;
	rightArray[5] = rc2;
	rightArray[4] = rc3;
	rightArray[3] = rc4;
	rightArray[2] = rc5;
	rightArray[1] = rc6;
	rightArray[0] = rc7;
	rightArrayEx[7] = rt0;
	rightArrayEx[6] = rt1;
	rightArrayEx[5] = rt2;
	rightArrayEx[4] = rt3;
	rightArrayEx[3] = rt4;
	rightArrayEx[2] = rt5;
	rightArrayEx[1] = rt6;
	rightArrayEx[0] = rt7;

	//cout << "Poly and Exponent:" << endl;
	//cout << "Coeff: " << lc1[0] << endl;
	//cout << "Expon: " << lt1[1] << endl;
	//
	//The above commands are for testing purposes

	int leftPolyArray[10][2];
	int rightPolyArray[10][2];

}

Last edited on
Problem is with this section of the code:

1
2
3
4
5
6
7
8
int** getPolynomial(string polynomial, int &count, string Exponent[], string Coefficient[], int polyArray[][2]){
	for (int i, i = 0; i<100; i++){
		for (int n, n = 0; n<100; n++){
			Exponent[n] = atoi(polyArray[n][1]);
		}
		Coefficient[i] = atoi(polyArray[i][0]);
	}
}


The lines containing polyArray (Exponent[n] = atoi(polyArray[n][1]);, for example) return the error (argument of type 'int' is incompatible with parameter of type 'const char*'.)
Topic archived. No new replies allowed.