Thread: Matrix in C...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    23

    Matrix in C...

    I want to do this in FOR, could someone help me.

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <time.h>
    #include <stdlib.h>
    
    int main () {
        int matriz[3][3],matriz2[3][3],matriz3[3][6];
        int cont,i;
        time_t hora;
        hora = time(NULL);
        srand(hora);  /*função para gerar numeros randomicos*/
    
    
        for (i=0;i<3;i++) 
        {
           for (cont=0;cont<3;cont++)
           {
            matriz[i][cont] = ((rand()%20)+1); /*atribui os numeros randomicos de 1 a 20 na matriz */
            matriz2[i][cont]=((rand()%20)+1);
           }
        }
    
    printf("   \x1A PRIMEIRA MATRIZ \n\n");
        for (i=0;i<3;i++)                    /*mostrar as matriz*/
         {
           for (cont=0;cont<3;cont++)
           {
              printf(" %i \t",matriz[i][cont]);
           }
           printf("\n");
        } 
           
        printf("\n\n");
    printf("  \x1A SEGUNDA MATRIZ \n\n");    
        for (i=0;i<3;i++)                        /*mostrar as matriz2*/
         {
           for (cont=0;cont<3;cont++)
           {
              printf(" %i \t",matriz2[i][cont]);
           }
           printf("\n");
        }    
      printf("\n\n");
      
      
    /*calculca a matriz3*/
    
    matriz3[0][0]=matriz[0][0];
    matriz3[1][0]=matriz[1][0];
    matriz3[2][0]=matriz[2][0];
    
    matriz3[0][1]=matriz2[0][0];
    matriz3[1][1]=matriz2[1][0];
    matriz3[2][1]=matriz2[2][0];
    
    matriz3[0][2]=matriz[0][1];
    matriz3[1][2]=matriz[1][1];
    matriz3[2][2]=matriz[2][1];
    
    matriz3[0][3]=matriz2[0][1];
    matriz3[1][3]=matriz2[1][1];
    matriz3[2][3]=matriz2[2][1];
    
    matriz3[0][4]=matriz[0][2];
    matriz3[1][4]=matriz[1][2];
    matriz3[2][4]=matriz[2][2];
    
    matriz3[0][5]=matriz2[0][2];
    matriz3[1][5]=matriz2[1][2];
    matriz3[2][5]=matriz2[2][2];
    
    
    
    printf("\n\n");
    printf("  \x1A TERCEIRA MATRIZ \n\n");
    
    for(i=0;i<3;i++)                        /*mostrar as matriz2*/
        {
           for(cont=0;cont<6;cont++)
           {
            printf(" %i \t",matriz3[i][cont]);
           }
           printf("\n");
        } 
    
    getch();
    }

  2. #2
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    You want to do all this in a for? If that's what you mean, you could make a function and execute it in a for..

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I'm guessing the question was, "How can I do the same thing as this ugly code in a few tidy for loops?"
    Code:
    matriz3[0][0]=matriz[0][0];
    matriz3[1][0]=matriz[1][0];
    matriz3[2][0]=matriz[2][0];
    
    matriz3[0][1]=matriz2[0][0];
    matriz3[1][1]=matriz2[1][0];
    matriz3[2][1]=matriz2[2][0];
    
    matriz3[0][2]=matriz[0][1];
    matriz3[1][2]=matriz[1][1];
    matriz3[2][2]=matriz[2][1];
    
    matriz3[0][3]=matriz2[0][1];
    matriz3[1][3]=matriz2[1][1];
    matriz3[2][3]=matriz2[2][1];
    
    matriz3[0][4]=matriz[0][2];
    matriz3[1][4]=matriz[1][2];
    matriz3[2][4]=matriz[2][2];
    
    matriz3[0][5]=matriz2[0][2];
    matriz3[1][5]=matriz2[1][2];
    matriz3[2][5]=matriz2[2][2];
    Well, it looks like you need one variable to loop through matriz3[X] and matriz2[X], and then another to loop through matriz3[X][Y]. (The same variable could be used for matriz2: matriz2[X][Y / 2].)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  3. Gauss-Jordan Matrix Inversion in C++
    By Max_Power82 in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2006, 08:31 PM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM