• 0

matrix program error


Question

I am having problems with figure out the problems with MATRIX program...i am havin few errors that i cant fix in the main function where : print_matrix

#include<iostream>

#include<fstream>

#include<string>

#include<iomanip>

#include<cmath>



using namespace std;



struct MATRIX {

	   double a11, a12,

			  a21, a22;

			  };



void file_open(ofstream &Out);

int file_close(ofstream &Out);



void print_matrix(MATRIX m, char word[], ofstream &out);

void get_matrix(MATRIX &matrix, char name[]);

void get_scalar(double &k);

void calc_sum(MATRIX m1, MATRIX m2, MATRIX ∑);

void calc_diff(MATRIX m1, MATRIX m2, MATRIX &diff);

void scalar_mult(double k, MATRIX m, MATRIX &km);

void calc_prod (MATRIX m1, MATRIX m2, MATRIX ∏);

void calc_inverse(MATRIX m, MATRIX &inverse);



int main()

{



MATRIX matrix1, matrix2, sum, difference, product, sProduct,inverse, sMult;

double k=4.5;

char name1[20], name2[20];



ofstream outfile;



	file_open(outfile);



get_matrix(matrix1, name1);

get_matrix(matrix2, name2);

get_scalar(k);   



calc_sum(matrix1, matrix2, sum);	

calc_diff(matrix1,matrix2,difference);	

scalar_mult (k, matrix1, sMult);

calc_prod(matrix1, matrix2, product);  

calc_inverse(matrix1, inverse);



	print_matrix(matrix1, name1,outfile);

	print_matrix(matrix2, name2,outfile);

	print_matrix(outfile,  "Sum =",sum);

	print_matrix(outfile, "Difference = ", difference);

	print_matrix(outfile, "Scalar Product = ", sMult);

	print_matrix(outfile, "Matrix Product = ", product);

	print_matrix(outfile, "Inverse  = ", inverse);









file_close(outfile);

return 0;

}





void get_matrix(MATRIX &matrix, char name[])

{

	 cout<<"Enter the name of the Matrix :";

	 cin>>ws;

	 cin.getline(name,20);

	 cout<<"Enter m1: ";

	 cin>>matrix.a11;

	 cout<<"Enter m2: ";

	 cin>>matrix.a12;

	 cout << "Enter m3: ";

	 cin >> matrix.a21;

	 cout<<"Enter m4: ";

	 cin>>matrix.a22;

}

void get_scalar(double &k)

{

	 cout<<endl<<"Enter scalar value: ";

	 cin>>k;

}	 



void calc_sum(MATRIX m1, MATRIX m2, MATRIX ∑)

{

	 sum.a11 = m1.a11 + m2.a11;

	 sum.a12 = m1.a12 + m2.a12;

	 sum.a21 = m1.a21 + m2.a21;

	 sum.a22 = m1.a22 + m2.a22;

}

void calc_diff(MATRIX m1, MATRIX m2, MATRIX &diff)

{

	 diff.a11 = m1.a11 - m2.a11;

	 diff.a12 = m1.a12 - m2.a12;

	 diff.a21 = m1.a21 - m2.a21;

	 diff.a22 = m1.a22 - m2.a22;

}

void scalar_mult(double k, MATRIX m, MATRIX &km)

{

	 km.a11 = m.a11 * k;

	 km.a12 = m.a12 * k;

	 km.a21 = m.a21 * k;

	 km.a22 = m.a11 * k;

}

void calc_prod (MATRIX m1, MATRIX m2, MATRIX ∏)

{

	 prod.a11= m1.a11 * m2.a11 + m1.a12 * m2.a21;

	 prod.a12= m1.a11 * m2.a11 + m1.a12 * m2.a21;

	 prod.a21= m1.a11 * m2.a11 + m1.a12 * m2.a21;

	 prod.a22= m1.a11 * m2.a11 + m1.a12 * m2.a21;

}



void calc_inverse(MATRIX m, MATRIX &inverse)

{

	 double det = m.a11 * m.a22 - m.a21 * m.a12;



	 inverse.a11 = m.a22/det;

	 inverse.a12 = -(m.a12/det);

	 inverse.a21 = -(m.a21/det);

	 inverse.a22 =  m.a11/det;



} 



void print_matrix(MATRIX m, char word[], ofstream &out)

{

	 out << word << " = " << matrix.a11 << "  " << matrix.a12 << "\n"

			<< "	"		<< matrix.a21 << "  " << matrix.a22 << "\n";



}



void file_open(ofstream &Out)

{

	int choice;



	char drive[3]; 

	char disk_f[20]; 

	char file[15];



	cout<< "Output to console [1] to disk file [2]:  ";

	cin >> choice;

	if(choice == 1)

			{

				Out.open("con");



				}

	else

	{

		cout<< "Which drive would you want to save your data?";



		cin >> drive;

		strcpy(disk_f, drive);

		strcat(disk_f, ":");

		cout<< "Please enter result file name: ";

		cin >>file;

		strcat(disk_f, file);

		strcat(disk_f, ".dta");

		Out.open(disk_f);

		}



	Out<<setiosflags(ios::showpoint|ios::fixed)<<setprecision(2);



	}



int file_close(ofstream &Out)

	{

	Out.close();

	return 0;

		}

print_matrix(matrix1, name1,outfile);

print_matrix(matrix2, name2,outfile);

print_matrix(outfile, "Sum =",sum);

print_matrix(outfile, "Difference = ", difference);

print_matrix(outfile, "Scalar Product = ", sMult);

print_matrix(outfile, "Matrix Product = ", product);

print_matrix(outfile, "Inverse = ", inverse);

This is where i am gettin errors my compiler tells me error conversion from std::ofstream to non-scalar type 'MATRIX' requested

also in line 131 in the void print_matrix error 'matrix' was not declered in this scope

and I am always gettin this errors cause I am using geany {linux} compiler where inside the function file_open inside the if statement 'strcpy' was not declered in this scope..

Thanks for help :)

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

Quick run through of the code:

void print_matrix(MATRIX m, char word[], ofstream &out)

{

	 out << word << " = " << matrix.a11 << "  " << matrix.a12 << "\n"

			<< "	"		<< matrix.a21 << "  " << matrix.a22 << "\n";



}

matrix should be m, that should take away the undeclared errors

Secondly, the parameters to your print_matrix are the wrong way around. Should look like this:

	print_matrix(matrix1, name1,outfile);

	print_matrix(matrix2, name2,outfile);

	print_matrix(sum,  "Sum =", outfile);

	print_matrix(difference, "Difference = ", outfile);

	print_matrix(sMult, "Scalar Product = ", outfile);

	print_matrix(product, "Matrix Product = ", outfile);

	print_matrix(inverse, "Inverse  = ", outfile);

The code now compiles for me :)

Link to comment
Share on other sites

  • 0

I am still gettin the warning when I compile in my program...I am using Geany{linux} compiler

matrix.cpp:57: warning: deprecated conversion from string constant to ?char*?

 print_matrix(sum, "Sum =", outfile);



	print_matrix(difference, "Difference = ", outfile);



	print_matrix(sMult, "Scalar Product = ", outfile);



	print_matrix(product, "Matrix Product = ", outfile);



	print_matrix(inverse, "Inverse  = ", outfile);

Link to comment
Share on other sites

  • 0

I fixed all the errors but when my program executed its ask me for all the input then i put all the information then it disappires ....its not givin me the output

Link to comment
Share on other sites

  • 0
I fixed all the errors but when my program executed its ask me for all the input then i put all the information then it disappires ....its not givin me the output

You are missing

[b]getch();[/b]


in your main() method

Link to comment
Share on other sites

  • 0
I fixed all the errors but when my program executed its ask me for all the input then i put all the information then it disappires ....its not givin me the output

I noticed that when I ran it yesterday. I'll try and have another look at it for you.

edit: Yep, it only needs some way to stop the console screen closing.

Add:

char g;
   cin >> g;

to the end of the code which will prompt the user to enter a key before exiting the program.

It now works. Here's the screen I get:

post-91323-1234612364_thumb.jpg

Edited by ViZioN
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.