hello again.
i am having a problem in trying to find a way to add 2 matrices i have created using vectors of type struct.
in my following code i have sucessfully entered the elements of both matrices in
std::vector<matrix_1> data_of_M1;
std::vector<matrix_2> data_of_M2;
i can add elements that have the same row and coloum position.
ie.// TIME TO COMPARE STRUCTURES.
cout<<"ADDED MATRIX"<< endl;
but the elements dat were not added, are NOT being pushback into the new added matrix correctly.
Am i doing something wrong? is there anyway to step both structures based on column position and then row position.
here is my code. if helps.
i didn't define "split" function in here, because it is not my function, but it works fine.
THANKS.
#include<iostream>
#include<vector>
#include<string>
#include <stdio.h>
#include <stdlib.h>
#include<algorithm>
#include<fstream>
#include<cmath>
#include<iterator>
#include<iomanip>
#include<sstream>
//#include<cmath.h>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using namespace std ;
using std::ifstream;
using std::vector ;
struct matrix_1 {
int number_1;
int row_1;
int col_1;
} values_1;
struct matrix_2 {
int number_2;
int row_2;
int col_2;
}values_2;
struct matrix_added {
int number_added;
int row_added;
int col_added;
}values_added;
int main(){
ifstream inputfile("matrix.txt");
string t ;
int gh = 1 ;
std::vector<int> row_count ;
std::vector<matrix_1> data_of_M1;
std::vector<matrix_2> data_of_M2;
std::vector<matrix_added> data_of_added ;
int col_count = 0 ;
std::vector<int> words ;
while (getline(inputfile, t)){;
if(t[0]!= ispunct('\n')){ //inputs elements in Matrix 1
words = split(t , row_count) ; // split was earlier //defined
for (int h = 0 ; h< row_count.size() ; h++){
values_1.col_1 = col_count;
values_1.number_1 = words[h] ;
values_1.row_1 = row_count[h] ;
data_of_M1.push_back(values_1);
}
col_count++ ;
row_count.clear();
}
else if (t[0]== ispunct('\n')) {inputs elements in Matrix 2
col_count = 0;
row_count.clear();
words.clear();
while (getline(inputfile, t)){
words = split(t , row_count) ;
for (int h = 0 ; h< row_count.size() ; h++){
values_2.col_2 = col_count;
values_2.number_2 = words[h] ;
values_2.row_2 = row_count[h] ;
data_of_M2.push_back(values_2);
}
col_count++ ;
row_count.clear();
}}}
cout<<"MATRIX 1"<<endl; // outputs elements of matrix 1
for (int rt = 0 ; rt< data_of_M1.size() ; rt++){
cout<<"("<<data_of_M1[rt].number_1<<", "<<data_of_M1[rt].col_1<<", "<<data_of_M1[rt].row_1<<") "<<endl ;
}
cout<<endl<<endl;
cout<<"MATRIX 2"<<endl; // outputs elements of matrix 2
for (int rt = 0 ; rt< data_of_M2.size() ; rt++){
cout<<"("<<data_of_M2[rt].number_2<<", "<<data_of_M2[rt].col_2<<", "<<data_of_M2[rt].row_2<<") "<<endl ;
}
cout<<endl<<endl;
// TIME TO COMPARE STRUCTURES.
cout<<"ADDED MATRIX"<< endl;
/*for( int rt = 0 ; rt< data_of_M1.size(); rt++){
for(int yu = 0; yu< data_of_M2.size() ; yu++){
if (data_of_M1[rt].col_1 == data_of_M2[yu].col_2 && data_of_M1[rt].row_1 == data_of_M2[yu].row_2){
int added = 0 ;
added = data_of_M1[rt].number_1 + data_of_M2[yu].number_2 ;
values_added.col_added = data_of_M1[rt].col_1;
values_added.number_added = added ;
values_added.row_added = data_of_M2[yu].row_2 ;
data_of_added.push_back(values_added);
}
else if (data_of_M1[rt].row_1 == data_of_M2[yu].row_2 ){
values_added.col_added = data_of_M2[yu].col_2;
values_added.number_added = data_of_M2[yu].number_2 ;
values_added.row_added = data_of_M2[yu].row_2 ;
data_of_added.push_back(values_added);
}}
}
*/
/*
// PRINTING out added matrix
for (int rt = 0 ; rt< data_of_added.size() ; rt++){
cout<<"( "<<data_of_added[rt].number_added<<", "<<data_of_added[rt].col_added<<", "<<data_of_added[rt].row_added<<" )"<< endl;
}*/
return 0;
}
i am having a problem in trying to find a way to add 2 matrices i have created using vectors of type struct.
in my following code i have sucessfully entered the elements of both matrices in
std::vector<matrix_1> data_of_M1;
std::vector<matrix_2> data_of_M2;
i can add elements that have the same row and coloum position.
ie.// TIME TO COMPARE STRUCTURES.
cout<<"ADDED MATRIX"<< endl;
but the elements dat were not added, are NOT being pushback into the new added matrix correctly.
Am i doing something wrong? is there anyway to step both structures based on column position and then row position.
here is my code. if helps.
i didn't define "split" function in here, because it is not my function, but it works fine.
THANKS.
#include<iostream>
#include<vector>
#include<string>
#include <stdio.h>
#include <stdlib.h>
#include<algorithm>
#include<fstream>
#include<cmath>
#include<iterator>
#include<iomanip>
#include<sstream>
//#include<cmath.h>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using namespace std ;
using std::ifstream;
using std::vector ;
struct matrix_1 {
int number_1;
int row_1;
int col_1;
} values_1;
struct matrix_2 {
int number_2;
int row_2;
int col_2;
}values_2;
struct matrix_added {
int number_added;
int row_added;
int col_added;
}values_added;
int main(){
ifstream inputfile("matrix.txt");
string t ;
int gh = 1 ;
std::vector<int> row_count ;
std::vector<matrix_1> data_of_M1;
std::vector<matrix_2> data_of_M2;
std::vector<matrix_added> data_of_added ;
int col_count = 0 ;
std::vector<int> words ;
while (getline(inputfile, t)){;
if(t[0]!= ispunct('\n')){ //inputs elements in Matrix 1
words = split(t , row_count) ; // split was earlier //defined
for (int h = 0 ; h< row_count.size() ; h++){
values_1.col_1 = col_count;
values_1.number_1 = words[h] ;
values_1.row_1 = row_count[h] ;
data_of_M1.push_back(values_1);
}
col_count++ ;
row_count.clear();
}
else if (t[0]== ispunct('\n')) {inputs elements in Matrix 2
col_count = 0;
row_count.clear();
words.clear();
while (getline(inputfile, t)){
words = split(t , row_count) ;
for (int h = 0 ; h< row_count.size() ; h++){
values_2.col_2 = col_count;
values_2.number_2 = words[h] ;
values_2.row_2 = row_count[h] ;
data_of_M2.push_back(values_2);
}
col_count++ ;
row_count.clear();
}}}
cout<<"MATRIX 1"<<endl; // outputs elements of matrix 1
for (int rt = 0 ; rt< data_of_M1.size() ; rt++){
cout<<"("<<data_of_M1[rt].number_1<<", "<<data_of_M1[rt].col_1<<", "<<data_of_M1[rt].row_1<<") "<<endl ;
}
cout<<endl<<endl;
cout<<"MATRIX 2"<<endl; // outputs elements of matrix 2
for (int rt = 0 ; rt< data_of_M2.size() ; rt++){
cout<<"("<<data_of_M2[rt].number_2<<", "<<data_of_M2[rt].col_2<<", "<<data_of_M2[rt].row_2<<") "<<endl ;
}
cout<<endl<<endl;
// TIME TO COMPARE STRUCTURES.
cout<<"ADDED MATRIX"<< endl;
/*for( int rt = 0 ; rt< data_of_M1.size(); rt++){
for(int yu = 0; yu< data_of_M2.size() ; yu++){
if (data_of_M1[rt].col_1 == data_of_M2[yu].col_2 && data_of_M1[rt].row_1 == data_of_M2[yu].row_2){
int added = 0 ;
added = data_of_M1[rt].number_1 + data_of_M2[yu].number_2 ;
values_added.col_added = data_of_M1[rt].col_1;
values_added.number_added = added ;
values_added.row_added = data_of_M2[yu].row_2 ;
data_of_added.push_back(values_added);
}
else if (data_of_M1[rt].row_1 == data_of_M2[yu].row_2 ){
values_added.col_added = data_of_M2[yu].col_2;
values_added.number_added = data_of_M2[yu].number_2 ;
values_added.row_added = data_of_M2[yu].row_2 ;
data_of_added.push_back(values_added);
}}
}
*/
/*
// PRINTING out added matrix
for (int rt = 0 ; rt< data_of_added.size() ; rt++){
cout<<"( "<<data_of_added[rt].number_added<<", "<<data_of_added[rt].col_added<<", "<<data_of_added[rt].row_added<<" )"<< endl;
}*/
return 0;
}