LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Closed Thread
  Search this Thread
Old 09-11-2007, 12:28 PM   #1
anzdyy
LQ Newbie
 
Registered: Sep 2007
Posts: 13

Rep: Reputation: 0
please check for me the error and tell me~~ thx!


Write a complete C program to perform Matrix operations such as Addition, Subtraction, Multiplication and Transpose according to the user’s choice. The program should display the following menu to the user for them to select one of the five options listed.

------------------
MAIN MENU
------------------
1. MATRIX ADDITION
2. MATRIX SUBTRACTION
3. MATRIX MLTIPLICATION
4. MATRIX TRANSPOSE
5. TO EXIT
---------------------------
Please enter your option <1/2/3/4/5>:

The program should ask the elements of the input matrix or matrices from the user once the required operation is selected. The program should produce a neat output showing both the input and the resultant matrix in matrix form. The program should not be terminated until the user selects the option number 5 (TO EXIT).


THIS IS THE PROGRAM I HAVE DONE.. URGENT PLEASE CORRECT FOR ME! THANKS A LOT!!!!



#include <stdio.h> /* This header file is for C language */
#include <stdlib.h> /* for the exit() function */

/*Function prototype*/
int matrix_add(int a[3][3],int b[3][3],int result[3][3]);
int matrix_subtract(int a[3][3],int b[3][3],int result[3][3]);
int matrix_multiply(int a[3][3],int b[3][3],int result[3][3]);
int matrix_transpose(int a[3][3], int result[3][3]);
int print_matrix(int c[3][3]);

int main()
{
int counter = 0; /* initialize counter */
int option;/*initialize options available*/
int a[3][3];
int b[3][3];
int c[3][3];

/*Print the operations that to be performed*/

printf(" Welcome to User Menu of Matrix Operations\n\n");
printf("--------------------------------------------\n");
printf("\t\tMAIN MENU\n");
printf("--------------------------------------------\n");
printf("\t1. MATRIX ADDITION\n");
printf("\t2. MATRIX SUBTRACTION\n");
printf("\t3. MATRIX MULTIPLICATION\n");
printf("\t4. MATRIX TRANSPOSE\n");
printf("\t5. TO EXIT\n");
printf("--------------------------------------------\n");
printf("Please enter your option <1/2/3/4/5>:\n");

/*Print the input matrices & output*/
{
printf("\nMatrix 1:\n");
print_matrix(a);

printf("\nMatrix 2:\n");
print_matrix(b);

printf("\nResult:\n");
print_matrix(c);
}


/*As buffer*/

while (counter= 0)
{
/* Get the option*/
if (option==1)
{
matrix_add(a,b,c);
}
else if (option==2)
{
matrix_subtract(a,b,c);
}
else if (option==3)
{
matrix_multiply(a,b,c);
}
else if (option==4)
{
matrix_transpose(a,c);
}
else
{
exit( 1 );
}

/*Get the input for two matrices*/
/*Suppose the option is transpose, the input is one matrice*/


/*Switch case for each operation*/
switch (matrix_operations)

{
case 1:/*addition function*/
{
printf("Enter elements of Matrix 1:\n");
scanf("%d",& int a[3][3]);
printf("Enter elements of Matrix 2:\n");
scanf("%d",& int b[3][3]);
matrix_add(a,b,c)
}
break;
case 2:/*subtraction function*/
{
printf("Enter elements of Matrix 1:\n");
scanf("%d",& int a[3][3]);
printf("Enter elements of Matrix 2:\n");
scanf("%d",& int b[3][3]);
matrix_subtract(a,b,c)
}
break;
case 3:/*multiplication function*/
{
printf("Enter elements of Matrix 1:\n");
scanf("%d",& int a[3][3]);
printf("Enter elements of Matrix 2:\n");
scanf("%d",& int b[3][3]);
matrix_multiply(a,b,c)
}
break;
case 4:/*transpose function*/
{
printf("Enter elements of Matrix 1:\n");
scanf("%d",& int a[3][3]);
matrix_transpose(a,c)
}
break;
default:
{
printf("End of Program!\n");
exit( 1 );
}

}

system("pause");
return 0;
}

/*Function definition*/
int matrix_add(int a[3][3], int b[3][3], int result[3][3])
{
int i, j;
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
result[i][j] = a[i][j] + b[i][j];
}
}
return (a+b);
}

int matrix_subtract(int a[3][3],int b[3][3], int result[3][3])
{
int i,j;
for (i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
result[i][j]= a[i][j]- b[i][j];
}
}
return (a-b);
}

int matrix_multiply(a[3][3],b[3][3],int result[3][3])
{
int i,j,k;
{
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
for (k = a[i][j] = 0; k < n; k++)
{
result[i][j] += b[i][k] * c[k][j];
}
}
return (a*b);
}

int matrix_transpose(int a[3][3],int result[3][3])
{
int i,j,n,temp;
{
for (i = 0; i < n-1; i++)
for (j = i+1; j < n; j++)
{
temp = b[i][j];
b[i][j] = b[j][i];
b[j][i] = temp;
}
}
return (b);
}
int print_matrix(int c[3][3])
{
int i, j;
for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
{
printf("%d\t", a[i][j]);
}
printf("\n");
}
}
}

Last edited by anzdyy; 09-11-2007 at 12:33 PM.
 
Old 09-11-2007, 01:00 PM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Is there something that the program does wrong? Ah, I see, it does not compile.

Line 77: matrix_operations is not declared, not initialized and never used.
Line 48: you assign a value to counter, you don't compare it
Line 83/85: why do you use 'int' there?
Line 86: semicolon missing

And there are a couple of things more like your function definitions that seem to be inside the main function.

Did you ever try to compile this? Or are we doing your homework?


PS 1: Nothing is urgent at LQ

PS 2: There is a dedicated programming forum at LQ.

PS 3: And please put your code in code tags, indentations will be maintained that way so it's better readable.
 
Old 09-11-2007, 01:22 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.

Dupe of http://www.linuxquestions.org/questi...d.php?t=583083
and 10 others .... :/


Clean up your act, kid, and we WON'T do your homework; less so if
you keep spamming the forums with the same question despite repeated
closures.
 
  


Closed Thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Please Help Me Solve This Please!! Thx! anzdyy Programming 9 09-12-2007 11:20 AM
Please Help Me Solve This Please!! Thx! anzdyy Linux - Newbie 9 09-08-2007 02:52 PM
hi,everyone.I need your help.thx fetsh Linux - Newbie 5 08-09-2004 12:41 AM
just like to say thx. Peacedog General 3 11-15-2003 10:42 AM
THX Trickykid! fxlee Linux - Newbie 2 01-30-2002 10:28 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration