Thread: what's wrong with this code? (lagange polynomial multiplier)

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    7

    what's wrong with this code? (lagange polynomial multiplier)

    insert
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    #include <stdlib.h>
    
    
    main()
    {
        FILE *exibir;
        int i, j, k, l, n=3;
        double x[3]={3,3.5,4}, fx[3]={1.099,1.253,1.386}, lag[n], c[n], d[n], e[n], vet[n], **pn, vpx=1, dif, vat;
    
    
        pn = (double**)malloc((n)*sizeof(double*));
        for(i=0;i<n;i++){
            pn[i] = (double*)malloc((2^n)*sizeof(double));
        }
    
    
        for(i=0; i<n; i++)
        {
            lag[i] = 1;
            for(j=0; j<n; j++)
            {
                if(j!=i)
                {
                    lag[i] = lag[i] * (1/(x[i] - x[j]));
                }  
            }
        }
        
        for(i=0;i<n;i++){
            vpx = vpx * (-x[i]);
        }
        
        for(i=0;i<n;i++){
            vet[i] = 1;
        }
        
            for(k=0;k<n;k++){
                        for(l=0;l<n;l++){
                                for(i=n-1;i>l;i--){
                                    
                                    
                                    vet[i] = (vet[i] * x[i]);
                                    vat = vat + vet[i];
                                    }
                                pn[k][l] = (vpx/vat);
                                vet[l] = 1;
                                vat = 0;
                                }
                                
                        }    
                    
                    
                    for(i=0;i<n;i++){
                            d[i] = lag[i]*fx[i];
                    }
                    
                
                    
                    for(i=0;i<n;i++){
                        for(j=0;j<n;j++){
                            c[i] = c[i] + pn[j][i];
                        }
                    }
                    
                    for(i=0;i<n;i++){
                        e[i] = c[i]*d[i];
                    }
        
        exibir = fopen("exemplo.txt","w");
        fprintf(exibir,"lagrange: ");
        for(i=0;i<n;i++){
            fprintf(exibir," %.5lf",e[i]);
        }
        fclose(exibir);
        free(pn);
    }

  2. #2
    Registered User
    Join Date
    Nov 2015
    Posts
    7
    i can't multiply the polynomials in the part with three "for"

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > pn = (double**)malloc((n)*sizeof(double*));
    Don't cast the result of malloc in C programs.
    FAQ > Casting malloc - Cprogramming.com
    If you've done everything right, you don't need to cast.
    If you've done something wrong, casting just hides the problem.

    > pn[i] = (double*)malloc((2^n)*sizeof(double));
    If you were hoping for 2 to the power of n here, then you're mistaken.
    ^ is the exclusive or operator.

    Some better indentation would help as well. Additional comments in the code.
    Code:
    #include <stdio.h>
    #include <conio.h>  //!! obsolete, don't use
    #include <math.h>
    #include <stdlib.h>
    
    main()  //!! should explicitly return int, and have return 0; at the end
    {
      FILE *exibir;
      int i, j, k, l, n = 3;
      //!! some of these could be localised to inner scopes
      double x[3] = { 3, 3.5, 4 }, 
            fx[3] = { 1.099, 1.253, 1.386}, 
            lag[n], c[n], d[n], e[n], vet[n], 
            **pn, vpx = 1, dif, vat;
    
      pn = (double **) malloc((n) * sizeof(double *));
      for (i = 0; i < n; i++) {
        pn[i] = (double *) malloc((2 ^ n) * sizeof(double));
      }
    
      for (i = 0; i < n; i++) {
        lag[i] = 1;
        for (j = 0; j < n; j++) {
          if (j != i) {
            lag[i] = lag[i] * (1 / (x[i] - x[j]));
          }
        }
      }
    
      for (i = 0; i < n; i++) {
        vpx = vpx * (-x[i]);
      }
    
      for (i = 0; i < n; i++) {
        vet[i] = 1;
      }
    
      for (k = 0; k < n; k++) {
        for (l = 0; l < n; l++) {
          for (i = n - 1; i > l; i--) {
            vet[i] = (vet[i] * x[i]);
            vat = vat + vet[i];
          }
          pn[k][l] = (vpx / vat);
          vet[l] = 1;
          vat = 0;
        }
      }
    
      for (i = 0; i < n; i++) {
        d[i] = lag[i] * fx[i];
      }
    
      for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
          c[i] = c[i] + pn[j][i];
        }
      }
    
      for (i = 0; i < n; i++) {
        e[i] = c[i] * d[i];
      }
    
      exibir = fopen("exemplo.txt", "w");
      fprintf(exibir, "lagrange: ");
      for (i = 0; i < n; i++) {
        fprintf(exibir, " %.5lf", e[i]);
      }
      fclose(exibir);
    
      //!! memory leak, you need to free each pn[i] first.
      free(pn);
    }
    > i can't multiply the polynomials in the part with three "for"
    Probably because your pn array is broken.
    Code:
    $ gcc -Wall -Wextra -O2 -g foo.c
    foo.c:6:1: warning: return type defaults to ‘int’ [-Wreturn-type]
    foo.c: In function ‘main’:
    foo.c:14:24: warning: unused variable ‘dif’ [-Wunused-variable]
    foo.c:73:1: warning: control reaches end of non-void function [-Wreturn-type]
    $ valgrind ./a.out
    ==5903== Memcheck, a memory error detector
    ==5903== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
    ==5903== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
    ==5903== Command: ./a.out
    ==5903== 
    ==5903== Invalid write of size 8
    ==5903==    at 0x40082A: main (foo.c:44)
    ==5903==  Address 0x51d20a8 is 0 bytes after a block of size 8 alloc'd
    ==5903==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==5903==    by 0x4006D4: main (foo.c:18)
    ==5903== 
    ==5903== Invalid read of size 8
    ==5903==    at 0x400896: main (foo.c:56)
    ==5903==  Address 0x51d20a8 is 0 bytes after a block of size 8 alloc'd
    ==5903==    at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
    ==5903==    by 0x4006D4: main (foo.c:18)
    /// much more reportage snipped.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2015
    Posts
    7
    thx! gonna try now

  5. #5
    Registered User
    Join Date
    Nov 2015
    Posts
    7
    well, the coding doesnt seem to be the problem, its the logic

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with c-code for polynomial calculation
    By dreadkopp in forum C Programming
    Replies: 12
    Last Post: 12-22-2011, 10:09 PM
  2. OpenMP parallel threads for matrix multiplier
    By collymitch in forum C Programming
    Replies: 0
    Last Post: 04-07-2005, 04:38 PM
  3. matrix multiplier...
    By spudtheimpaler in forum C Programming
    Replies: 8
    Last Post: 02-26-2004, 01:40 PM
  4. least common multiplier
    By Jaguar in forum C Programming
    Replies: 6
    Last Post: 03-14-2003, 11:57 AM

Tags for this Thread