[C] ช่วยด้วยครับ ผมไม่เข้าใจว่าจะต้องเขียนยังไงดี( โปรแกรมบวก ลบ และ คูณ เมตริกซ

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: [C] ช่วยด้วยครับ ผมไม่เข้าใจว่าจะต้องเขียนยังไงดี( โปรแกรมบวก ลบ และ คูณ เมตริกซ

Re: [C] ช่วยด้วยครับ ผมไม่เข้าใจว่าจะต้องเขียนยังไงดี( โปรแกรมบวก ลบ และ คูณ เมตริกซ

โดย oramaru » 09/08/2011 12:48 am

touleg เขียน:ใช้ while(1) ครอบตั้งแต่แสดงเมนูจนจบโปรแกรม โดยถ้าจะให้จบโปรแกรม ก็ตรวจสอบค่าที่รับจากการเลือกเมนู สมมุติว่ากด 'q' ออกจากโปรแกรม ก็ตรวจสอบหลังจากรับค่าเลือกเมนู ว่าถ้าเป็น 'q' ก็ให้ break; มันก็จะออกจากลูปทำให้จบโปรแกรมได้ ถ้าเลือกเมนูอื่น เมื่อทำงานจนจบแล้วมันก็จะวนกลับมาแสดงเมนูใหม่ แล้วก็รับค่าเลือกเมนูใหม่ไปอย่างนี้เรื่อยๆ จนกว่าจะกด q ออกจากโปรแกรม

ถ้าจะ clear หน้าจอด้วยแล้วค่อยแสดงเมนูใหม่ก็ให้ใช้ clrscr(); หลังจากทำงานเสร็จแล้ว (ก่อนวนกลับขึ้นมา)

clrscr() จะต้อง include<conio.h> ด้วย เฉพาะเขียนกับ turbo c++ หรือ Borland C++
ถ้าเป็นบน Visual Studio จะไม่มีคำสั่งนี้ใน conio.h แล้ว

ถ้าอยากได้ก็เอาโค้ดนี้ไปใช้ อาจารย์ผมเขียนไว้ ก็อบไปใส่ไฟล์แล้วเซพเป็น .h นะครับ
http://thanwa.cpe.mut.ac.th/howto/thaiconsole/
ไม่เข้าใจครับ

คือต้องการให้มันเริ่มโปรแกรมใหม่อ่ะครับ เช่น Back to Menu (Y/N)

แล้วถ้าเลือก Y ก็คือโปรแกรมจะย้อนกลับไปเริ่มต้นใหม่ แต่ถ้าเลือก N ก็คือปิดโปรแกรมเลยอะครับ

Re: [C] ช่วยด้วยครับ ผมไม่เข้าใจว่าจะต้องเขียนยังไงดี( โปรแกรมบวก ลบ และ คูณ เมตริกซ

โดย touleg » 08/08/2011 11:00 pm

ใช้ while(1) ครอบตั้งแต่แสดงเมนูจนจบโปรแกรม โดยถ้าจะให้จบโปรแกรม ก็ตรวจสอบค่าที่รับจากการเลือกเมนู สมมุติว่ากด 'q' ออกจากโปรแกรม ก็ตรวจสอบหลังจากรับค่าเลือกเมนู ว่าถ้าเป็น 'q' ก็ให้ break; มันก็จะออกจากลูปทำให้จบโปรแกรมได้ ถ้าเลือกเมนูอื่น เมื่อทำงานจนจบแล้วมันก็จะวนกลับมาแสดงเมนูใหม่ แล้วก็รับค่าเลือกเมนูใหม่ไปอย่างนี้เรื่อยๆ จนกว่าจะกด q ออกจากโปรแกรม

ถ้าจะ clear หน้าจอด้วยแล้วค่อยแสดงเมนูใหม่ก็ให้ใช้ clrscr(); หลังจากทำงานเสร็จแล้ว (ก่อนวนกลับขึ้นมา)

clrscr() จะต้อง include<conio.h> ด้วย เฉพาะเขียนกับ turbo c++ หรือ Borland C++
ถ้าเป็นบน Visual Studio จะไม่มีคำสั่งนี้ใน conio.h แล้ว

ถ้าอยากได้ก็เอาโค้ดนี้ไปใช้ อาจารย์ผมเขียนไว้ ก็อบไปใส่ไฟล์แล้วเซพเป็น .h นะครับ
http://thanwa.cpe.mut.ac.th/howto/thaiconsole/

Re: [C] ช่วยด้วยครับ ผมไม่เข้าใจว่าจะต้องเขียนยังไงดี( โปรแกรมบวก ลบ และ คูณ เมตริกซ

โดย oramaru » 08/08/2011 9:36 pm

ขอสอบถามเพิ่มเติมหน่อยครับ

ถ้าผมจะเอาคำสั่ง Continue to back menu อ่ะครับ ต้องเขียนยังไง ผมไม่เข้าใจ

Re: [C] ช่วยด้วยครับ ผมไม่เข้าใจว่าจะต้องเขียนยังไงดี( โปรแกรมบวก ลบ และ คูณ เมตริกซ

โดย touleg » 02/08/2011 8:32 pm

อ่าดีแล้วครับทำเอง อิอิ

ถ้าทำเป็นแบบเลือกช้อย คราวหน้าแนะนำว่าให้ใช้ switch ดีกว่าครับ จะดูเป็นระเบียบกว่าครับ

ส่วนตัวแปรตัวไหนที่ใช้เก็บค่าแล้วก็แสดงผลแล้ว แล้วไม่ได้เอาไปใช้กับข้อมูลเดิมแล้ว แนะนำว่าให้เอาไปใช้กับข้อมูลอื่นต่อก็ได้ครับ แต่ต้องเป็นข้อมูลแบบเดียวกันนะครับ จะได้ไม่ต้องประกาศตัวแปรหลายตัว มันเปลืองพื้นที่นะครับ แล้วทำให้โค้ดดูเยอะขึ้นด้วย มันรกครับ

Re: [C] ช่วยด้วยครับ ผมไม่เข้าใจว่าจะต้องเขียนยังไงดี( โปรแกรมบวก ลบ และ คูณ เมตริกซ

โดย oramaru » 02/08/2011 7:53 am

oramaru เขียน:#include <stdio.h>
#include <conio.h>
void main()
{
int m1[10][10],i,j,c,k,m2[10][10],add[10][10],mult[10][10],r1,c1,r2,c2;
printf("Enter number of rows and columns of A matrix MAX 10\n");
scanf("%d%d",&r1,&c1);
printf("Enter number of rows and columns of B matrix MAX 10\n");
scanf("%d%d",&r2,&c2);
clrscr();
printf("1. Addition Matrix\n\n");
printf("2. Deletion Matrix\n\n");
printf("3. Multiply Matrix\n\n");
printf("Choose : ");
scanf("%d",&c);
clrscr();
/*################################# Addition ################################*/
if(c==1)
{
if(r1==r2&&c1==c2)
{
printf("Enter rows and columns of A matrix \n");
printf("Row wise numbers\n\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&m1[j]);
}
printf("You have entered the A matrix as follows:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d\t",m1[j]);
printf("\n");
}
printf("Enter rows and columns of B matrix \n");
printf("Again row wise numbers\n\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&m2[j]);
}
printf("You have entered the B matrix as follows:\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d\t",m2[j]);
printf("\n");
}
printf("The result of the addition is as follows;\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
add[j]=m1[j]+m2[j];
printf("%d\t",add[j]);
}
printf("\n");
}
}
/*################################# Deletion ################################*/
else if(c==2)
{
if(r1==r2&&c1==c2)
{
printf("Enter rows and columns of A matrix \n");
printf("Row wise numbers\n\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&m1[j]);
}
printf("You have entered the A matrix as follows:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d\t",m1[j]);
printf("\n");
}
printf("Enter rows and columns of B matrix \n");
printf("Again row wise numbers\n\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&m2[i][j]);
}
printf("You have entered the B matrix as follows:\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d\t",m2[i][j]);
printf("\n");
}
printf("The result of the deletion is as follows;\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
add[i][j]=m1[i][j]-m2[i][j];
printf("%d\t",add[i][j]);
}
printf("\n");
}
}
}
/*################################# Multiply ################################*/
else if(c==3)
{
if(c1==r2)
{
printf("The result of the multiplication is as follows:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
mult[i][j]=0;
for(k=0;k<r1;k++)
{
mult[i][j]+=m1[i][k]*m2[k][j];
/*mult[0][0]=m1[0][0]*m2[0][0]+m1[0][1]*m2[1][0]+m1[0][2]*m2[2][0];*/
}
printf("%d\t",mult[i][j]);
}
printf("\n");
}
}
}
getch();
}
/*else
{
printf("Matrix multiplication cannot be done");
}*/
}


***** ติดตรงที่ว่า ตอนผมรันโปรแกรม ผมเลือก choice ข้อที่ 2 - 3 แล้วมัน ไม่รันให้อ่ะครับ มึนๆ งงๆ


ผมแก้ได้ละคับ ><

Re: [C] ช่วยด้วยครับ ผมไม่เข้าใจว่าจะต้องเขียนยังไงดี( โปรแกรมบวก ลบ และ คูณ เมตริกซ

โดย oramaru » 02/08/2011 7:45 am

#include <stdio.h>
#include <conio.h>
void main()
{
int m1[10][10],i,j,c,k,m2[10][10],add[10][10],mult[10][10],r1,c1,r2,c2;
printf("Enter number of rows and columns of A matrix MAX 10\n");
scanf("%d%d",&r1,&c1);
printf("Enter number of rows and columns of B matrix MAX 10\n");
scanf("%d%d",&r2,&c2);
clrscr();
printf("1. Addition Matrix\n\n");
printf("2. Deletion Matrix\n\n");
printf("3. Multiply Matrix\n\n");
printf("Choose : ");
scanf("%d",&c);
clrscr();
/*################################# Addition ################################*/
if(c==1)
{
if(r1==r2&&c1==c2)
{
printf("Enter rows and columns of A matrix \n");
printf("Row wise numbers\n\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&m1[j]);
}
printf("You have entered the A matrix as follows:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d\t",m1[j]);
printf("\n");
}
printf("Enter rows and columns of B matrix \n");
printf("Again row wise numbers\n\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&m2[j]);
}
printf("You have entered the B matrix as follows:\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d\t",m2[j]);
printf("\n");
}
printf("The result of the addition is as follows;\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
add[j]=m1[j]+m2[j];
printf("%d\t",add[j]);
}
printf("\n");
}
}
/*################################# Deletion ################################*/
else if(c==2)
{
if(r1==r2&&c1==c2)
{
printf("Enter rows and columns of A matrix \n");
printf("Row wise numbers\n\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&m1[j]);
}
printf("You have entered the A matrix as follows:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d\t",m1[j]);
printf("\n");
}
printf("Enter rows and columns of B matrix \n");
printf("Again row wise numbers\n\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&m2[i][j]);
}
printf("You have entered the B matrix as follows:\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d\t",m2[i][j]);
printf("\n");
}
printf("The result of the deletion is as follows;\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
add[i][j]=m1[i][j]-m2[i][j];
printf("%d\t",add[i][j]);
}
printf("\n");
}
}
}
/*################################# Multiply ################################*/
else if(c==3)
{
if(c1==r2)
{
printf("The result of the multiplication is as follows:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
mult[i][j]=0;
for(k=0;k<r1;k++)
{
mult[i][j]+=m1[i][k]*m2[k][j];
/*mult[0][0]=m1[0][0]*m2[0][0]+m1[0][1]*m2[1][0]+m1[0][2]*m2[2][0];*/
}
printf("%d\t",mult[i][j]);
}
printf("\n");
}
}
}
getch();
}
/*else
{
printf("Matrix multiplication cannot be done");
}*/
}


***** ติดตรงที่ว่า ตอนผมรันโปรแกรม ผมเลือก choice ข้อที่ 2 - 3 แล้วมัน ไม่รันให้อ่ะครับ มึนๆ งงๆ

Re: [C] ช่วยด้วยครับ ผมไม่เข้าใจว่าจะต้องเขียนยังไงดี( โปรแกรมบวก ลบ และ คูณ เมตริกซ

โดย touleg » 27/07/2011 1:53 am

ฝึกเองเถอะครับมันเป็นประโยชน์มาก ตอนผมเรียนอาจารย์ออกข้อสอบให้เขียนลงในกระดาษเลย โจทย์เป็น [A]t * อีกตั้งหาก ไม่มีคอมไพล์เลอร์ไว้ตรวจสอบด้วย ลองเขียนเถอะครับจะได้เป็น อีกอย่างคงไม่มีใครเสียเวลามานั่งพิมพ์ให้หรอกครับ ยังไงทำมาก่อนติดตรงไหนก็เอามาโพสถามนะครับ

Re: [C] ช่วยด้วยครับ ผมไม่เข้าใจว่าจะต้องเขียนยังไงดี( โปรแกรมบวก ลบ และ คูณ เมตริกซ

โดย oramaru » 25/07/2011 11:25 pm

สงสัยจะไม่มีใครช่วยผมเลยแหะ TT ^ TT

ยังไงกะขอบคุณที่เข้ามาดูรายละเอียดกันนะครับ เดี๋ยวผมลองไปมั่วๆ เองละกัน

ขอบคุณมากครับ TT ^ TT

[C] ช่วยด้วยครับ ผมไม่เข้าใจว่าจะต้องเขียนยังไงดี( โปรแกรมบวก ลบ และ คูณ เมตริกซ

โดย oramaru » 24/07/2011 3:28 pm

คือต้องการรับค่า m x n เท่าไหร่ก็ได้ แล้วมาแสดงผลให้เป็นตารางตามจำนวน m x n เลยอะครับ
คือผมไม่รู้จะเขียนออกมายังไงดี ให้มันแสดงผลในรูปของเมตริกซ์เลย เขียนออกมาทีไร แสดงผลเป็น แนวระนาบหมดเลย T ^ T

รบกวนผู้เชี่ยวชาญเขียนมาให้ผมดูเป็นแนวทางหน่อยนะครับ มะต้องหมดก็ได้ ขอทั้ง บวก ลบ และ คูณเลย

ขอบพระคุณมากครับ พอดีผมต้องใช้ไปเป็นโครงการก่อนสอบมิดเทอมอ่า ช่วยทีนะครับ

ข้างบน