Advertisement
michael_xgrind

Vetor -> Matriz

Aug 6th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.46 KB | None | 0 0
  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. #include "time.h"
  4. #include "math.h"
  5.  
  6. int main(){
  7.     int i, j, k, num, lMatriz, cMatriz, lVetor, aux;
  8.  
  9.     srand(time(NULL));
  10.  
  11.     /* Entrada de dados */
  12.     printf("Entre com o numero de elementos do vetor: ");
  13.     scanf("%d", &num);
  14.  
  15.     lMatriz = pow(num,0.5); /* linha da matriz = raiz quadrada do n de elementos */
  16.     cMatriz = num/lMatriz; /* coluna da matriz = n de elementos divido por linhas da matriz */
  17.     aux = num - (lMatriz*cMatriz);
  18.  
  19.     /* Criacao de matriz e vetores */
  20.     int vetor[num];
  21.     int matriz[lMatriz][cMatriz];
  22.     int novoVetor[aux];
  23.  
  24.     for(i=0; i<num; i++){
  25.         vetor[i] = rand() % 9+1;
  26.     }
  27.  
  28.     for(i=0; i<lMatriz; i++){
  29.         for(j=0; j<cMatriz; j++){
  30.             matriz[i][j] = vetor[k];
  31.             k++; /* variavel que percorre o vetor */
  32.         }
  33.     }
  34.  
  35.     if(aux){ /* Se aux for diferente de 0, entao entra no laco */
  36.         for(i=0; i<aux; i++){
  37.             novoVetor[i] = -9;
  38.         }
  39.     }
  40.  
  41.     /* Impressao */
  42.     printf("\n\nVetor:\n");
  43.     for(i=0; i<num; i++){
  44.         printf("%d ", vetor[i]);
  45.     }
  46.  
  47.     printf("\n\nMatriz:\n");
  48.     for(i=0; i<lMatriz; i++){
  49.         for(j=0; j<cMatriz; j++){
  50.             printf("%d ", matriz[i][j]);
  51.         }
  52.         printf("\n");
  53.     }
  54.  
  55.     if(aux){
  56.         printf("\n\nNovo vetor:\n");
  57.         for(i=0; i<aux; i++){
  58.             printf("%d ", novoVetor[i]);
  59.         }
  60.     }
  61.  
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement