I am using C++. I have a file with three equations involving x, y, and z. The fi
ID: 3873423 • Letter: I
Question
I am using C++.
I have a file with three equations involving x, y, and z.
The file looks something like this:
2.95x+3y-1z=9
5x-0y+.3z=4.2
-1.7z+0y+4x=5
Notice how some of the coefficients/answers are floating point (decimal) numbers? I need to grab all numbers from these equations and store them into an array using pointers. So in the case provided above my array would have 12 numbers total. May I please have some help? For input/output, please use "cout <<" and "cin>>" if needed. No scanf or printf.
I'll be performing row operations on these equations, and since the variables don't have to be in order, it would also be awesome if you could give me advice about that. I have no idea how to handle it.
Explanation / Answer
#include<iostream>
#include<string>
#include<fstream>
#include<stdlib.h>
using namespace std;
int main(){
string line;
ifstream fin("input35.txt");
double a[3][4];
char const *p;
if (fin){
int count = 0;
while (getline(fin,line)){
string str = "";
int j = 0;
line = line + ' ';
p = line.c_str();
for (int k = 0; k<line.size(); k++){
if ((*(p+k) >= '0' && *(p+k) <= '9') || *(p+k) == '.' || *(p+k) == '-'){
str = str + *(p+k);
}
else {
if (str != ""){
a[count][j] = atof(str.c_str());
j++;
str = "";
}
}
}
count++;
}
for (int i = 0; i<3; i++){
for( int j=0; j<4; j++){
cout << a[i][j] << " ";
}
cout << endl;
}
}
else {
cout << "Error in opening file ";
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.