Thread: charactor selection from cmd

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

    charactor selection from cmd

    Dear All,
    after compiling, I run the program as:
    program.exe infile outfile foldpoint vel base. I.e. I type 6 arguments.
    The program works good. Now I want to make some modifications and dont know how to do it.
    I want to make 1 extra argument (it can be either 't' or 's'), so that I type
    program.exe infile outfile foldpoint vel base t or s.
    If I type t, then in the following code (line 86), fprintf should be executed.
    If I type s, then in the following code (line 87), /* fprintf...*/ should be executed. Please help me how can I do it?
    Another help (but not important!):
    In the program dp is the integer value obtained from line 70. And this integer value is used in fprintf (refer line 86 or 87). I cannot use this value directly without including lines 73-80. So I need to repeat the lines 48-56 two times. The lines 48-56 and 73-80 are same. Are there any way to avoid 73-80? So the program looks more compact.
    cos is cosine! 3.14 is pi, instead of using 3.14 I want to declare pi constant efficiently in c program.
    For this C codes I was actually interested to make my own program (Creating own application). But it seems it is difficult and I dropped my plan.

    My code:
    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,dp;
      double fp,v,b;
         
      if (argc != 6) {
        puts("Improper number of arguments\nType rawfilename outputfilename foldingpoint velocity baseline");
        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);
         
      sscanf(argv[5], "%lf", &b);
      printf("Base line counts= %lf\n", b);
         
      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) {
        sum=sum+((n[low]-n[high])*(n[low]-n[high]));
        i++;
        low--;
        high++;
        dp=i;
      }
        
      printf("Number of data points= %d\n", dp);
      printf("Sum of the sqaured differences= %d\n", sum);
     
      if (fp == (int)fp) {
        low = (int)fp;
        high = low;
      }
      else {
       low = (int)fp;
       high = low + 1;
      }
      
      low--;
      high--;
      i = 0;
      while (low >= 0 && high < size) {
       fprintf(ft, "%lf  %lf\n", ((2*i)-dp+1)*v/(dp-1), (n[low]+n[high])*100/b);
    /*fprintf(ft, "%lf  %lf\n", v*cos(2*i*3.14/(dp-1)), (n[low]+n[high])*100/b);*/
       i++;
       low--;
       high++;
      }
      
    fclose(fs);
    fclose(ft);
     
    return 0;
    }
    Thanks for your help.
    Cheers,
    Raja.
    Last edited by Raja78; 03-28-2017 at 04:49 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,110
    Well your question illustrates perfectly the down-side of begging others to do all your work.
    Finding differences around a point
    The begging never stops, because you never learn anything.

    > The program works good. Now I want to make some modifications and dont know how to do it.
    > I want to make 1 extra argument (it can be either 't' or 's'), so that I type
    > program.exe infile outfile foldpoint vel base t or s.
    Good, print off the program on some paper, use a highlight pen to colour in all the occurrences of 'argc' and 'argv', then try and figure it out.
    Make an attempt.


    > Are there any way to avoid 73-80? So the program looks more compact.
    Sure, do something like
    int lowsave = low;

    // mess around with low

    // get back the original
    low = lowsave;



    > 3.14 is pi, instead of using 3.14 I want to declare pi constant efficiently in c program.
    Well you (well, someone else - you failed to understand it) used #define.
    Perhaps you could investigate and use the same idea.
    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
    I tried
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define MAX 10000
    #define PI 3.14
      
    int main(int argc, char *argv[])
    {
      FILE *fs, *ft;
      char *foldpoint, line[MAX],m,t;
      int i, low, high, n[MAX], size,sum,dp;
      double fp,v,b;
         
      if (argc != 7) {
        puts("Improper number of arguments\nType rawfilename outputfilename foldingpoint velocity baseline t(or)s");
        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);
         
      sscanf(argv[5], "%lf", &b);
      printf("Base line counts= %lf\n", b);
      
      sscanf(argv[6], "%c", &m);
      printf("Source motion= %c\n", m);
         
      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) {
        sum=sum+((n[low]-n[high])*(n[low]-n[high]));
        i++;
        low--;
        high++;
        dp=i;
      }
        
      printf("Number of data points= %d\n", dp);
      printf("Sum of the sqaured differences= %d\n", sum);
     
      if (fp == (int)fp) {
        low = (int)fp;
        high = low;
      }
      else {
       low = (int)fp;
       high = low + 1;
      }
      
      low--;
      high--;
      i = 0;
      while (low >= 0 && high < size) {
      if (m=t){
       fprintf(ft, "%lf  %lf\n", ((2*i)-dp+1)*v/(dp-1), (n[low]+n[high])*100/b);
       i++;
       low--;
       high++;
      }
      else {
       fprintf(ft, "%lf  %lf\n", v*cos(i*PI/(dp-1)), (n[low]+n[high])*100/b);
       i++;
       low--;
       high++;
      }}
      
    fclose(fs);
    fclose(ft);
     
    return 0;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,110
    Not bad - see what you can achieve if you try for yourself first.

    > if (m=t)
    What's the difference between = and == ?
    What's the difference between m == t and m == 't' ?

    Now that you've defined PI, how about adding a few more significant digits?
    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.

  5. #5
    Registered User
    Join Date
    Feb 2017
    Posts
    12
    Hello Salem,
    after your tips, I understand = and == and 't' and improved with if, else if and else command. So that the code now is very strict; only s or t in cmd works. Also used M_PI without declaring/define PI value (but I prefer to use define PI and use it, because of compilation problems).
    But I really dont understand
    PHP Code:
    int lowsave low;
    // mess around with low
    // get back the original
    low lowsave
    so that the repeated lines 87-94 can be removed
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define MAX 10000
     
    int main(int argc, char *argv[])
    {
      FILE *fs, *ft;
      char *foldpoint, line[MAX],m,t;
      int i, low, high, n[MAX], size,sum,dp;
      double fp,v,b;
         
      if (argc != 7) {
        puts("Improper number of arguments\nType rawfilename outputfilename foldingpoint velocity baseline t(or)s");
        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);
         
      sscanf(argv[5], "%lf", &b);
      printf("Base line counts= %lf\n", b);
      
      sscanf(argv[6], "%c", &m);{
       if (m=='t') {
         printf("Source motion= Triangular/Constant acceleration motion\n");
         }
       else if (m=='s') {
         printf("Source motion= Sinus motion\n");
         }
       else {
         puts("Type either t or s; t for triangular/constant acceleration motion,\ns for sine motion");
         exit(0);
         }
      }
         
      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) {
        sum=sum+((n[low]-n[high])*(n[low]-n[high]));
        i++;
        low--;
        high++;
        dp=i;
      }
        
      printf("Number of data points= %d\n", dp);
      printf("Sum of the sqaured differences= %d\n", sum);
     
      if (fp == (int)fp) {
        low = (int)fp;
        high = low;
      }
      else {
       low = (int)fp;
       high = low + 1;
      }
     
      low--;
      high--;
      i = 0;
      while (low >= 0 && high < size) {
        if (m=='t'){
         fprintf(ft, "%lf  %lf\n", ((2*i)-dp+1)*v/(dp-1), (n[low]+n[high])*100/b);
         i++;
         low--;
         high++;
        }
        else {
         fprintf(ft, "%lf  %lf\n", v*cos(i*M_PI/(dp-1)), (n[low]+n[high])*100/b);
         i++;
         low--;
         high++;
       }
      }
     
    fclose(fs);
    fclose(ft);
     
    return 0;
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,110
    After line 70, you do
    lowsave = low;

    Instead of lines 87 to 94, you do
    low = lowsave;
    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
    Feb 2017
    Posts
    12
    Hello Salem,
    Thanks! I tried using you lowsave options and it did not work. Only first 2 rows were computed. Then I included printf statements after line 70 and after 87 to know what is happening (I did this after deleting 87 to 94). Then looked at the repeated lines 87-94 or 62-70. Obviously I forgot the increment part and so introduced highsave option with printf statements..Now I hope it is working perfect.
    In the above c code I included the following after 70:
    lowsave = low;
    highsave = high;
    and instead of lines 87 to 94, I included:
    low = lowsave;
    high= highsave;
    Now the code producing the right output hopefully.
    Cheers,
    Raja. (in case of any logical errors if you see, inform me)
    Last edited by Raja78; 03-29-2017 at 03:18 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. marking a selection
    By megafiddle in forum General Discussions
    Replies: 2
    Last Post: 08-22-2011, 08:36 PM
  2. The 2d selection sort
    By Techgique in forum C++ Programming
    Replies: 3
    Last Post: 08-12-2011, 11:05 PM
  3. OpenGL selection?
    By sirSolarius in forum Game Programming
    Replies: 0
    Last Post: 12-05-2003, 08:24 PM
  4. charactor count wthout getche function..?
    By gardenair in forum C Programming
    Replies: 3
    Last Post: 05-18-2003, 04:04 AM
  5. scanf a null charactor
    By toad in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2002, 05:20 AM

Tags for this Thread