Thread: NEED help, error in the last line of code

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    5

    NEED help, error in the last line of code

    so this code is in DEv_C,
    i keep getting a syntax error at end of line, all the way at the last line, please help me, im not sure if my modularization id wrong or not. HELP ME!!!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    void linreg(float *x,float *y,int n,float *m,float *b);
    int main(int argc, char *argv[])
    {
      int i; //year
      int j; //month
      int k; //date
      FILE *fpm;
      FILE *fpb;
      FILE *fp;
      char fn[100];
      
      //fpout = fopen("dataesout.txt","w");
      fpm = fopen("slopesout.txt","w");
      fpb = fopen("interceptsout.txt","w");
      
      for(i=2010;i<=2011;i++) 
      {
           for(j=1;j<12;j++)
            {
              for(k=1;k<=31;k++) 
              {
                        int z;
      int n=0;
      float x[5000];
      float y[5000];
      float m;
      float b;           
                 if(j==1) {
                      sprintf(fn,"jan %d %d.dat",k,i);    // makes the file name into a string
                      fp = fopen(fn,"r");
                       z=0.0;
                        while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF) {
                         z++;
                         }
                         n=z;
                     linreg(x,y,n,&m,&b);
                     //calculate m and b
                     fprintf(fpm,"%g\n",&m);
                     //save each to a file
                     fprintf(fpb,"%g\n",&b);
                     fclose(fp);
                     }
                     else if(j==2) {
                      if(k<=28){
                     sprintf(fn,"feb %d %d.dat",k,i);
                     fp = fopen(fn,"r");
                       z=0.0;
                        while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF) {
                         z++;
                         }
                         n=z;
                     linreg(x,y,n,&m,&b);
                     //calculate m and b
                     fprintf(fpm,"%g\n",&m);
                     //save each to a file
                     fprintf(fpb,"%g\n",&b);
                     fclose(fp);
                     }
                                  
                 else if(j==3) {
                     sprintf(fn,"mar %d %d.dat",k,i);
                      fp = fopen(fn,"r");
                       z=0.0;
                        while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF) {
                         z++;
                         }
                         n=z;
                     linreg(x,y,n,&m,&b);
                     //calculate m and b
                     fprintf(fpm,"%g\n",&m);
                     //save each to a file
                     fprintf(fpb,"%g\n",&b);
                    fclose(fp);
                     }
    
    
                 else if(j==4) {
                      if(k<=30) {
                     sprintf(fn,"apr %d %d.dat",k,i);
                              fp = fopen(fn,"r");
                       z=0.0;
                        while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF) {
                         z++;
                         }
                         n=z;
                     linreg(x,y,n,&m,&b);
                     //calculate m and b
                     fprintf(fpm,"%g\n",&m);
                     //save each to a file
                     fprintf(fpb,"%g\n",&b);
                     fclose(fp);
                     }
                     
                 else if(j==5) {
                     sprintf(fn,"may %d %d.dat",k,i);
                    fp = fopen(fn,"r");
                       z=0.0;
                        while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF) {
                         z++;
                         }
                         n=z;
                     linreg(x,y,n,&m,&b);
                     //calculate m and b
                     fprintf(fpm,"%g\n",&m);
                     //save each to a file
                     fprintf(fpb,"%g\n",&b);
                   fclose(fp);
                     }
                     
                 else if(j==6) {
                      if(k<=30){
                     sprintf(fn,"jun %d %d.dat",k,i);
                      fp = fopen(fn,"r");
                       z=0.0;
                        while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF) {
                         z++;
                         }
                         n=z;
                     linreg(x,y,n,&m,&b);
                     //calculate m and b
                     fprintf(fpm,"%g\n",&m);
                     //save each to a file
                     fprintf(fpb,"%g\n",&b);
                   fclose(fp);
                     }
                     
                 else if(j==7) {
                     sprintf(fn,"jul %d %d.dat",k,i);
                     fp = fopen(fn,"r");
                       z=0.0;
                        while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF) {
                         z++;
                         }
                         n=z;
                     linreg(x,y,n,&m,&b);
                     //calculate m and b
                     fprintf(fpm,"%g\n",&m);
                     //save each to a file
                     fprintf(fpb,"%g\n",&b);
                      fclose(fp);
                     }
                     
                 else if(j==8) {
                     sprintf(fn,"aug %d %d.dat",k,i);
                    fp = fopen(fn,"r");
                       z=0.0;
                        while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF) {
                         z++;
                         }
                         n=z;
                     linreg(x,y,n,&m,&b);
                     //calculate m and b
                     fprintf(fpm,"%g\n",&m);
                     //save each to a file
                     fprintf(fpb,"%g\n",&b);
                      fclose(fp);
                     }
                     
                 else if(j==9) {
                      if(k<=30){
                     sprintf(fn,"sep %d %d.dat",k,i);
                       fp = fopen(fn,"r");
                       z=0.0;
                        while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF) {
                         z++;
                         }
                         n=z;
                     linreg(x,y,n,&m,&b);
                     //calculate m and b
                     fprintf(fpm,"%g\n",&m);
                     //save each to a file
                     fprintf(fpb,"%g\n",&b);
                      fclose(fp);
                     }
                     
                 else if(j==10) {
                     sprintf(fn,"oct %d %d.dat",k,i);
                     fp = fopen(fn,"r");
                       z=0.0;
                        while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF) {
                         z++;
                         }
                         n=z;
                     linreg(x,y,n,&m,&b);
                     //calculate m and b
                     fprintf(fpm,"%g\n",&m);
                     //save each to a file
                     fprintf(fpb,"%g\n",&b);
                      fclose(fp);
                     }
                     
                 else if(j==11) {
                      if(k<=30) {
                     sprintf(fn,"nov %d %d.dat",k,i);
                          fp = fopen(fn,"r");
                       z=0.0;
                        while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF) {
                         z++;
                         }
                         n=z;
                     linreg(x,y,n,&m,&b);
                     //calculate m and b
                     fprintf(fpm,"%g\n",&m);
                     //save each to a file
                     fprintf(fpb,"%g\n",&b);
                    fclose(fp);
                     }
                     
                 else if(j==12) {
                     sprintf(fn,"dec %d %d.dat",k,i);
                     fp = fopen(fn,"r");
                       z=0.0;
                        while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF) {
                         z++;
                         }
                         n=z;
                     linreg(x,y,n,&m,&b);
                     //calculate m and b
                     fprintf(fpm,"%g\n",&m);
                     //save each to a file
                     fprintf(fpb,"%g\n",&b);
                  fclose(fp);
                     }
                     //fprintf(fpout,"%d %d %d.dat\n",j,k,i);
                  }
              }
         }
         fclose(fpb);
         fclose(fpm);
      system("PAUSE");    
      return 0;
    }
    
    
    void linreg(float *x, float *y, int n, float *m, float *b)
    {
      int p;
      float sumx=0.0;
      float sumy=0.0;
      float sumxy = 0.0;
      float sumxx=0.0;
      float sum2x=0.0;
      float sumyy=0.0;
      float sum2y=0.0;
    
    
      
    for(p=0;p<n;p++)
    {
      sumx = sumx + x[p];
      sumy = sumy + y[p];
      sumxy = sumxy + (x[p]*y[p]);
      sumxx = sumxx + (x[p]*x[p]);
      sum2x = sumx*sumx;
      sumyy = sumyy + (y[p]*y[p]);
      sum2y = sumy*sumy;
    }
    
    
        *m = (((n*sumxy)-(sumx*sumy))/((n*sumxx)-(sum2x)));
        *b = ((sumy - ((*m)*sumx))/n);
      return ;
    }

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Lines 21, 23, 25, 81, 47 don't have matching closing braces.


    and


    Code:
    fprintf(fpm,"%g\n",&m);
     
    should be
    
    
    fprintf(fpm,"%g\n", m);
    Fact - Beethoven wrote his first symphony in C

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Also, conio.h isn't supported on all compilers (it's not standard) -> Be sure that the person marking your assignment has it before using it.
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    5
    i have closing braces in lines 229, 230, and 231. don't those work??

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    When I indented your code properly, I found that the last line didn't have enough closing braces.

    That is why you indent your code properly.

    Code:
    oid linreg(float *x,float *y,int n,float *m,float *b);
    int main(int argc, char *argv[])
    {
        int i; //year
        int j; //month
        int k; //date
        FILE *fpm;
        FILE *fpb;
        FILE *fp;
        char fn[100];
    
    
        //fpout = fopen("dataesout.txt","w");
        fpm = fopen("slopesout.txt","w");
        fpb = fopen("interceptsout.txt","w");
    
    
        for(i=2010; i<=2011; i++)
        {
            for(j=1; j<12; j++)
            {
                for(k=1; k<=31; k++)
                {
                    int z;
                    int n=0;
                    float x[5000];
                    float y[5000];
                    float m;
                    float b;
                    if(j==1)
                    {
                        sprintf(fn,"jan %d %d.dat",k,i);    // makes the file name into a string
                        fp = fopen(fn,"r");
                        z=0.0;
                        while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF)
                        {
                            z++;
                        }
                        n=z;
                        linreg(x,y,n,&m,&b);
                        //calculate m and b
                        fprintf(fpm,"%g\n",&m);
                        //save each to a file
                        fprintf(fpb,"%g\n",&b);
                        fclose(fp);
                    }
                    else if(j==2)
                    {
                        if(k<=28)
                        {
                            sprintf(fn,"feb %d %d.dat",k,i);
                            fp = fopen(fn,"r");
                            z=0.0;
                            while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF)
                            {
                                z++;
                            }
                            n=z;
                            linreg(x,y,n,&m,&b);
                            //calculate m and b
                            fprintf(fpm,"%g\n",&m);
                            //save each to a file
                            fprintf(fpb,"%g\n",&b);
                            fclose(fp);
                        }
    
    
                        else if(j==3)
                        {
                            sprintf(fn,"mar %d %d.dat",k,i);
                            fp = fopen(fn,"r");
                            z=0.0;
                            while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF)
                            {
                                z++;
                            }
                            n=z;
                            linreg(x,y,n,&m,&b);
                            //calculate m and b
                            fprintf(fpm,"%g\n",&m);
                            //save each to a file
                            fprintf(fpb,"%g\n",&b);
                            fclose(fp);
                        }
    
    
    
    
                        else if(j==4)
                        {
                            if(k<=30)
                            {
                                sprintf(fn,"apr %d %d.dat",k,i);
                                fp = fopen(fn,"r");
                                z=0.0;
                                while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF)
                                {
                                    z++;
                                }
                                n=z;
                                linreg(x,y,n,&m,&b);
                                //calculate m and b
                                fprintf(fpm,"%g\n",&m);
                                //save each to a file
                                fprintf(fpb,"%g\n",&b);
                                fclose(fp);
                            }
    
    
                            else if(j==5)
                            {
                                sprintf(fn,"may %d %d.dat",k,i);
                                fp = fopen(fn,"r");
                                z=0.0;
                                while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF)
                                {
                                    z++;
                                }
                                n=z;
                                linreg(x,y,n,&m,&b);
                                //calculate m and b
                                fprintf(fpm,"%g\n",&m);
                                //save each to a file
                                fprintf(fpb,"%g\n",&b);
                                fclose(fp);
                            }
    
    
                            else if(j==6)
                            {
                                if(k<=30)
                                {
                                    sprintf(fn,"jun %d %d.dat",k,i);
                                    fp = fopen(fn,"r");
                                    z=0.0;
                                    while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF)
                                    {
                                        z++;
                                    }
                                    n=z;
                                    linreg(x,y,n,&m,&b);
                                    //calculate m and b
                                    fprintf(fpm,"%g\n",&m);
                                    //save each to a file
                                    fprintf(fpb,"%g\n",&b);
                                    fclose(fp);
                                }
    
    
                                else if(j==7)
                                {
                                    sprintf(fn,"jul %d %d.dat",k,i);
                                    fp = fopen(fn,"r");
                                    z=0.0;
                                    while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF)
                                    {
                                        z++;
                                    }
                                    n=z;
                                    linreg(x,y,n,&m,&b);
                                    //calculate m and b
                                    fprintf(fpm,"%g\n",&m);
                                    //save each to a file
                                    fprintf(fpb,"%g\n",&b);
                                    fclose(fp);
                                }
    
    
                                else if(j==8)
                                {
                                    sprintf(fn,"aug %d %d.dat",k,i);
                                    fp = fopen(fn,"r");
                                    z=0.0;
                                    while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF)
                                    {
                                        z++;
                                    }
                                    n=z;
                                    linreg(x,y,n,&m,&b);
                                    //calculate m and b
                                    fprintf(fpm,"%g\n",&m);
                                    //save each to a file
                                    fprintf(fpb,"%g\n",&b);
                                    fclose(fp);
                                }
    
    
                                else if(j==9)
                                {
                                    if(k<=30)
                                    {
                                        sprintf(fn,"sep %d %d.dat",k,i);
                                        fp = fopen(fn,"r");
                                        z=0.0;
                                        while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF)
                                        {
                                            z++;
                                        }
                                        n=z;
                                        linreg(x,y,n,&m,&b);
                                        //calculate m and b
                                        fprintf(fpm,"%g\n",&m);
                                        //save each to a file
                                        fprintf(fpb,"%g\n",&b);
                                        fclose(fp);
                                    }
    
    
                                    else if(j==10)
                                    {
                                        sprintf(fn,"oct %d %d.dat",k,i);
                                        fp = fopen(fn,"r");
                                        z=0.0;
                                        while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF)
                                        {
                                            z++;
                                        }
                                        n=z;
                                        linreg(x,y,n,&m,&b);
                                        //calculate m and b
                                        fprintf(fpm,"%g\n",&m);
                                        //save each to a file
                                        fprintf(fpb,"%g\n",&b);
                                        fclose(fp);
                                    }
    
    
                                    else if(j==11)
                                    {
                                        if(k<=30)
                                        {
                                            sprintf(fn,"nov %d %d.dat",k,i);
                                            fp = fopen(fn,"r");
                                            z=0.0;
                                            while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF)
                                            {
                                                z++;
                                            }
                                            n=z;
                                            linreg(x,y,n,&m,&b);
                                            //calculate m and b
                                            fprintf(fpm,"%g\n",&m);
                                            //save each to a file
                                            fprintf(fpb,"%g\n",&b);
                                            fclose(fp);
                                        }
    
    
                                        else if(j==12)
                                        {
                                            sprintf(fn,"dec %d %d.dat",k,i);
                                            fp = fopen(fn,"r");
                                            z=0.0;
                                            while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF)
                                            {
                                                z++;
                                            }
                                            n=z;
                                            linreg(x,y,n,&m,&b);
                                            //calculate m and b
                                            fprintf(fpm,"%g\n",&m);
                                            //save each to a file
                                            fprintf(fpb,"%g\n",&b);
                                            fclose(fp);
                                        }
                                        //fprintf(fpout,"%d %d %d.dat\n",j,k,i);
                                    }
                                }
                            }
                        }
                    }
                    fclose(fpb);
                    fclose(fpm);
                    system("PAUSE");
                    return 0;
                }
    
    
    
    
                void linreg(float *x, float *y, int n, float *m, float *b)
                {
                    int p;
                    float sumx=0.0;
                    float sumy=0.0;
                    float sumxy = 0.0;
                    float sumxx=0.0;
                    float sum2x=0.0;
                    float sumyy=0.0;
                    float sum2y=0.0;
    
    
    
    
    
    
                    for(p=0; p<n; p++)
                    {
                        sumx = sumx + x[p];
                        sumy = sumy + y[p];
                        sumxy = sumxy + (x[p]*y[p]);
                        sumxx = sumxx + (x[p]*x[p]);
                        sum2x = sumx*sumx;
                        sumyy = sumyy + (y[p]*y[p]);
                        sum2y = sumy*sumy;
                    }
    
    
    
    
                    *m = (((n*sumxy)-(sumx*sumy))/((n*sumxx)-(sum2x)));
                    *b = ((sumy - ((*m)*sumx))/n);
                    return ;
                }
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    5
    i fixed everything as stated by Click_here, but the error still comes up

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Then you didn't fix it properly.

    And you should post your latest effort so we can see what you did.


    Before you write another program, read this
    A development process
    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.

  8. #8
    Registered User
    Join Date
    Dec 2012
    Posts
    5
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    void linreg(float *x,float *y,int n,float *m,float *b);
    int main(int argc, char *argv[])
    {
        int i; //year
        int j; //month
        int k; //date
        FILE *fpm;
        FILE *fpb;
        FILE *fp;
        char fn[100];
     
     
        //fpout = fopen("dataesout.txt","w");
        fpm = fopen("slopesout.txt","w");
        fpb = fopen("interceptsout.txt","w");
     
     
        for(i=2010; i<=2011; i++)
        {
            for(j=1; j<12; j++)
            {
                for(k=1; k<=31; k++)
                {
                    int z;
                    int n=0;
                    float x[5000];
                    float y[5000];
                    float m;
                    float b;
                    if(j==1)
                    {
                        sprintf(fn,"jan %d %d.dat",k,i);    // makes the file name into a string
                        fp = fopen(fn,"r");
                        z=0.0;
                        while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF)
                        {
                            z++;
                        }
                        n=z;
                        linreg(x,y,n,&m,&b);
                        //calculate m and b
                        fprintf(fpm,"%g\n",m);
                        //save each to a file
                        fprintf(fpb,"%g\n",b);
                        fclose(fp);
                    }
                    else if(j==2)
                    {
                        if(k<=28)
                        {
                            sprintf(fn,"feb %d %d.dat",k,i);
                            fp = fopen(fn,"r");
                            z=0.0;
                            while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF)
                            {
                                z++;
                            }
                            n=z;
                            linreg(x,y,n,&m,&b);
                            //calculate m and b
                            fprintf(fpm,"%g\n",m);
                            //save each to a file
                            fprintf(fpb,"%g\n",b);
                            fclose(fp);
                        }
    }
     
                        else if(j==3)
                        {
                            sprintf(fn,"mar %d %d.dat",k,i);
                            fp = fopen(fn,"r");
                            z=0.0;
                            while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF)
                            {
                                z++;
                            }
                            n=z;
                            linreg(x,y,n,&m,&b);
                            //calculate m and b
                            fprintf(fpm,"%g\n",m);
                            //save each to a file
                            fprintf(fpb,"%g\n",b);
                            fclose(fp);
                        }
     
     
     
     
                        else if(j==4)
                        {
                            if(k<=30)
                            {
                                sprintf(fn,"apr %d %d.dat",k,i);
                                fp = fopen(fn,"r");
                                z=0.0;
                                while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF)
                                {
                                    z++;
                                }
                                n=z;
                                linreg(x,y,n,&m,&b);
                                //calculate m and b
                                fprintf(fpm,"%g\n",m);
                                //save each to a file
                                fprintf(fpb,"%g\n",b);
                                fclose(fp);
                            }
     
     
                            else if(j==5)
                            {
                                sprintf(fn,"may %d %d.dat",k,i);
                                fp = fopen(fn,"r");
                                z=0.0;
                                while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF)
                                {
                                    z++;
                                }
                                n=z;
                                linreg(x,y,n,&m,&b);
                                //calculate m and b
                                fprintf(fpm,"%g\n",m);
                                //save each to a file
                                fprintf(fpb,"%g\n",b);
                                fclose(fp);
                            }
     
     
                            else if(j==6)
                            {
                                if(k<=30)
                                {
                                    sprintf(fn,"jun %d %d.dat",k,i);
                                    fp = fopen(fn,"r");
                                    z=0.0;
                                    while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF)
                                    {
                                        z++;
                                    }
                                    n=z;
                                    linreg(x,y,n,&m,&b);
                                    //calculate m and b
                                    fprintf(fpm,"%g\n",m);
                                    //save each to a file
                                    fprintf(fpb,"%g\n",b);
                                    fclose(fp);
                                }
    }
     
                                else if(j==7)
                                {
                                    sprintf(fn,"jul %d %d.dat",k,i);
                                    fp = fopen(fn,"r");
                                    z=0.0;
                                    while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF)
                                    {
                                        z++;
                                    }
                                    n=z;
                                    linreg(x,y,n,&m,&b);
                                    //calculate m and b
                                    fprintf(fpm,"%g\n",m);
                                    //save each to a file
                                    fprintf(fpb,"%g\n",b);
                                    fclose(fp);
                                }
     
     
                                else if(j==8)
                                {
                                    sprintf(fn,"aug %d %d.dat",k,i);
                                    fp = fopen(fn,"r");
                                    z=0.0;
                                    while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF)
                                    {
                                        z++;
                                    }
                                    n=z;
                                    linreg(x,y,n,&m,&b);
                                    //calculate m and b
                                    fprintf(fpm,"%g\n",m);
                                    //save each to a file
                                    fprintf(fpb,"%g\n",b);
                                    fclose(fp);
                                }
     
     
                                else if(j==9)
                                {
                                    if(k<=30)
                                    {
                                        sprintf(fn,"sep %d %d.dat",k,i);
                                        fp = fopen(fn,"r");
                                        z=0.0;
                                        while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF)
                                        {
                                            z++;
                                        }
                                        n=z;
                                        linreg(x,y,n,&m,&b);
                                        //calculate m and b
                                        fprintf(fpm,"%g\n",m);
                                        //save each to a file
                                        fprintf(fpb,"%g\n",b);
                                        fclose(fp);
                                    }
     
     
                                    else if(j==10)
                                    {
                                        sprintf(fn,"oct %d %d.dat",k,i);
                                        fp = fopen(fn,"r");
                                        z=0.0;
                                        while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF)
                                        {
                                            z++;
                                        }
                                        n=z;
                                        linreg(x,y,n,&m,&b);
                                        //calculate m and b
                                        fprintf(fpm,"%g\n",m);
                                        //save each to a file
                                        fprintf(fpb,"%g\n",b);
                                        fclose(fp);
                                    }
     
     
                                    else if(j==11)
                                    {
                                        if(k<=30)
                                        {
                                            sprintf(fn,"nov %d %d.dat",k,i);
                                            fp = fopen(fn,"r");
                                            z=0.0;
                                            while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF)
                                            {
                                                z++;
                                            }
                                            n=z;
                                            linreg(x,y,n,&m,&b);
                                            //calculate m and b
                                            fprintf(fpm,"%g\n",m);
                                            //save each to a file
                                            fprintf(fpb,"%g\n",b);
                                            fclose(fp);
                                        }
     
     
                                        else if(j==12)
                                        {
                                            sprintf(fn,"dec %d %d.dat",k,i);
                                            fp = fopen(fn,"r");
                                            z=0.0;
                                            while(fscanf(fp,"%g %g",&x[z],&y[z])!=EOF)
                                            {
                                                z++;
                                            }
                                            n=z;
                                            linreg(x,y,n,&m,&b);
                                            //calculate m and b
                                            fprintf(fpm,"%g\n",m);
                                            //save each to a file
                                            fprintf(fpb,"%g\n",b);
                                            fclose(fp);
                                        }
                                        //fprintf(fpout,"%d %d %d.dat\n",j,k,i);
                                    }
                                }
                            }
                        }
                    }
                    fclose(fpb);
                    fclose(fpm);
                    system("PAUSE");
                    return 0;
                }
     
     
     
     
                void linreg(float *x, float *y, int n, float *m, float *b)
                {
                    int p;
                    float sumx=0.0;
                    float sumy=0.0;
                    float sumxy = 0.0;
                    float sumxx=0.0;
                    float sum2x=0.0;
                    float sumyy=0.0;
                    float sum2y=0.0;
     
                     for(p=0; p<n; p++)
                    {
                        sumx = sumx + x[p];
                        sumy = sumy + y[p];
                        sumxy = sumxy + (x[p]*y[p]);
                        sumxx = sumxx + (x[p]*x[p]);
                        sum2x = sumx*sumx;
                        sumyy = sumyy + (y[p]*y[p]);
                        sum2y = sumy*sumy;
                    }
     
     
                    *m = (((n*sumxy)-(sumx*sumy))/((n*sumxx)-(sum2x)));
                    *b = ((sumy - ((*m)*sumx))/n);
                    return ;
                }

    this is the updated code. i think i am doing the the modularization wrong till, can you help me out?

  9. #9
    Registered User
    Join Date
    Dec 2012
    Posts
    5
    everything is working fine now!!! thank you for the help guys, i really appreciate it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comment each line and convert this C++ OOP code into C++ code.
    By Shayaan_Mustafa in forum C++ Programming
    Replies: 3
    Last Post: 01-03-2011, 01:23 PM
  2. Run command line code from within my c code.
    By baikal_m in forum C Programming
    Replies: 6
    Last Post: 01-28-2010, 03:58 PM
  3. Why do I need this line of code?
    By Nextstopearth in forum C Programming
    Replies: 10
    Last Post: 09-05-2008, 04:54 AM
  4. what does this line of code mean?
    By blindleaf in forum C Programming
    Replies: 4
    Last Post: 05-01-2003, 01:52 PM
  5. what does this line of code do?
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2002, 12:09 AM

Tags for this Thread