ArtifeX Opublikowano 10 Lutego 2005 Zgłoś Opublikowano 10 Lutego 2005 Aż sie boje napisać tutaj ale naprawde nie mialm kontaktu z macierzami a czytam i nic nie rozumiem. Jesli mozecie pomozcie w rozwiazaniu nastepujcego probalmu: Napisac program do obliczania iloczynu dwoch macierzy A[m][p] i B[p][n], Funkcja 1: Mnozenie wiersza przez kolumne, Funkcja 2: Mnozenie wszystkich wierszy przez jedna kolumne, #include<stdio.h>#include<math.h>#include<string.h>int main(void){unsigned int A[m][p];unsigned int B[p][n];int k,w; //k-kolumna, w-wiersz NIe postaralem sie wiem, coz prosze Was o pomoc. Naprowadzcie mnie. Cytuj Udostępnij tę odpowiedź Odnośnik do odpowiedzi Udostępnij na innych stronach Więcej opcji udostępniania...
_Borys Opublikowano 11 Lutego 2005 Zgłoś Opublikowano 11 Lutego 2005 Niech strace : #include <iostream>#include <iomanip> using namespace std;// declarin classclass Matrix{ // public functions public: // these functions are in charge of getting matrix dimensions from the user bool Input_matrix_1st_dimensions (); bool Input_matrix_2nd_dimensions (); // this checks if the dimensions allow the matrixes to be multiplied bool Check_2nd_matrix_dimensions (); // these functions get matrix dimansions from private values int Get_number_of_rows_m1 (); int Get_number_of_columns_m1 (); int Get_number_of_rows_m2 (); int Get_number_of_columns_m2 (); int Get_number_of_rows_m3 (); int Get_number_of_columns_m3 (); // these functions let the matrix values to be inputed bool Input_values_1st_matrix (int &Number_of_rows_m1, int &Number_of_columns_m1); bool Input_values_2nd_matrix (int &Number_of_rows_m2, int &Number_of_columns_m2); // these funcyions display first and second matrix void Display_1st_martrix ( int &Number_of_rows_m1, int &Number_of_columns_m1 ); void Display_2nd_martrix ( int &Number_of_rows_m2, int &Number_of_columns_m2 ); // this function creates third matrix by multiplying first and second matrix void Multiply_Matrixes (int &Number_of_rows_m3, int &Number_of_columns_m3, int &Number_of_columns_m1); // this function displays third matrix void Display_3rd_martrix ( int &Number_of_columns_m3, int &Number_of_rows_m3 ); // this function sets third matrix dimaensions void Set_matrix_3_dimensions (); // private values for private: // Matrix 1: // number of rows int Number_of_rows_m1; // number of columns int Number_of_columns_m1; // Matrix 2: // number of rows int Number_of_rows_m2; // number of columnss int Number_of_columns_m2; // Matrix 3: // number of rows int Number_of_rows_m3; // number of columns int Number_of_columns_m3; // this intializes the maximum matrixes that can be used int Matrix_1 [10] [10]; int Matrix_2 [10] [10]; int Matrix_3 [10] [10];};int main (){char loop_control = 'y';int return_value = 0;// declaring objectMatrix M;// begining the first loopwhile ( loop_control == 'y'){ // error checking if the input data is wrong the function returns falseif ( M.Input_matrix_1st_dimensions () == true ) { // error checking if the input data is wrong the function returns false if ( M.Input_matrix_2nd_dimensions () == true && M.Check_2nd_matrix_dimensions () == true ) { // declaring local variables used in int main function int number_of_rows_m1 = 0; int number_of_columns_m1 = 0; int number_of_rows_m2 = 0; int number_of_columns_m2 = 0; int number_of_rows_m3 = 0; int number_of_columns_m3 = 0; // setting the variables by calling functions that get data from class Matrix private values number_of_rows_m1 = M.Get_number_of_rows_m1 (); number_of_columns_m1 = M.Get_number_of_columns_m1 (); number_of_rows_m2 = M.Get_number_of_rows_m2 (); number_of_columns_m2 = M.Get_number_of_columns_m2 (); M.Set_matrix_3_dimensions (); number_of_rows_m3 = M.Get_number_of_rows_m3 (); number_of_columns_m3 = M.Get_number_of_columns_m3 (); // error checking if the input data is wrong the function returns false if ( M.Input_values_1st_matrix ( number_of_rows_m1, number_of_columns_m1) == true ) { // error checking if the input data is wrong the function returns false if ( M.Input_values_2nd_matrix (number_of_rows_m2, number_of_columns_m2) == true ) { // calling functions displaing matrixes 1 and 2 M.Display_1st_martrix ( number_of_rows_m1, number_of_columns_m1 ); M.Display_2nd_martrix ( number_of_rows_m2 ,number_of_columns_m2 ); // calling functions multiplying matrixes 1 and 2 M.Multiply_Matrixes (number_of_rows_m3, number_of_columns_m3, number_of_columns_m1); // calling functions displaing matrix 3 M.Display_3rd_martrix (number_of_rows_m3, number_of_columns_m3); cout << endl; // this asks user if he wants to continue cout << " Do you want to continue ? Press y if so or no if not " << endl; cin >> loop_control; if ( cin.fail() != 0 ) { loop_control = 'n'; return_value = 1; cerr << " ERROR! Could not read the continue input ! " << endl; } } else { loop_control = 'n'; return_value = 1; } } else { loop_control = 'n'; return_value = 1; } } else { loop_control = 'n'; return_value = 1; } }else { loop_control = 'n'; return_value = 1; }}cout << "Bye!Bye! " << endl;return return_value;} // this gets data from user if data correct returns true if not falsebool Matrix::Input_matrix_1st_dimensions (){ bool rv = true; int number_of_rows = 0; int number_of_columns = 0; cout << endl; cout << " Enter the number of rows for the first matrix" << endl; cin >> number_of_rows; if (cin.fail () == 0 and number_of_rows >= 1 and number_of_rows <= 10) { cout << endl; cout << " Enter the number of columns for the first matrix " << endl; cin >> number_of_columns; if ( cin.fail() ==0 and number_of_columns >= 1 and number_of_columns <= 10) { Number_of_rows_m1 = number_of_rows; Number_of_columns_m1 = number_of_columns; } else { cerr << " Error!! Could not read the number of columns input " << endl; cerr << " Remember the number of columns must be an inteager grater or equal 1 and less than 10" << endl; rv = false; } } else { cerr << " Error!! Could not read the number of rows input " << endl; cerr << " Remember the number of columns must be an inteager grater or equal 1 and less rhan 10 " << endl; rv = false; }return rv;cout << endl;}// this gets data from user if data correct returns true if not falsebool Matrix::Input_matrix_2nd_dimensions (){ bool rv = true; int number_of_rows = 0; int number_of_columns = 0; cout << endl; cout << " Enter the number of rows for the second matrix" << endl; cin >> number_of_rows; if (cin.fail () == 0 and number_of_rows >= 1 and number_of_rows <= 10) { cout << endl; cout << " Enter the number of columns for the second matrix " << endl; cin >> number_of_columns; if ( cin.fail() ==0 and number_of_columns >= 1 and number_of_columns <= 10) { Number_of_rows_m2 = number_of_rows; Number_of_columns_m2 = number_of_columns; } else { cerr << " Error!! Could not read the number of columns input " << endl; cerr << " Remember the number of columns must be an inteager grater or equal 1 and less than 10" << endl; rv = false; } } else { cerr << " Error!! Could not read the number of rows input " << endl; cerr << " Remember the number of columns must be an inteager grater or equal 1 and less than 22" << endl; rv = false; } return rv;}// this gets data from user if data correct returns true if not falsebool Matrix::Check_2nd_matrix_dimensions (){ bool rv = true; if ( Matrix::Number_of_columns_m1 == Matrix::Number_of_rows_m2 ) { } else { cerr << " Wrong number of rows input " << endl; cerr << " To multiply two matrixes the number of columns in first matrix must be equal to " << endl; cerr << " the number of rows in second matrix " << endl; rv = false; }return rv;} int Matrix::Get_number_of_rows_m1 (){return Number_of_rows_m1;}int Matrix::Get_number_of_columns_m1 (){return Number_of_columns_m1;}int Matrix::Get_number_of_rows_m2 (){return Number_of_rows_m2;}int Matrix::Get_number_of_columns_m2 (){return Number_of_columns_m2;}int Matrix::Get_number_of_rows_m3 (){return Number_of_rows_m3;}int Matrix::Get_number_of_columns_m3 (){return Number_of_columns_m3;}// this gets data from user if data correct returns true if not falsebool Matrix::Input_values_1st_matrix (int &Number_of_rows_m1, int &Number_of_columns_m1){ bool rv = true; cout << endl; cout << endl; cout << " Enter the values for first Matrix " << endl; cout << endl; int i; int j; for ( int i = 0; ( (i < Number_of_rows_m1) && (cin.fail () ==0)); i ++ ) { for (j = 0; ((j < Number_of_columns_m1) && ( cin.fail() == 0)); j ++ ) { cout << " Enter the value for the cell in row " << i+1 << " column " << j+1 << endl; cin >> Matrix_1 [i][j]; } } if ( cin.fail () != 0 ) { cout << " ERROR! Wrong input data !" << endl; rv = false; } return rv;}// this gets data from user if data correct returns true if not falsebool Matrix::Input_values_2nd_matrix (int &Number_of_rows_m2, int &Number_of_columns_m2){ bool rv = true; cout << endl; cout << endl; cout << " Enter the values for second Matrix " << endl; cout << endl; int i; int j; for (i = 0; i < Number_of_rows_m2; i ++ ) { for ( j = 0;(( j < Number_of_columns_m2) && (cin.fail () == 0)); j ++ ) { cout << " Enter the value for the cell in row " << i+1 << " column " << j+1 << endl; cin >> Matrix_2 [i][j]; } } if ( cin.fail () != 0 ) { cout << " ERROR! Wrong input data !" << endl; rv = false; } cout << endl;return rv;}void Matrix::Display_1st_martrix ( int &Number_of_rows_m1, int &Number_of_columns_m1 ){ cout << " 1st Matrix: " << endl; cout << endl; for (int i = 0; i < Number_of_rows_m1; i ++ ) { cout << " | "; for (int j = 0; j < Number_of_columns_m1; j ++ ) { cout << setw (5) << Matrix_1 [i][j] << " | "; } cout << endl; }cout << endl;}void Matrix::Display_2nd_martrix ( int &Number_of_rows_m2, int &Number_of_columns_m2 ){ cout << " 2nd Matrix : " << endl; cout << endl; for (int i = 0; i < Number_of_rows_m2; i ++ ) { cout << " | "; for (int j = 0; j < Number_of_columns_m2; j ++ ) { cout << setw (5) << Matrix_2 [i][j] << " | "; } cout << endl; } cout << endl;}void Matrix::Set_matrix_3_dimensions (){ Matrix::Number_of_rows_m3 = Matrix::Number_of_rows_m1; Matrix::Number_of_columns_m3 = Matrix::Number_of_columns_m2;}// this multiply matrixes 1 and 2 void Matrix::Multiply_Matrixes (int &Number_of_rows_m3, int &Number_of_columns_m3, int &Number_of_columns_m1){ for (int i = 0; i < Number_of_rows_m3; i++) { for (int j = 0; j < Number_of_columns_m2; j++) { int sum = 0; for (int k = 0; k < Number_of_columns_m1; k++) { sum = sum + ( Matrix_1[i][k] * Matrix_2[k][j]); Matrix_3[i][j] = sum; } } }} void Matrix::Display_3rd_martrix ( int &Number_of_rows_m3, int &Number_of_columns_m3 ){ cout << " 3rd Matrix = 1st Matrix * 2nd Matrix : " << endl; cout << endl; for (int i = 0; i < Number_of_rows_m3; i ++ ) { cout << " | "; for (int j = 0; j < Number_of_columns_m3; j ++ ) { cout << setw (5) << Matrix_3 [i][j] << " | "; } cout << endl; } cout << endl;} Przyjrzyj sie uwarznie funkcji void Matrix::Multiply_Matrixes (int &Number_of_rows_m3, int &Number_of_columns_m3, int &Number_of_columns_m1) powinna ci ona pomodz w zrozumieniu jak to dziala Pozdrawiam Cytuj Udostępnij tę odpowiedź Odnośnik do odpowiedzi Udostępnij na innych stronach Więcej opcji udostępniania...
ArtifeX Opublikowano 11 Lutego 2005 Zgłoś Opublikowano 11 Lutego 2005 Dodam jeszcze ze ma byc to w jezyku C. Siedze nad tym programem troche mam, jak skoncze wkleje kod i poprosze od modyfikacje (bo na pewno take beda potrzebne). Z gory dziekuje Cytuj Udostępnij tę odpowiedź Odnośnik do odpowiedzi Udostępnij na innych stronach Więcej opcji udostępniania...
_Borys Opublikowano 11 Lutego 2005 Zgłoś Opublikowano 11 Lutego 2005 Dodam jeszcze ze ma byc to w jezyku C. Siedze nad tym programem troche mam, jak skoncze wkleje kod i poprosze od modyfikacje (bo na pewno take beda potrzebne). Z gory dziekuje 1270421[/snapback] To zmien class na struct i bedzie w jezyku C. Mowilem popatrz na ten program zobacz co z czym i wykorzystaj obserwacje w swoim programie. Juz latwiej chyba nie moze byc. Cytuj Udostępnij tę odpowiedź Odnośnik do odpowiedzi Udostępnij na innych stronach Więcej opcji udostępniania...
jack0 Opublikowano 19 Lutego 2005 Zgłoś Opublikowano 19 Lutego 2005 #include<iostream>#include<iomanip>using namespace std;int main(){int m,p,n;cout<<"Wierze macierzy A: "; cin>>m;cout<<"Kolumny macierzy A: "; cin>>p;cout<<"Kolumny macierzy B: "; cin>>n;int A[m][p], B[p][n], C[m][n];//Wczytanie do macierzy Acout<<"Podaj liczby do amcierzy A:";for(int i=0;i<m;i++) for(int j=0;j<p;j++) cin>>A[i][j]; //Wyswietlenie macierzy Acout<<"Macierz A:"<<endl;for(int i=0;i<m;i++,cout<<endl) for(int j=0;j<p;j++) cout<<setw(4)<<A[i][j];//--------------//Wczytanie do macierzy Bcout<<"Podaj liczby do amcierzy B:";for(int i=0;i<p;i++) for(int j=0;j<n;j++) cin>>B[i][j]; //Wyswietlenie macierzy Bcout<<"Macierz B:"<<endl;for(int i=0;i<p;i++,cout<<endl) for(int j=0;j<n;j++) cout<<setw(4)<<B[i][j]; //--------------//Zerowanie macierzy Cfor(int i=0;i<m;i++) for(int j=0;j<n;j++) C[i][j]=0; //-------------- //Mnozenie macierzyfor(int i=0;i<m;i++)//wiersze macierzy A for(int j=0;j<n;j++)//kolumny macierzy B for(int k=0;k<p;k++)//kolumny macierzy A=wiersze macierzy B C[i][j]=C[i][j]+A[i][k]*B[k][j]; //-------------- //Wyswietlenie macierzy Cfor(int i=0;i<m;i++,cout<<endl) for(int j=0;j<n;j++) cout<<setw(4)<<C[i][j]; system("pause");return 0;} Moja własna wersja tego programu :) przed sekundka napisana. Sam algorytm, brak zabezpieczen typu: if(m!=n) ..... wszelkie pytania na GG 4777941 bo na forum nie siedze, zajrzalem przez przypadek po 2 latach sprawdzic czy mi konta nie skasowali ;) Cytuj Udostępnij tę odpowiedź Odnośnik do odpowiedzi Udostępnij na innych stronach Więcej opcji udostępniania...