LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Please Help Me Solve This Please!! Thx! (https://www.linuxquestions.org/questions/programming-9/please-help-me-solve-this-please-thx-583079/)

anzdyy 09-07-2007 11:34 PM

Please Help Me Solve This Please!! 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 MULTIPLICATION
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).


i would like to have this question in linux programming.. can any kind people help me to solve this?

teabag_46 09-07-2007 11:58 PM

Have you written the pseudocode for this? It might help you to do that first.

SilentSam 09-08-2007 02:10 AM

After the huge array of posts done by yourself, I offer this advice:

If you can't do this assignment, drop programming.

vdx 09-08-2007 03:40 AM

dear anzdyy

For ur problem I can sudgest u the following.

Step 1: Clear the matrix operations first(methemetically). U must be sure abt the matrix operation, only then u can do further.

Step 2: Prepare an algorithm for the problem. (Algo means general outline of ur program).

Step 3: Code ur algorithm into C.

If u think twice and take intrest into programing, U will definatly win the game.

Best Of Luck

b0uncer 09-08-2007 03:46 AM

To me this sounds like a school homework. We should not write the answer for you, instead you should figure it out and write yourself. If you have problems, we can help you, but you won't get a complete answer from us; it's you who need to do the work and learn from the task. If you just ask and get a ready answer, you don't learn anything.

Instead of just posting the homework, you should state that it is a homework, tell us what you have done so far, why you didn't succeed in it/what is the problematic part, and then read carefully what is answered to you.

If you ever want to learn programming, you need to do it yourself - it's not something you'll learn in a day or in a year by copying others' written works, it's something you learn by working and thinking yourself. Those operations described, except for the actual matrice calculations, are basic things and if you feel they're too difficult for you, you should consult your teacher. The matrice calculations are another matter, and I'm sure you are either told how they work or have documentation about them (they're simple as well). Anyway, please post here what you have done so far: "pseudocode" for example, or parts of the actual code (functions, ...), so we see what you've managed to do so far, and what you don't understand.

EDIT: there seems to be a small weakness in the work; the size of the matrices is not specified. Writing a program that can calculate matrices of any size may be a little more work than a program that calculates fixed-size (probably small) matrices. I'm not sure about the goal, but I suspect the point is not to write a program to deal with any kinds of matrices (including checks for matrice sizes to determine if the operation is possible at all); taking this point of view you should first probably think of the easiest case, working with 2x2 matrices only, and after that - if it's needed - fix the code to be able to deal with other sizes as well.

anzdyy 09-11-2007 01:15 PM

please check for me the error and tell me~~ thx!
 
C program to perform Matrix operations such as Addition, Subtraction, Multiplication and Transpose according to the user’s choice.
displaying following menu.
------------------
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).




#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",& a[3][3]);
printf("Enter elements of Matrix 2:\n");
scanf("%d",& 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");
}
}
}

wjevans_7d1@yahoo.co 09-11-2007 02:06 PM

What error message are you getting?

Or if there is no error message, what is the exact problem?

exvor 09-11-2007 07:03 PM

When I attempted to compile it I got many compiler errors on line 78,93,95


This code is formated rather poorly and its hard to follow the inner and outer statements. I probably could figure out where the error is but im busy at the moment maybe someone else here can.

Nylex 09-12-2007 02:20 AM

Why do you have a while loop and then a switch statement that look like they're doing the same thing? Also, you have "matrix_operations" in your switch statement, except it's not defined anywhere..

Wim Sturkenboom 09-12-2007 11:20 AM

See my answer in this thread
You were supposed to fix all the lines in the switch that contained someting with the 'int', not only in the line numbers that I gave you.

Your function definitions like 'int matrix_transpose(int a[3][3],int result[3][3])' still seem to be in the main function. That is not the way it's done (see example below).
Code:

int main()
{
...
...

return 0;
}


int matrix_transpose(int a[3][3],int result[3][3])
{

    return (b);
}

int matrix_subtract(int a[3][3],int b[3][3],int result[3][3])
{

    return (a-b);
}

Another problem that I just spotted is the return values of the matrix functions.
1) You tell the compiler that an integer is returned by these functions, but you return an array (return (b) in matrix_transpose)
2) You can not add, subtract etc arrays (e.g. return (a-b) in matrix_subtract)


All times are GMT -5. The time now is 10:09 PM.