samedi 11 juin 2016

Getting a Runtime Error on online jugde(TCS)


I am getting a Run Time Error for the following code of a matrix multiplicaton by the online judge of Tata Consultancy Servive's channel "Anyone Can Code" The code runs perfectly on my system. Here's the code:

#include<iostream>
#include<string>
#include<vector>
#include<sstream>
#include<fstream>
#include<stdlib.h>
#include<iomanip>
using namespace std;



vector<vector<double> > readmatrix(char file[200])
{
vector<vector<double> > temp;
string str;
double a;
int i=0;
ifstream fin;
fin.open(file);
while(fin.good())
{
    getline(fin,str);
    istringstream sstr(str);

    temp.push_back(vector<double>());
    while(sstr.good())
    {
        if(sstr>>a)
        {
            temp[i].push_back(a);

        }

        else
        {

            cout<<"INVALID INPUT";
            exit(0);
        }

    }
    i++;
}
fin.close();

return temp;
}



void printmatrix(vector<vector<double> >&temp)
{

for(size_t i=0; i<temp.size(); i++)
{
    for(size_t j=0; j<temp[0].size(); j++)
    {
        cout<<fixed<<setprecision(3)<<temp[i][j]<<" ";

    }


    cout<<endl;
}

}

void mul(vector<vector<double> >&vec1,vector<vector<double> >&vec2)
{
vector<vector<double> > temp;
float sum=0;

if((vec1[0].size())==(vec2.size()) )
{
    for(size_t i=0; i<vec1.size(); i++)
    {

        temp.push_back(vector<double>());

        for(size_t j=0; j<vec2[0].size(); j++)
        {
            sum=0;

            for(size_t k=0; k<vec2.size(); k++)
            {
                sum+=vec1[i][k]*vec2[k][j];
            }

            temp[i].push_back(sum);

        }

    }

    printmatrix(temp);


}


else
{

    cout<<"Cannot multiply matrix of dimensions "<<vec1.size()<<"x"   <<vec1[0].size()
        <<" with "<<vec2.size()<<"x"<<vec2[0].size();


}
}

int main()
{
string str;
vector<vector<double> > v1,v2;
char file1[200],file2[200];
cin>>file1;
cin>>file2;

v1=readmatrix(file1);
v2=readmatrix(file2);



mul(v1,v2);

return 0;
}

I have used stls in my code.I have tried a lot and checked my code many times but couldnt succeed.Please Help!!


Aucun commentaire:

Enregistrer un commentaire