Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. void Gauss_seidel(double **matriz, int erro_maximo, int info[]){
  6.     double *inicial, aux = 0;
  7.     int x = 0, y = 0, opa = 0, contador = 0;
  8.  
  9.     inicial = (double *) malloc(sizeof(double)*info[0]);
  10.  
  11.     for(x = 0; x < info[0]; x++)
  12.         scanf("%lf", &inicial[x]);
  13.  
  14.     //calcando o x1.. x2 e por aí vai
  15.     do{
  16.         printf("\n");
  17.         printf("%d .", contador+1);
  18.       //nesse loop haverá o cálculo do x1,x2 e por aí vai
  19.         for(x = 0; x < info[0]; x++){
  20.             aux = inicial[x];
  21.             inicial[x] = matriz[x][info[0]];
  22.  
  23.             for (y = 0; y < info[0];y++){
  24.                 if (y != x)
  25.                 inicial[x] = inicial[x]-matriz[x][y]*inicial[y];
  26.             }
  27.  
  28.             inicial[x] = inicial[x]/matriz[x][x];
  29.             //verificando se o número tá na precisão certa;
  30.             if(abs(inicial[x]-aux)<= erro_maximo)          
  31.                 opa++;
  32.              printf("%lf", inicial[x]);
  33.         }
  34.        printf("\n");
  35.        contador++;  
  36.     }while(opa < info[1] || abs(inicial[0]-aux)<= erro_maximo);                    
  37.    
  38.     /*printf("\nA solução eh:\n");
  39.     for (x = 0; x < info[0]; x++)
  40.         printf("x", x); */
  41. }
  42.  
  43. int main(){
  44.     FILE *numeros;
  45.     char nomedoarquivo[30], nome[30];
  46.     int info[2] = { 0 }, aux = 0, x = 0, y = 0, choose = 0;
  47.     double **matriz, erro_maximo = 0;
  48.  
  49.     do{
  50.         scanf("%d", &choose);
  51.     }while(choose < 1 && choose > 5);
  52.  
  53.     scanf("%s", nome);
  54.  
  55.     sprintf(nomedoarquivo, "%s.txt", nome);
  56.  
  57.     numeros = fopen(nomedoarquivo, "r");
  58.  
  59.     if(numeros == NULL)
  60.         printf("Arquivo nao pode ser lido");
  61.  
  62.     for(aux = 0; aux < 2; aux++)
  63.         fscanf(numeros, "%d", &info[aux]);
  64.  
  65.     fseek(numeros, 6, SEEK_SET);
  66.  
  67.     fscanf(numeros, "%lf", &erro_maximo);
  68.  
  69.     matriz = (double **) malloc(sizeof(double*)*info[0]);
  70.  
  71.     for(aux = 0; aux < info[0]; aux++)
  72.         matriz[aux] = (double *) malloc(sizeof(double)*(1+info[0]));
  73.  
  74.     for(x = 0; x < info[0]; x++){
  75.         for(y = 0; y < (1 + info[0]); y++){
  76.             if(!(fscanf(numeros, "%lf", &matriz[x][y])))
  77.                 break;
  78.         }
  79.     }
  80.  
  81.     fclose(numeros);
  82.     //coloquei o menu aqui pq a partir daqui todas as informações já estarão salvas no matriz, daí tu vai poder usar pra passar pra função
  83.      //o teu menu
  84.     //quando tiver algo pra botar, tira o ; que tá depois do )
  85.     if(choose == 1)
  86.         exit(1);
  87.     else if(choose == 2);
  88.     else if(choose == 3);
  89.     else if(choose == 4);
  90.     else if(choose == 5)
  91.         Gauss_seidel(matriz, erro_maximo, info);
  92.  
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement