Page 1 of 4 123 ... LastLast
Results 1 to 10 of 37

Thread: Program to do matrices

  1. #1
    Join Date
    Dec 2004
    Posts
    476
    Rep Power
    0

    Default Program to do matrices

    im suppose to write a program to do matrices in C but my problem have already started. as im having problems with the menu. can someone plz take a look and give your take on the situation.


    #include stdio.h

    using namespace std;

    void populate();
    void display();
    void operation();
    void credit();

    int main()
    {
    int input;

    printf("1. Populate the matrix\n");
    printf("2. Display Matrix\n");
    printf("3. Perform Operation\n");
    printf("4. Credits\n");
    printf("5. Exit\n");
    cout<<"Selection: ";
    cin>> input;
    switch ( input ) {
    case 1:
    populate();
    break;
    case 2:
    display();
    break;
    case 3:
    operation();
    break;
    case 4:
    credit();
    break;
    case 5:
    cout<<"Thank you for using our program\n";
    break;
    default: /
    cout<<"Error, bad input, quitting\n";
    break;
    }

    }
    Last edited by akoo; Apr 8, 2006 at 10:35 AM.
    WHY PAY WHEN YOU CAN GET IT FREE!!!!!

  2. #2
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Put the switch in a loop, eg a while loop

    On case 5, exit the program.
    You can return for main after display text nfo

    You can use a boolean flag bEscape that is set in 'case 5' to true, then test the flag in the while loop and break if true

    You can use goto but that will get messy easily if you are not careful, so I advise the use of a flag
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  3. #3
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    the problem is you havenot started thinking about how a matrix works. how come you start at the menu? you should be thinking about the structure of your matrix and the operations. mathematics behind it (inverse, determinant, adjoint, addition, subtraction, multiplication. etc). how you expect to be good at programming when you running from the problem. now you run up in the syntax of c, and still nu start solve the problem.

    Remember : logic -> algorithm -> (test|proof) -> code -> (run|debug)

    Youre at stage 4, without completing step 1.

    good luck still.

    Code:
    #include <stdio.h> 
    
    // using namespace std; // this is for c++ 
    
    void populate(void);
    void display(void);
    void operation(void);
    void credit(void);
    
    int main()
    {
    	int input;
    
    	// in order to coordinate all those functions you would declare your matrix here
    	// float matrix[MAX_SIZE][MAX_SIZE]; // such as this
    
    	do
    	{
    		printf("1. Populate the matrix\n");
    		printf("2. Display Matrix\n");
    		printf("3. Perform Operation\n");
    		printf("4. Credits\n");
    		printf("5. Exit\n");
    
    		printf("Selection: ");
    		fflush(stdin);
    		scanf("%d", &input);
    
    		switch( input ) 
    		{
    		case 1:
    			populate(); // you should pass the matrix to the populate function
    			break;
    		case 2:
    			display();	// you should pass the matrix to the display function
    			break;
    		case 3:
    			operation();// you should pass the matrix to the operation function
    			break;
    		case 4:
    			credit();	// 
    			break;
    		case 5:
    			printf("Thank you for using our program\n");
    			break;
    		default: 
    			printf("Error, bad input\n"); // dont quit give them a chance... maybe three
    			break;
    		}
    
    		printf("\n\n");
    		// clrscr(); // #include <conio.h> to use this function
    	}while(input != 5 );
    
    	// getch(); // #include <conio.h> to use this function
    
    	return 0;
    }
    
    // now you have to define what the functions do here
    // by the way you have the prototypes above, i doubt they will be useful
    // since its just the menu though, i hope you change them
    void populate(){}
    void display(){}
    void operation(){}
    void credit(){}
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  4. #4
    Join Date
    Dec 2004
    Posts
    476
    Rep Power
    0

    Default

    ok i have wrote 2 of the function (credits and display) but now im having problems with the polulate function(this is suppose to populate the function) and the operation function(scalar,addition,subtraction,multiplicatio n) and also the program should be able to store up to 5 received matrices in an array. please help me as best as possible.
    WHY PAY WHEN YOU CAN GET IT FREE!!!!!

  5. #5
    Join Date
    Dec 2004
    Posts
    476
    Rep Power
    0

    Default

    I sat at school and i wrote the code until my fingers hurt this is what i came up with
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<math.h>
    #include<ctype.h>
    
    
    # define ROW_MAX 5
    # define Column_MAX 5
    
    int rows=0,size=0;
    int columns =0;
    int arrayA[ROW_MAX][Column_MAX];
    int arrayB[ROW_MAX][Column_MAX];
    int arrayC[ROW_MAX][Column_MAX];
    
    
    void display(void);
    void Populate_Matrix(void);
    void Main_menu(void);
    void operations();
    void addition();
    void subtraction();
    void ScalarMult();
    
    void main()
    {// start of main
    
    Main_menu();
    
    }//end of main
    
    
    void Main_menu(void)
    {
    	char choice;
    	do
    	{
    
    	printf("\tA.   Populate the matrix\n");
    	printf("\tB.   Display the matrix\n");
    	printf("\tC.   Perform operation\n");
    	printf("\tD.   Credits\n");
    	printf("\tE.   Exit Program\n\n\n");
    	printf("Please enter your choice: ");
    	scanf(" %c",&choice);
    	choice = toupper(choice);
        //system("cls");
    
    	switch(choice)
    	{
    	case 'A':{  
    				 Populate_Matrix();
    				break;
    			 }
    
    	case 'B':
    
    		{
    			display();
    			break;
    		}
    
    	case 'C':
    	
    		{
    			operations();
    
    			break;
    		}
    
    	case 'D':
    
    		{
    			printf("done by Akeem Gordon CMCS 0506476\n\n\n\n");
    			break;
    		}
    
    	case 'E':
    		{
    			break;
    		}
    
        default:
    		{
    		printf("Please choose a letter from A-E,You enterd a letter that is not accepted\n");
    		}
    	}
    	}while(choice !=0);
    }
    
    void Populate_Matrix(void)
    {
    	 
    	 int  i,j;
    
    	do
    	{
    
    	 printf("Enter the number of mantrix you need\n");
    	 scanf("%d",&size);
    
    	}while(size <0 || size >3);
    
    	 printf("Enter the number of row and colums of mantrix\n");
    	
    	 scanf("%d%d",&rows,&columns);
    
    
    	if(size > 0 && size <= 3 )
    	{
      	 printf("\tA.   Add values to matrix A\n");
    		for(i=0; i < rows; i++)
    		{
    			for(j=0;j < columns; j++)
    			{
    				scanf("%d",&arrayA[i][j]);
    			}
    		}
    	}
    
    	
    	if(size>1 && size <=3)
    	{
      	 printf("\tA.   Add values to matrix B\n");
    		for(i=0; i < rows; i++)
    		{
    			for(j=0;j < columns; j++)
    			{
    				scanf("%d",&arrayB[i][j]);
    			}
    		}
    	}
    
    	
    	if(size>2 && size <=3)
    	{
      	 printf("\tA.   Add values to matrix C\n");
    		for(i=0; i < rows; i++)
    		{
    			for(j=0;j < columns; j++)
    			{
    				scanf("%d",&arrayC[i][j]);
    			}
    		}
    	}
    
    
    }
    
    void display(void)
    {
    	int i, j;
    	
    	if(size > 0 && size <= 3 )
    	{
      	 printf("\t  Add values to matrix A\n");
    		for(i=0; i < rows; i++)
    		{
    			for(j=0;j < columns; j++)
    			{
    				printf("%d ",arrayA[i][j]);
    			}
    				printf("\n");
    		}
    	}
    	printf("\n");
    	
    	if(size>1 && size <=3)
    	{
      	 printf("\t  values to matrix B\n");
    		for(i=0; i < rows; i++)
    		{
    			for(j=0;j < columns; j++)
    			{
    					printf("%d ",arrayB[i][j]);
    			}
    			printf("\n");
    		}
    	}
    	printf("\n");
    	
    	if(size>2 && size <=3)
    	{
      	 printf("\tValues stores matrix C\n");
    		for(i=0; i < rows; i++)
    		{
    			for(j=0;j < columns; j++)
    			{
    					printf("%d ",arrayC[i][j]);
    			}
    				printf("\n");
    		}
    	}
    		printf("\n");
    
    
    
    
    }
    
    void operations()
    {
    	int choice=0;
    
    	do
    	{
    
    	printf("\t1	  Scalar\n");
    	printf("\t2   Addition\n");
    	printf("\t3   Substraction\n");
    	printf("\t4   Credits\n");
    	printf("\t5   Exit Program\n\n\n");
    	
    	scanf(" %d",&choice);
    	
    		switch(choice)
    		{
    				case 1:
    
    					break;
    				case 2:addition();
    
    					break;
    						case 3:subtraction();
    
    					break;
    						case 4:ScalarMult();
    
    					break;
    				
    				
    				
    				
    				default:
    					{
    					printf("Please choose a letter from A-E,You enterd a letter that is not accepted\n");
    					}
    		}
    	}while(choice !=0);
    
    
    }
    
    void addition()
    {
    	int i,j;
    	int result[5][5]={0};
    	int choice1, choice2;
    
    	printf("Enter the two matrix you need to use\n");
    	scanf("%d %d", &choice1,&choice2);
    
    	if(choice1 == 1 && choice2 ==2)
    	{
    		for(i=0; i < rows; i++)
    		{
    			for(j=0;j < columns; j++)
    			{
    					printf("%d ",result[i][j] = arrayA[i][j] + arrayB[i][j]);
    			}
    			printf("\n");
    				
    		}
    	}
    
    		
    
    	
    	if(choice1 == 1 && choice2 ==3)
    	{
    		for(i=0; i < rows; i++)
    		{
    			for(j=0;j < columns; j++)
    			{
    				printf("%d ",result[i][j] = arrayA[i][j] + arrayC[i][j]);
    			}
    			printf("\n");
    				
    		}
    	}
    
    
    	if(choice1 == 2 && choice2 ==3)
    	{	
    		for(i=0; i < rows; i++)
    		{
    			for(j=0;j < columns; j++)
    			{
    				result[i][j] = arrayC[i][j] + arrayB[i][j];
    			}
    				
    		}
    	}
    	
    }
    
    void subtraction()
    {
    	int i,j;
    	int result[5][5]={0};
    	int choice1, choice2;
    
    	printf("Enter the two matrix you need to use\n");
    	scanf("%d %d", &choice1,&choice2);
    
    	if(choice1 == 1 && choice2 ==2)
    	{
    		for(i=0; i < rows; i++)
    		{
    			for(j=0;j < columns; j++)
    			{
    					printf("%d ",result[i][j] = arrayA[i][j] - arrayB[i][j]);
    			}
    			printf("\n");
    				
    		}
    	}
    
    		
    
    	
    	if(choice1 == 1 && choice2 ==3)
    	{
    		for(i=0; i < rows; i++)
    		{
    			for(j=0;j < columns; j++)
    			{
    				printf("%d ",result[i][j] = arrayA[i][j] - arrayC[i][j]);
    			}
    			printf("\n");
    				
    		}
    	}
    
    
    	if(choice1 == 2 && choice2 ==3)
    	{	
    		for(i=0; i < rows; i++)
    		{
    			for(j=0;j < columns; j++)
    			{
    				result[i][j] = arrayC[i][j] - arrayB[i][j];
    			}
    				
    		}
    	}
    
    }
    
    
    
    void ScalarMult()
    {
    	int i,j;
    	int result[5][5]={0};
    	int choice1, scaleFactor=0;
    
    	printf("Enter the two matrix you need to SCALE  and scale factor\n");
    	scanf("%d %d", &choice1,&scaleFactor);
    
    	if(choice1 == 1 )
    	{
    		for(i=0; i < rows; i++)
    		{
    			for(j=0;j < columns; j++)
    			{
    					printf("%d ",result[i][j] = arrayA[i][j] * scaleFactor);
    			}
    			printf("\n");
    				
    		}
    	}
    
    		
    	if(choice1 == 2 )
    	{
    		for(i=0; i < rows; i++)
    		{
    			for(j=0;j < columns; j++)
    			{
    				printf("%d ",result[i][j] = arrayB[i][j] * scaleFactor);
    			}
    			printf("\n");
    				
    		}
    	}
    
    
    	if(choice1 == 3)
    	{	
    		for(i=0; i < rows; i++)
    		{
    			for(j=0;j < columns; j++)
    			{
    					printf("%d ",result[i][j] = arrayC[i][j] * scaleFactor);
    			}
    				
    		}
    	}
    
    }
    plz look at it and tell me what u think
    WHY PAY WHEN YOU CAN GET IT FREE!!!!!

  6. #6
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Looks OK.

    Improvements
    1) Matrix A, B, and C should be in an array for easy 'index' selection
    Code:
    int arrays[Matrix_MAX][ROW_MAX][Column_MAX];
    2) Between the input and the calculations, you can simplify the data passed which is just an array of matrices instead of a 3D array in the previous suggestion
    Code:
    Matrix arrays[Matrix_MAX]; //where Matrix is a data type contaning int array[ROW_MAX][Column_MAX]
    3) Separate the input code from the calculation code. (Think of it as if you got another program to do like "calculate the average of 3 matrices and then subtract 2A, 3B and 4C" and you would not need a user input for most of the operations. Centralize as much input code in one region, and separate it from the calculations

    4) If you are using C++, you can add the operations on a matrix as the behaviours of a matrix

    class Matrix //any matrix of any size
    attributes
    Size
    ArrayValues
    behaviours
    AddMatrix(Matrix OtherMatrix)
    ...

    When Matrix is in a class, it has the same organization like a struct, ie treated as one data type, but it can also hide the data inside it from user interface code that does not need to access it, such code only needs to know that it is adding matrices A.AddMatrix(B). The Matrix class, therefore, encapsulates data (which is its array) for external codes
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  7. #7
    Join Date
    Dec 2004
    Posts
    476
    Rep Power
    0

    Default

    at this moment im having problems writing the code for the multiplication section does anyone care to help???? also i dont know if anyone tried to run the program but the addition, subtraction and scalar fucntions do not work so plz help me out!
    Last edited by akoo; Apr 10, 2006 at 08:44 PM.
    WHY PAY WHEN YOU CAN GET IT FREE!!!!!

  8. #8
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    I dont remember if it row by column or column by row

    for a 3 by 3 matrix

    diagram

    [ 1 2 3 ] . [ 1 2 3 ]
    [ 4 5 6 ] x [ 4 5 6 ]
    [ 7 8 9 ] . [ 7 8 9 ]

    gives

    [ (1.1+2.4+3.7) (1.2+2.5+3.8) (1.3+2.6+3.9) ]
    [ (4.1+5.4+6.7) (4.2+5.5+6.8) (4.3+5.6+6.9) ]
    [ (7.1+8.4+9.7) (7.2+8.5+9.8) (7.3+8.6+9.9) ]



    Find a pattern for the loop(s) eg notice how i and j increment for the first element of the matrix. There are many pattern just code the one that seems straight forward yet efficiently done, in my opinion .
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  9. #9
    Join Date
    Dec 2004
    Posts
    476
    Rep Power
    0

    Default

    bwoy saying it is one thing but to typw this program is another
    WHY PAY WHEN YOU CAN GET IT FREE!!!!!

  10. #10
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    try
    printf("%d ",arrayC[i][j] * scaleFactor);

    instead of
    printf("%d ",result[i][j] = arrayC[i][j] * scaleFactor);

    Apart from that I do not have a debugger (just reinstalled OS)
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •