-
نکته ی اشتباه اول (همون طور که آقای آریاپور گفتن) این هست که در موقع صدا زدن یک تابع برای اجرا نوع خروجی و انواع پرامترهای ورودی رو هم می نویسید و این کار اشتباهیه. این اطلاعات فقط موقع تعریف تابع باید نوشته بشه.(خطوط قرمز)
نکته ی دوم در نوع ورودی های توابعتون هست:
تابع minput که ورودی نمی خواهد!
برای تابع ضرب شما دو آرایه به عنوان ورودی دارید ولی در هنگام تعریف تابع نوع ورودی ها رو int گرفته اید که اشتباهه و نوع ورودی ها برای ورود دو تا آرایه باید مثل کد زیر باشه. همین مسئله هم برای تابع output صدق می کنه.(خطوط سبز)
نکته ی سوم در تابع minput و هنگام استفاده از تابع scanf فقط به یک علامت "%d" نیازه چون فقط یک عدد از کاربر گرفته میشه . پس چرا دو تا گذاشته شده ؟!(خطوط زرد)
نکته ی چهارم در بدنه ی تابع output شما قصد دارید یک ارایه را چاپ بکنید نه فقط یک عدد رو . پس باید از حلقه ی for بهره گرفت.
امیدوارم جایی رو اشتباه نکرده باشم و اشتباه نگفته باشم!
کد اصلاح شده:
کد:
#include<stdio.h>
#include<conio.h>
void minput();
void multiply(int a[2][3] ,int b[3][4]);
void output(int c[2][4]);
int 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();
}
else{
printf("\n\t\t\tThanks.My program finished here.");
}
clrscr();
getch();
return 0;
}
//********************************
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",&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");
}
printf("\n\t\t\tNow please enter your second matrix:");
for(i=0;i<3;i++)
for(j=0;j<4;j++)
scanf("%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");
}
printf("\t\t\tDo you want to continew?(y\n)");
char ans='y';
ans=getch();
if(ans=='y'){
multiply(mat1,mat2); }
else{
printf("\n\t\t\tThanks for your giving time for my program");
}
getch();
}
//*******************************
void multiply(int mat1[2][3],int mat2[3][4]){
printf("\n\t\t\tnow we want to multiply your two matrixes");
int mat3[2][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];
}
output(mat3);}
//********************************
void output(int mat3[2][4]){
printf("\n\t\t\tThis is your result of choosing matrixes:\n");
for(int i = 0; i < 2; i++){
for(int j = 0; j < 4; j++)
printf("%d", mat3[i][j]);
printf("\n");
}
getch();
}
-
بالاخره تمومش كردم.
فقط ميشه بگيد چجوريه و درست اجرا ميشه يا نه؟
ممنون.
کد:
#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
-
دوست عزیز کد شما کاملا درسته فقط یه جا در گذاشتن کروشه ی آخر for در تابع multyple دقت کنید یه اشتباه کوچکی شده...
-
ممنون.
تا قبلشم 4 بار با هم ضرب ميكرد كه فهميدم مشكل از اكولاد for بوده كه گزاشتمش بعد از فراخواني تابع خروجي.
اما اين كد رو كه ميزارم بعد از تلاش هاي بسيار درستش كردم كه دوستان هم كمك كردن.و درست درست است.:31::10:
کد:
#include<stdio.h>
#include<conio.h>
void minput();
void multiply(int mat1[2][2],int mat2[2][2]);
void output(int mat3[2][2]);
int main()
{
char ans='y',ans1='n';
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{
ans1=getch();
if(ans1=='n'){
printf("\n\n\t\t*****Thanks.My program finished here.*****");
}//end of if
else{
printf("please click y/n");
}//end of else
getch();
}//end of else
clrscr();
getch();
return 0;
}//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];
}//end of for
}//end of for
}//end of for
output(mat3);
}//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