PDA

View Full Version : مشكل در كد ضرب ماتريسها



mafia5
سه شنبه 29 مرداد 1387, 12:45 عصر
ببخشيد اما من اين كد رو نوشتم كه دو تا ماتريس رو با هم ضرب كنه.

اما همون اول كار شرط IF رو انجام نميده و تابع فراخواني نميشه.:متعجب::گریه:

ممنون ميشم راهنمايي كنيد



#include<stdio.h>
#include<conio.h>
void minput(int);
void multiply(int,int);
void output(int);
void main()
{
char ans='y';
printf("\t\tHi,This is a program that has been written by Abtin");
printf("\n\n\t\t\tThis program will multiply two matrix.");
printf("\n\n\t\t\tDo you want to continew?­­­(y/n)");
ans=getch();
if(ans=='y')
{
void minput();
}//end of if
else
{
printf("\n\t\t\tThanks.My program finished here.");
}//end of else
clrscr();
getch();
}//end of main
//********************************
void minput(){
int mat1[2][3],mat2[3][4],i,j;
printf("\n\t\t\tplease enter the value of the first matrix:");
for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%d%d",&mat1[i][j]);
printf("\n\t\t\tyour matrix is this:");
for(i=0;i<2;i++)
for(j=0;j<3;j++)
printf("%d",mat1[i][j]);
printf("/n");
//end of inputin matrix 1
printf("\n\t\t\tNow please enter your second matrix:");
for(i=0;i<3;i++)
for(j=0;j<4;j++)
scanf("%d%d",&mat2[i][j]);
printf("\n\t\t\tyour second matrix is this:");
for(i=0;i<3;i++)
for(j=0;j<4;j++)
printf("%d",mat2[i][j]);
printf("/n");
//end of inputing matrix 2
printf("\t\t\tDo you want to continew?(y\n)");
char ans='y';
ans=getch();
if(ans=='y'){
void multiply(int mat1[2][3],int mat2[3][4]);
}//end of if
else{
printf("\n\t\t\tThanks for your giving time for my program");
}//end of else
getch();
}//end of minput
//*******************************
void multiply(int mat1[2][3],int mat2[3][4]){
printf("\n\t\t\tnow we want to multiply your two matrixes");
int mat3[3][4],k,i,j;
for(i=0;i<2;i++)
for(j=0;j<4;j++){
mat3[i][j]=0;
for(k=0;k<3;k++)
mat3[i][j]=mat3[i][j]+mat1[i][k]*mat2[k][j];
void output(int mat3[3][4]);
}//end of for
}//end of multiply
//********************************
void output(int mat3[3][4]){
printf("\n\t\t\tThis is your result of choosing matrixes:%d",mat3[3][4]);
}//end of output

MOHSEN8000
سه شنبه 29 مرداد 1387, 14:38 عصر
اشکالی که همین اول داشت یکی این بود که : برای فراخوانی تابع دیگه نیاز نیست که نوع بازگردانده شده اون رو هم بنویسی ( مثلا برای minput ، باید بنویسی minput خالی و نباید از void استفاده کنی . منظورم فراخوانی داخل if هست ) . ضمن این که نمونه اولیه که برای هر تابع معرفی کردی که مثلا این تابع یک متغیر int می گیره ، باید دقیقا مطابق با همین تعریف بهش یه متغیر int پاس بدی. که در داخل این if اصلا چیزی به تابع ندادی) .

mafia5
سه شنبه 29 مرداد 1387, 17:05 عصر
ممنون
درستش كردم اما دوباره تو خطي كه قرمزه ايراد ميگيره.

ممنون ميشم يه نگاهي كنيد.هرچي هم دست كاريش ميكنم درست نميشه:عصبانی++:(يهو فكر نكنيد خودم كاري نميكنم:گیج:)



#include<stdio.h>
#include<conio.h>
void minput();
void multiply(int [1][2],int [2][1]);
void output(int [1][1]);
void main()
{
char ans='y';
printf("\t\tHi,This is a program that has been written by Abtin");
printf("\n\n\t\t\tThis program will multiply two matrix.");
printf("\n\n\t\t\tDo you want to continew?­­­(y/n)");
ans=getch();
if(ans=='y')
{
minput();
}//end of if
else
{
printf("\n\t\t\tThanks.My program finished here.");
}//end of else
clrscr();
getch();
}//end of main
//********************************
void minput(){
int mat1[1][2],mat2[2][1],i,j;
printf("\n\t\t\tplease enter the value of the first matrix:");
for(i=0;i<1;i++){
for(j=0;j<2;j++)
scanf("%d%d",&mat1[i][j]);
}
printf("\n\t\t\tyour matrix is this:");
for(i=0;i<1;i++){
for(j=0;j<2;j++)
printf("%d",mat1[i][j]);
printf("/n");
}//end of for
//end of inputing matrix 1
printf("\n\t\t\tNow please enter your second matrix:");
for(i=0;i<2;i++)
for(j=0;j<1;j++)
scanf("%d%d",&mat2[i][j]);
printf("\n\t\t\tyour second matrix is this:");
for(i=0;i<2;i++){
for(j=0;j<1;j++)
printf("%d",mat2[i][j]);
printf("/n");
}//end of for
//end of inputing matrix 2
printf("\t\t\tDo you want to continew?(y\n)");
char ans='y';
ans=getch();
if(ans=='y'){
multiply(int mat1[1][2],int mat2[2][1]);
}//end of if
else{
printf("\n\t\t\tThanks for your giving time for my program");
}//end of else
getch();
clrscr();
}//end of minput
//*******************************
void multiply(int mat1[1][2],int mat2[2][1]){
printf("\n\t\t\tnow we want to multiply your two matrixes");
int mat3[1][1],k,i,j;
for(i=0;i<1;i++)
for(j=0;j<1;j++){
mat3[i][j]=0;
for(k=0;k<1;k++)
mat3[i][j]=mat3[i][j]+mat1[i][k]*mat2[k][j];
void output(int mat3[3][4]);
}//end of for
}//end of multiply
//********************************
void output(int mat3[3][4]){
printf("\n\t\t\tThis is your result of choosing matrixes:%d",mat3[3][4]);
}//end of output

MOHSEN8000
سه شنبه 29 مرداد 1387, 17:19 عصر
اون کدی رو که قرمز کردی که فکر نکنم مشکل داشته باشه. کاملا درست نوشتیش. فقط چند خط بالاترش که فراخوانی تابع muliply هست رو ، داخل پرانتز باید int ها رو برداری و به این خاطر که آرایه با فراخوانی با ارجاع به تابع رد می شه ( یعنی فقط اسمش برای تابع فرستاده می شه. اونم به صورت ارجاع با آدرس. ) دیگه نباید سایزش رو بزنی. یعنی کافیه اسم خود 2 تا آرایه رو بنویسی.

mafia5
سه شنبه 29 مرداد 1387, 18:58 عصر
آره درستش كردم

فقط ببينيد درست اجرا ميشه يا نه؟

ممنون


#include<stdio.h>
#include<conio.h>
void minput();
void multiply(int mat1[2][2],int mat2[2][2]);
void output(int mat3[2][2]);
void main()
{
char ans='y';
printf("\tغغغةثثثثثثثثثثثثثثثثثثثث ثثثثثثثثثثثثثثثثثثثثثثثثث ثثثثثثثثثثثثثثغغغ");
printf("\n\n\t\tHi,This is a program that has been written by Abtin");
printf("\n\n\t\tThis program will multiply two matrix.");
printf("\n\n\t\tDo you want to continew?­­­(y/n)");
ans=getch();
if(ans=='y')
{
minput();
}//end of if
else
{
printf("\n\n\t\t*****Thanks.My program finished here.*****");
getch();
}//end of else
clrscr();
getch();
}//end of main
//********************************

void minput(){
int mat1[2][2],mat2[2][2],i,j;
printf("\n\n\n\t\t*****please enter the value of the first matrix:*****");
for(i=0;i<2;i++){
for(j=0;j<2;j++)
scanf("%d",&mat1[i][j]);
}
printf("\n\n\t\t*****your matrix is this:*****");
for(i=0;i<2;i++){
for(j=0;j<2;j++)
printf("\t%d",mat1[i][j]);
printf("\n");
}//end of for
//end of inputing matrix 1
printf("\n\n\t\t*****Now please enter your second matrix:*****");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&mat2[i][j]);
printf("\n\n\t\t*****your second matrix is this:*****");
for(i=0;i<2;i++){
for(j=0;j<2;j++)
printf("\t%d",mat2[i][j]);
printf("\n");
}//end of for
//end of inputing matrix 2
printf("\n\n\t\t*****Do you want to continew?(y/n)*****");
char ans='y';
ans=getch();
if(ans=='y'){
multiply(mat1,mat2);
}//end of if
else{
printf("\n\n\t\t*****Thanks for your giving time for my program*****");
}//end of else
getch();
clrscr();
}//end of minput
//*******************************

void multiply(int mat1[2][2],int mat2[2][2]){
printf("\n\n\t\t*****now we want to multiply your two matrixes*****");
int mat3[2][2],k,i,j;
for(i=0;i<2;i++)
for(j=0;j<2;j++){
mat3[i][j]=0;
for(k=0;k<2;k++)
mat3[i][j]=mat3[i][j]+mat1[i][k]*mat2[k][j];
output(mat3);
}//end of for
}//end of multiply
//********************************

void output(int mat3[2][2]){
printf("\t\t\n\n*****your result of choosing of matrixes is:*****");
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
printf("\t\t%d",mat3[i][j]);
printf("\t\n");
}//end of output

MOHSEN8000
سه شنبه 29 مرداد 1387, 19:37 عصر
چند تا نکته رو به نظر من اگه رعایت کنی خیلی راحت تر می تونی برنامه بنویسی. چون برنامت مشکل نداره. یکم شکل نوشتنت بد جوره که برای آدم گیج کننده هست. اول این که حتی زمانی که دستورات for یا if فقط یکی هست ، از آکلاد استفاده کن و آکلاد های هر دستوری رو جلوش باز کن و وقتی می بندی ، دقیقا با خودش تو یه راستا باشه. بعد برنامتو که می نویسی خیلی تو هم ننویس یعنی سعی کن دستورای برنامه رو با فاصله بنویسی از هم. خیلی بهتر می تونی اون وقت برنامه رو اشکال گیری کنی. از دستور رفتن به خط بعد هم زیاد استفاده کن. ضرر نداره. منم اینارو رعایت نمی کردم قبلا ولی از وقتی این جوری می نویسم خیلی error گیری برنامه هام برام راحت تره.




#include<stdio.h>
#include<conio.h>
void minput();
void multiply(int mat1[2][2],int mat2[2][2]);
void output(int mat3[2][2]);
void main()
{
char ans='y';
printf("\tÛÛÛÉËËËËËËËËËËËËËËËËËËËË ËËËËËËËËËËËËËËËËËËËËËËËËË ËËËËËËËËËËËËËËÛÛÛ");
printf("\n\n\t\tHi,This is a program that has been written by Abtin");
printf("\n\n\t\tThis program will multiply two matrix.");
printf("\n\n\t\tDo you want to continew?­­­(y/n)");
ans=getch();
if(ans=='y')
{
minput();
}//end of if
else
{
printf("\n\n\t\t*****Thanks.My program finished here.*****");
getch();
}//end of else
getch();
}//end of main
//********************************
void minput(){
int mat1[2][2],mat2[2][2],i,j;
printf("\n\n\n\t\t*****please enter the value of the first matrix:***** \n");
for(i=0;i<2;i++){
for(j=0;j<2;j++){
printf("[%d , %d] : " ,i , j);
scanf("%d",&mat1[i][j]);
}
}
printf("\n\n\t\t*****your matrix is this:*****\n");
for(i=0;i<2;i++){
for(j=0;j<2;j++)
printf("\t%d",mat1[i][j]);
printf("\n");
}//end of for
//end of inputing matrix 1
printf("\n\n\t\t*****Now please enter your second matrix:*****\n");
for(i=0;i<2;i++){
for(j=0;j<2;j++){
printf("[%d , %d] : " ,i , j);
scanf("%d",&mat2[i][j]);
}
}
printf("\n\n\t\t*****your second matrix is this:*****\n");
for(i=0;i<2;i++){
for(j=0;j<2;j++)
printf("\t%d",mat2[i][j]);
printf("\n");
}//end of for
//end of inputing matrix 2
printf("\n\n\t\t*****Do you want to continew?(y/n)*****\n");
char ans='y';
ans=getch();
if(ans=='y'){
multiply(mat1,mat2);
}//end of if
else{
printf("\n\n\t\t*****Thanks for your giving time for my program*****\n");
}//end of else
getch();
}//end of minput
//*******************************
void multiply(int mat1[2][2],int mat2[2][2]){
printf("\n\n\t\t*****now we want to multiply your two matrixes*****");
int mat3[2][2],k,i,j;
for(i=0;i<2;i++){
for(j=0;j<2;j++){
mat3[i][j]=0;
for(k=0;k<2;k++){
mat3[i][j]=mat3[i][j]+mat1[i][k]*mat2[k][j];
}
}
}
output(mat3);
//end of for
}//end of multiply
//********************************
void output(int mat3[2][2]){
printf("\t\t\n\n*****your result of choosing of matrixes is:*****\n");
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
printf("\t\t%d",mat3[i][j]);
}
printf("\t\n");
}
}//end of output


اینم برنامه خودته . فقط من یه چند خط بهش اضافه کردم. یه چند جاشم آکلاد گذاشتم. موفق باشی.