Thread: Finding differences around a point

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    12

    Finding differences around a point

    Hello all,
    I want to write a c program to find differences around a point. My data file has only one column containing positive integers. I want to write a new file as follows:
    If I input 48 then a new file with 2 columns should be written as:
    0 (47th integer/line vaue-49th)
    1 (46th-50th)
    2 (45th-51st)
    .
    .
    .
    until there is no pair to find difference.

    If I input 48.5, then,
    0 (48th integer vaue-49th)
    1 (47th-50th)
    2 (46th-51st)
    .
    .
    .

    NOTE: I will only input positive integer or half integer. After compiling the c program I prefer to run from command as follows:
    myprogram inputfilename outputfilename integerorhalfinteger

    I tried a c program, which gives the value of a specific point. If I input 512, the program correctly gives the value of 512th point/line. Beyond this I dont know how to proceed.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX 3000
    
    int main(int argc,char *argv[])
    {
        FILE *fs,*ft;
        char *foldpoint,line[MAX];
        int i;
        double fp;
    
        if(argc!=4)
        {
            puts("Improper number of arguments");
            exit(0);
        }
    
    fs=fopen(argv[1],"r");
    if(fs==NULL)
            {
            puts("Cannot open source file");
            exit(0);
            }
    
    ft=fopen(argv[2],"w");
    if(ft==NULL)
        {
        puts("Cannot open target file");
        fclose(fs);
        exit(0);
        }
    
    foldpoint=argv[3];
    sscanf(foldpoint,"%lf",&fp);
    printf("Folding point= %lf\n", fp);
    
    while (fgets(line, sizeof(line), fs))
                {i++;
                if(i == fp )
                {printf("%lf is %s",fp,line);   
                }
                }
       fclose(fs);
       fclose(ft);
    }
    This is not a homework case. It is for my personal work for manipulating a data.
    Thanks and regards, raja
    Last edited by Raja78; 02-27-2017 at 07:21 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    My first suggestion would be to learn how to indent code.
    Indent style - Wikipedia

    > if(i == fp )
    Not sure how you hope to compare half values here.


    > If I input 48 then a new file with 2 columns should be written as:
    So if the input file contains
    1
    2
    3
    4
    5
    6

    and you want the foldpoint at 3, then the output file would read this?
    3 4
    2 5
    1 6

    If not, then provide better actual examples of input and output.
    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.

  3. #3
    Registered User
    Join Date
    Feb 2017
    Posts
    12
    Hello Salem,
    thanks for looking into my code. I am providing you a better examaple here to help me.
    Suppose I have a data file containing only one column of positive integers.
    On opening my data file, it looks like the following:
    4589
    1459
    3628
    1574
    6369
    4895
    1113
    4777
    5557
    2111

    Now if I input 5, a new file with 2 columns should be created and on opening it should look as:
    0 (4th value - 6th value) /* i.e. 1574-4895 */
    1 (3th value - 7th value) /*i.e. 3628-1113 */
    2 (2th value - 8th value)
    3 (1st value - 9th value)

    write until there is no pairs to find the difference!!

    Now if I input 5.5, a new file with 2 columns should be created and on opening it should look as:
    0 (5th value-6th value) /*i.e. 6369-4895 */
    1 (4th value-7th value) /*i.e. 1574-1113 */
    2 (3th value-8th value)
    3 (2th value-9th value)
    4 (1st value-10th value)

    write until there is no pairs to find the difference!!
    I will only input positive integer or positive half-integer.
    Thanks again for your help.
    Raja.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Do you mean something like this:
    Code:
    #define MAX_SIZE 1000  // or whatever is appropriate
    ...
    
        double pivot;
        int low, high;
        int n[MAX_SIZE];
        int size;
        
        // read data file into n
        // and set size to it's size
        ...
    
        printf("Enter pivot: ");
        scanf("%lf", &pivot);
    
        if (pivot == (int)pivot) {
            low = (int)pivot - 1;
            high = low + 2;
        }
        else {
            low = (int)pivot;
            high = low + 1;
        }
    
        // adjust for 0-based indexing
        // (your input seems to be 1-based)
        low--;
        high--;
    
        int i = 0;
        while (low >= 0 && high < size) {
            printf("%d %d\n", i, n[low] - n[high]);
            low--;
            high++;
            i++;
        }

  5. #5
    Registered User
    Join Date
    Feb 2017
    Posts
    12
    Hello algorism,
    Please append to my previous program!
    I am getting error..after your suggestion I now modified the program, see below and correct:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX 1024
    int main(int argc,char *argv[])
    {    FILE *fs,*ft;
        char *foldpoint;
        int i,low,high,n[MAX],size;
        double fp;
        if(argc!=4)
        {puts("Improper number of arguments");
         exit(0);
        }
    fs=fopen(argv[1],"r");
    if(fs==NULL)
            {
            puts("Cannot open source file");
            exit(0);
            }
    ft=fopen(argv[2],"w");
    if(ft==NULL)
        {
        puts("Cannot open target file");
        fclose(fs);
        exit(0);
        }
    foldpoint=argv[3];
    sscanf(foldpoint,"%lf",&fp);
    printf("Folding point= %lf\n", fp);
    while (fgets(line, sizeof(line), fs))
                {i++;
                if(i == fp )
                {printf("%lf is %s",fp,line);   
                }
                }
    if(fp==(int)fp) {
            low = (int)fp-1;
            high = low + 2;
        }
        else {
            low = (int)fp;
            high = low + 1;
        }
     
        low--;
        high--;
    size=0;
            while (low >= 0 && high < size) {
            fprintf("%d %d\n", i, n[low] - n[high]);
            low--;
            high++;
            i++;
        }
    /* writing to file */     
       for (i=0;i<size;i++)
       {
       fprintf(ft, "%f     %f\n", i,n[low]-n[high]);
       }
     fclose(fs);
     fclose(ft);
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Please learn how to indent code before posting
    Indent style - Wikipedia

    It's simply too awful to look at.
    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.

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Can you not read the errors/warnings and fix them yourself?

    There are 3 errors/warnings (line numbers refer to your code above) :

    Line 29: You are using line without defining it anywhere

    Line 48: You are using fprintf without giving the first parameter as a FILE* (presumably ft).

    Line 56: You are using %f where you should be using %d. Although this bit of code is not needed and won't work anyway.

    Also, on line 46 you are setting size to 0 (!) where you should be setting i to 0.

    Your biggest problem at the moment is that you seem to expect the n array to magically fill up with the values in your file. That won't happen. You actually have to read them in yourself. Your fgets loop from 29 to 34 is printing out the fold point but not reading the values into n.

    BTW, I won't read your code any more if it's presented in this arbitrary "style". If you expect other people to read your code you need to show them the respect of presenting it in a sane manner.

  8. #8
    Registered User
    Join Date
    Feb 2017
    Posts
    12
    Hello algorism,
    I again made some changes, please look into it for any problems. Please help me to correct the lines, as you said in the last..printing out the fold point but not reading the values into n?? I think something to do with scanf..but really dont know! The following code is now compile with no error..But nothing is inside in the written file.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX 1024
    int main(int argc,char *argv[])
    {
      FILE *fs,*ft;
      char *foldpoint,line[MAX];
      int i,low,high,n[MAX],size;
      double fp;
      
      if(argc!=4) {
      puts("Improper number of arguments");
      exit(0);
      }
      fs=fopen(argv[1],"r");
      if(fs==NULL) {
      puts("Cannot open source file");
      exit(0);
      }
      ft=fopen(argv[2],"w");
      if(ft==NULL) {
      puts("Cannot open target file");
      fclose(fs);
      exit(0);
      }
    
      foldpoint=argv[3];
      sscanf(foldpoint,"%lf",&fp);
      printf("Folding point= %lf\n", fp);
    
      while  (fgets(line, sizeof(line), fs)) {
        i++;
        if(i == fp ) {
        printf("%lf is %s",fp,line);
        }         
      }
      if(fp==(int)fp) {
      low = (int)fp-1;
      high = low + 2;
      }
       else {
       low = (int)fp;
       high = low + 1;
       }
      
      low--;
      high--;
      i=0;
      while (low >= 0 && high < size) {
        fprintf(ft, "%d  %d\n", i,n[low]-n[high]);
        low--;
        high++;
        i++;
      }
      
    fclose(fs);
    fclose(ft);
    }
    Thanks and regards, raj
    Last edited by Raja78; 02-27-2017 at 07:28 PM.

  9. #9
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Maybe something like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX 1024
    
    int main(int argc, char *argv[])
    {
      FILE *fs, *ft;
      char *foldpoint, line[MAX];
      int i, low, high, n[MAX], size;
      double fp;
       
      if (argc != 4) {
        puts("Improper number of arguments");
        exit(0);
      }
    
      fs = fopen(argv[1], "r");
      if (fs == NULL) {
        puts("Cannot open source file");
        exit(0);
      }
    
      ft = fopen(argv[2], "w");
      if (ft == NULL) {
        puts("Cannot open target file");
        fclose(fs);
        exit(0);
      }
     
      foldpoint = argv[3];
      sscanf(foldpoint, "%lf", &fp);
      printf("Folding point= %lf\n", fp);
     
      i = 0;
      while (fgets(line, sizeof(line), fs)) {
        sscanf(line, "%d", &n[i]);
        i++;
        if (i == (int)fp) {
          printf("%lf is %s", fp, line);
        }
      }
      size = i;
    
      if (fp == (int)fp) {
        low = (int)fp - 1;
        high = low + 2;
      }
      else {
        low = (int)fp;
        high = low + 1;
      }
    
      low--;
      high--;
    
      i = 0;
      while (low >= 0 && high < size) {
        fprintf(ft, "%d  %d\n", i, n[low] - n[high]);
        low--;
        high++;
        i++;
      }
    
      fclose(fs);
      fclose(ft);
      
      return 0;
    }

  10. #10
    Registered User
    Join Date
    Feb 2017
    Posts
    12
    Hello algorism,
    Thank you! You solved my problem. Now I have to find the sum of all in the second column and it should be displayed on screen. Then the line 59 will be replaced (note: +) with
    Code:
    fprintf(ft, "%d  %d\n", i, n[low] + n[high]);
    I hope I can manage and succeed. If not I may post question in this thread.
    Once again thank you.
    Regards, Raja.
    Last edited by Raja78; 02-28-2017 at 03:59 AM.

  11. #11
    Registered User
    Join Date
    Feb 2017
    Posts
    12
    Hello algorism,
    I have written the same code with a small change, just included 'sum', in the second 'while' statement. Actually I wanted to write a file not with differences, but with its addition around the fp. But the sum of the squares of the differences around the fp should be displayed on the screen. The code compiles error free and gives the correct and expected result (I think). I made a small calculation using excel and it seems it is correct. It will be nice if you have a look at it for any errors.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX 10000
     
    int main(int argc, char *argv[])
    {
      FILE *fs, *ft;
      char *foldpoint, line[MAX];
      int i, low, high, n[MAX], size,sum;
      double fp;
        
      if (argc != 4) {
        puts("Improper number of arguments");
        exit(0);
      }
    
      fs = fopen(argv[1], "r");
      if (fs == NULL) {
        puts("Cannot open source file");
        exit(0);
      }
    
      ft = fopen(argv[2], "w");
      if (ft == NULL) {
        puts("Cannot open target file");
        fclose(fs);
        exit(0);
      }
    
      foldpoint = argv[3];
      sscanf(foldpoint, "%lf", &fp);
      printf("Folding point= %lf\n", fp);
      
      i = 0;
      while (fgets(line, sizeof(line), fs)) {
        sscanf(line, "%d", &n[i]);
        i++;
        if (i == (int)fp) {
          printf("%lf is %s", fp, line);
        }
      }
    
      size = i;
      if (fp == (int)fp) {
        low = (int)fp - 1;
        high = low + 2;
      }
       else {
        low = (int)fp;
        high = low + 1;
       }
    
      sum=0;
      low--;
      high--;
      i = 0;
      while (low >= 0 && high < size) {
        /* sscanf(line,"%d %d", &i, &n[i] ); */
        fprintf(ft, "%d  %d\n", i, (n[low]+n[high]));
        /* printf("%d  %d\n", i, (n[low]-n[high])*(n[low]-n[high])); */
        sum=sum+((n[low]-n[high])*(n[low]-n[high]));
        low--;
        high++;
        i++;
      }
    
    printf("Sum of least sqaures= %d\n", sum);
      
    fclose(fs);
    fclose(ft);
    
    return 0;
    }
    Thanks and regards,
    Raja.

  12. #12
    Registered User
    Join Date
    Feb 2017
    Posts
    12
    Hello algorism,
    I want to divide the first column by the number of data points, i. Suppose whn you run the program it shows the number of data point on screen, ref. line 68. I am confused of how to substitute '?' by i in line 61.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX 10000
      
    int main(int argc, char *argv[])
    {
      FILE *fs, *ft;
      char *foldpoint, line[MAX];
      int i, low, high, n[MAX], size,sum;
      double fp,v;
         
      if (argc != 5) {
        puts("Improper number of arguments\nType rawfilename foldedfilename foldingpoint velocity");
        exit(0);
      }
     
      fs = fopen(argv[1], "r");
      if (fs == NULL) {
        puts("Cannot open source file");
        exit(0);
      }
     
      ft = fopen(argv[2], "w");
      if (ft == NULL) {
        puts("Cannot open target file");
        exit(0);
      }
     
      foldpoint = argv[3];
      sscanf(foldpoint, "%lf", &fp);
      printf("Folding point= %lf\n", fp);
      
      sscanf(argv[4], "%lf", &v);
      printf("Drive velocity= %lf\n", v);
         
      i = 0;
      while (fgets(line, sizeof(line), fs)) {
        sscanf(line, "%d", &n[i]);
        i++;
        if (i == (int)fp) {
          printf("%lf is %s", fp, line);
        }
      }
     
      size = i;
      if (fp == (int)fp) {
        low = (int)fp;
        high = low;
      }
       else {
        low = (int)fp;
        high = low + 1;
       }
     
      sum=0;
      low--;
      high--;
      i = 0;
       while (low >= 0 && high < size) {
        /* sscanf(line,"%d %d", &i, &n[i] ); */
        fprintf(ft, "%lf  %d\n", (2*i)*v/?, (n[low]+n[high]));
        /* printf("%d  %d\n", i, (n[low]-n[high])*(n[low]-n[high])); */
        sum=sum+((n[low]-n[high])*(n[low]-n[high]));
        low--;
        high++;
        i++; 
     }
    printf("data points= %d\n", i);
    printf("Sum of least sqaures= %d\n", sum);
       
    fclose(fs);
    fclose(ft);
     
    return 0;
    }
    Thanks again.
    Regads, raja.
    Last edited by Raja78; 03-10-2017 at 10:06 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. finding about maximum or minimum point
    By Ph0x in forum C Programming
    Replies: 4
    Last Post: 10-29-2014, 02:44 PM
  2. finding boiling point need help!!!
    By muradyan14 in forum C Programming
    Replies: 12
    Last Post: 02-18-2013, 04:38 AM
  3. Replies: 2
    Last Post: 04-27-2011, 04:34 AM
  4. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 11:34 PM
  5. Finding a point within a circle
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-23-2001, 08:34 PM

Tags for this Thread