how to change this simple opeation into power operation..

This is a discussion on how to change this simple opeation into power operation.. within the C Programming forums, part of the General Programming Boards category; I got this simple operation of 2 equal matrix multiplication: How to change it so if i enter a number ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    how to change this simple operation into power operation..

    I got this simple operation of 2 equal matrix multiplication:
    How to change it so if i enter a number
    it will put the matrix into the power of the input number.

    How do i transform some how this simple multiplication operation into a power operation.

    Ii have tried to use temporary matrix to save the local multiplication.
    and multiply by it on the next step
    but its not working
    ??

    Code:
    #include <stdio.h>
    
    
    
    
    int main(){//star
        int rows,cols,jndex,input;
        int sum2=0;
        printf("enter rows and cols [1..50] ==> ");
        scanf("%d %d",&rows,&cols);
        int matrix[rows][cols];
        int sum[rows][cols];
        int temp[rows][cols];
        int transpose [cols][rows];
        int index,kndex,tndex,gndex,lndex;
    printf("enter power:");
          scanf("%d",&input);
    
        for (index=0;index<rows;index++){
    
            for (kndex=0;kndex<cols;kndex++){
               matrix[index][kndex]=0;
               sum[index][kndex]=0;
               temp[index][kndex]=0;
               transpose[index][kndex]=0;
            }//end inner for
        }//end outer for
    
    printf("enter numbers in a row for a matrix:");                                                 //stat input
    
          for (index=rows-1;index>=0;index--){
    
               for (kndex=cols-1;kndex>=0;kndex--){
    
                 scanf("%d",&matrix[index][kndex]);
    
                 transpose [kndex][index]=matrix[index][kndex];
               }
    
            }
    
    getchar();  //needed because of scanf()
                                                 //end input
    
    
    
    
    
    
    
    
    
    
                                                 //start power operation
    
    
    
                                           //start temp=matrix
        for (gndex=0;gndex<rows;gndex++){
    
            for (lndex=0;lndex<cols;lndex++){
    
               temp[index][kndex]=matrix[index][kndex];
    
            }//end inner for
        }//end outer for
                                        //end temp=matrix
      for (tndex=0;tndex<input;tndex++){
    
                        for(index=0;index<rows;index++) {
                           for(jndex=0;jndex< cols;jndex++) {
    
                                     sum[index][jndex]=0;
                                        for(kndex=0;kndex<cols;kndex++) {
                                            sum[index][jndex] = sum[index][jndex] + temp[index][kndex] * matrix[kndex][jndex];
                                        }
                            }
                         }
    
    
    
                                           //start temp=sum
        for (gndex=0;gndex<rows;gndex++){
    
            for (lndex=0;lndex<cols;lndex++){
    
               temp[index][kndex]=sum[index][kndex];
    
            }//end inner for
        }//end outer for
                                        //end temp=sum
      }
    
                                              //end power operation
    
    
                    //start print
    
            for (index=0;index<rows;index++){
    
               for (kndex=0;kndex<cols;kndex++){
    
                 printf("%d ",sum[index][kndex]);
               }
               printf("\n");
            }
    
                                              //end print
    
                    printf("\n");
    
    
    
    
    
        return 0;
    }//end main func
    Last edited by transgalactic2; 12-20-2008 at 12:52 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Look at this loop again:
    Code:
                                       //start temp=matrix
        for (gndex=0;gndex<rows;gndex++){
    
            for (lndex=0;lndex<cols;lndex++){
    
               temp[index][kndex]=matrix[index][kndex];
    
            }//end inner for
        }//end outer for
    Notice how what's inside doesn't match the loop variables.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    How many times now have you been asked to fix your indentation? I can't keep track...
    Are you ignoring it on purpose?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thanks

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by Elysia View Post
    How many times now have you been asked to fix your indentation? I can't keep track...
    Are you ignoring it on purpose?

    i did ordered the code as much as i could
    look at the structure of the loops
    there is a clear pyramid on every operation
    for which you could easily see whats inside what

    the same way i ordered the cols

    how to order it??

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    this code is not working
    for the input of

    0 1 2
    3 0 7
    1 1 1

    and power 3

    i need to get
    15 14 33
    43 20 97
    16 14 32

    instead i get..

    75 48 161
    157 140 323
    74 48 162

    why..

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by transgalactic2 View Post
    i did ordered the code as much as i could
    look at the structure of the loops
    there is a clear pyramid on every operation
    for which you could easily see whats inside what

    the same way i ordered the cols

    how to order it??
    Except everything at the same level needs to stay at the same level -- no printfs going back to the left margin, or scanfs getting one extra level of indentation. This is your original code (without the fix), but you should have something like this:
    Code:
    #include <stdio.h>
    
    int main(){//star
      int rows,cols,jndex,input;
      int sum2=0;
      printf("enter rows and cols [1..50] ==> ");
      scanf("%d %d",&rows,&cols);
      int matrix[rows][cols];
      int sum[rows][cols];
      int temp[rows][cols];
      int transpose [cols][rows];
      int index,kndex,tndex,gndex,lndex;
      printf("enter power:");
      scanf("%d",&input);
    
      for (index=0;index<rows;index++){
        for (kndex=0;kndex<cols;kndex++){
          matrix[index][kndex]=0;
          sum[index][kndex]=0;
          temp[index][kndex]=0;
          transpose[index][kndex]=0;
        }//end inner for
      }//end outer for
    
      printf("enter numbers in a row for a matrix:");  //stat input
      for (index=rows-1;index>=0;index--){
        for (kndex=cols-1;kndex>=0;kndex--){
          scanf("%d",&matrix[index][kndex]);
          transpose [kndex][index]=matrix[index][kndex];
        }
      }
      getchar();  //needed because of scanf()
      //end input
      //start power operation
    
      //start temp=matrix
      for (gndex=0;gndex<rows;gndex++){
          for (lndex=0;lndex<cols;lndex++){
             temp[index][kndex]=matrix[index][kndex];
          }//end inner for
      }//end outer for
      //end temp=matrix
    
      for (tndex=0;tndex<input;tndex++){
        for(index=0;index<rows;index++) {
          for(jndex=0;jndex< cols;jndex++) {
            sum[index][jndex]=0;
            for(kndex=0;kndex<cols;kndex++) {
              sum[index][jndex] = sum[index][jndex] + temp[index][kndex] * matrix[kndex][index];
            }
          }
        }
        //start temp=sum
        for (gndex=0;gndex<rows;gndex++){
          for (lndex=0;lndex<cols;lndex++){
            temp[index][kndex]=sum[index][kndex];
          }//end inner for
        }//end outer for
        //end temp=sum
      }
      //end power operation
    
      //start print
      for (index=0;index<rows;index++){
        for (kndex=0;kndex<cols;kndex++){
          printf("%d ",sum[index][kndex]);
        }
        printf("\n");
      }
      //end print
      printf("\n");
      return 0;
    }//end main func
    Note also that you don't double-space your code, nor do you need eight blank lines between sections of code.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    How about code that looks like this?
    So much easier to read than your mess of code.

    Code:
    #include <stdio.h>
    
    int main(){//star
    	int rows,cols,jndex,input;
    	int sum2=0;
    	printf("enter rows and cols [1..50] ==> ");
    	scanf("%d %d",&rows,&cols);
    	int matrix[rows][cols];
    	int sum[rows][cols];
    	int temp[rows][cols];
    	int transpose [cols][rows];
    	int index,kndex,tndex,gndex,lndex;
    
    	printf("enter power:");
    	scanf("%d",&input);
    
    	for (index = 0; index < rows; index++)
    	{
    		for (kndex = 0; kndex < cols; kndex++)
    		{
    			matrix[index][kndex] = 0;
    			sum[index][kndex] = 0;
    			temp[index][kndex] = 0;
    			transpose[index][kndex] = 0;
    		}//end inner for
    	}//end outer for
    
    	printf("enter numbers in a row for a matrix:"); //stat input
    
    	for (index = rows - 1;index >= 0; index--)
    	{
    		for (kndex = cols - 1; kndex >= 0; kndex--)
    		{
    			scanf("%d", &matrix[index][kndex]);
    			transpose[kndex][index] = matrix[index][kndex];
    		}
    	}
    	getchar();  //needed because of scanf()
    	//end input
    
    	//start power operation
    
    	//start temp=matrix
    	for (gndex = 0; gndex < rows; gndex++)
    	{
    		for (lndex = 0; lndex < cols; lndex++)
    			temp[index][kndex] = matrix[index][kndex];
    	}//end outer for
    	
    	//end temp=matrix
    	for (tndex = 0; tndex < input; tndex++)
    	{
    		for (index = 0; index < rows; index++)
    		{
    			for (jndex = 0; jndex < cols; jndex++)
    			{
    				sum[index][jndex] = 0;
    				for (kndex = 0; kndex < cols; kndex++)
    					sum[index][jndex] = sum[index][jndex] + temp[index][kndex] * matrix[kndex][jndex];
    			}
    		}
    
    		//start temp=sum
    		for (gndex = 0; gndex < rows; gndex++)
    		{
    			for (lndex = 0; lndex < cols; lndex++)
    				temp[index][kndex] = sum[index][kndex];
    		}//end outer for
    		//end temp=sum
    	}
    
    	//end power operation
    
    	//start print
    	for (index = 0; index < rows; index++)
    	{
    		for (kndex = 0; kndex < cols; kndex++)
    			printf("%d ", sum[index][kndex]);
    		printf("\n");
    	}
    
    	//end print
    	printf("\n");
    	return 0;
    }//end main func
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your t loop goes one too far -- the first multiplication gives you matrix^2, but your program believes it is computing matrix^1.

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thanks it works

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  2. help with calculating change from a dollar
    By z.tron in forum C++ Programming
    Replies: 3
    Last Post: 09-13-2002, 03:58 PM
  3. (structure+array+pointer)to make a simple database
    By frankie in forum C Programming
    Replies: 5
    Last Post: 04-26-2002, 05:14 PM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM
  5. Replies: 2
    Last Post: 09-04-2001, 02:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21