Advertisement
salatiel

GuitarristaMatriz

Sep 27th, 2012
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.57 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "dinamico.h"
  4.  
  5. int num_instancias(FILE *f){
  6. char vetor [1000] ;
  7. int instancias,cont=0;
  8.  
  9.     while(!feof(f)){
  10.     fgets(vetor,200,f);
  11.     cont++;
  12.     }
  13.  instancias = cont/2;
  14.  return instancias;
  15.  
  16. }
  17.  
  18.  
  19. void preenche_matriz(int** matriz,int numero_variacoes,int volume_maximo){
  20. int i,j;
  21.  
  22. for (i=0;i<numero_variacoes;i++){
  23.     for(j=0;j<=volume_maximo;j++)
  24.     matriz[i][j]= 0;
  25. }
  26.  
  27.  
  28. }
  29.  
  30.  
  31. void matriz_dinamica(int **matriz, int* vetor_variacoes, int volume_inicial, int volume_maximo, int numero_variacoes){
  32.    int i,j, resultado=0, conta_vetor=numero_variacoes-1, posicao, posicao_sub, posicao_final;
  33.  
  34.    for (j=0;j<=volume_maximo;j++){
  35.    resultado = (j) + vetor_variacoes[conta_vetor];
  36.    if (resultado>volume_maximo ) resultado = (j) - vetor_variacoes[conta_vetor];
  37.    if (resultado<0) resultado =-1;
  38.  
  39.    matriz[numero_variacoes-1][j] = resultado;
  40.  
  41.    }
  42.  
  43.     for (i=numero_variacoes-2;i>=0;i--){
  44.         for (j=0;j<=volume_maximo;j++){
  45.         posicao = j+ vetor_variacoes[conta_vetor-1];
  46.         posicao_sub = j - vetor_variacoes[conta_vetor-1] ;
  47.  
  48.         if(posicao>volume_maximo) posicao = -1;
  49.             else posicao = matriz[i+1][posicao];
  50.  
  51.         if(posicao_sub<0) posicao_sub = -1;
  52.             else posicao_sub = matriz[i+1][posicao_sub];
  53.  
  54.  
  55.          if (posicao < posicao_sub) posicao_final = posicao_sub;
  56.             else posicao_final = posicao;
  57.  
  58.  
  59.  
  60.  
  61.         matriz[i][j]= posicao_final;
  62.  
  63.  
  64.  
  65.          }
  66.         conta_vetor--;
  67.         }
  68.  
  69.         printf("%d\n", matriz[0][volume_inicial]);
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement