one small help

This is a discussion on one small help within the C Programming forums, part of the General Programming Boards category; Dear members, i need a small help in C programming.. I have a file with two columns column one starts ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    17

    one small help

    Dear members,
    i need a small help in C programming..
    I have a file with two columns
    column one starts from 0,1,2,3,.......may be up to 1000
    column two contains some numbers...may be integer and/or some numbers
    but the two columns are +ve...
    Now i need to change/multiply column one with a constant number, K, which may be up to 3 decimal places...and then divide by the last number (in this case 1000) from column one.
    then the second column need to be divided by a number M, through out...
    So i need to write the new values in a new file...
    its better for me give input and output file name from command and also the K and M...in this the 1000 shall be selected automatically...
    if anyone willing to help me...

    advance thanks,
    kumar

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,165
    What have you tried?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    read file using fgets in a loop like
    Code:
    while(fgets(buf, sizeof buf, fp))
    {
       /* process buf */
    }
    parse the buffer using scanf or strtol or some other method
    fill the array of numbers...
    calculate the last index in the first column

    then - use loop to write the result of calculation based on the array and stored index into the new file

    post your code attemp and ask about problems you failed to achive

    do one thing at a time - do not get to the next step till you successfully finishe the previos one
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    So what's your actual difficulty?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    17

    new to C

    I am new to C...
    i know only very little......
    can some one pls. write me the code??

    thanks
    kumar

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,165
    Kindly read about our homework policy.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    17
    I understand...this is not like a home work...all these days i use excell...but now i feel C would be more good...

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    17
    does anyone have bohariho's email id????

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Forget those guys... being so unkind and heartless to a poor student. With the cost of school ever increasing my heart goes out to you.

    But since I prefer PERL for this type of operation, you get what you get.

    Code:
    # may as well multiply by e  and pi :)
    $K = 2.718
    $M = 3.142
    
    open(NUMBERIN, "<mycolumns.txt")
    open(NUMBEROUT, ">mynewcolumns.txt")
    
    @lines = <NUMBERIN>
    @nums = split(/ /, $lines[-1])
    $N = $nums[0]
    
    foreach $line(@lines) {
      @nums = split(/ /, $line)
      $nums[0] *= $K / $N
      $nums[1] /= $M
      print NUMBEROUT "$nums[0] $nums[1]\n"
    }
    
    close(NUMBERIN)
    close(NUMBEROUT)
    I hope that helps.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by kumaravel_futu View Post
    I understand...this is not like a home work...all these days i use excell...but now i feel C would be more good...
    For this I think Excel would actually be the optimal choice. You can do stuff like this in C, but if all you need is the output, then Excel would be so much simpler...

  12. #12
    Registered User
    Join Date
    Sep 2007
    Posts
    17

    hi

    hi master thanks,,,
    anyways i write my own code and inform u..may be tomorrow i will do it...now 50% finished..

  13. #13
    Registered User
    Join Date
    Sep 2007
    Posts
    17

    yes

    Yes excel is better...for may complex calculation i do it in excel!!!but its time consuming..so if i have some C codes..then its really efficient...Also i am not a programmer.....yes i have time i can do like u guys...
    anyways tomorrow i will tell abt. my c codes
    kumar

  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Good luck buddy. As for the time element, consider this: In Excel the time consuming element is data entry (which you already have the table it seems) whereas to develop a program you still have the time consuming element plus developmental time. Which in this case is marginal since to write something according to that specification takes around a minute or less.

    I am not discouraging you from learning the trade. I just hope your rationale for picking it up isn't one that is susceptible to thinking there is little point in the pursuit.

    Keep the questions coming though pal. They are always welcomed and sometimes answered with less immediate sarcasm.

  15. #15
    Registered User
    Join Date
    Sep 2007
    Posts
    17

    Hi

    Hi now i created my code...but with lots of error..pls. correct me...at least now can anyone help me out...
    my input file looks like this:
    0 1212.23
    1 2323.45
    2 5985.555
    3 3778.7812
    4 45.56
    . .
    . .
    . .
    (N-1) .

    that is two columns...if the last number in 1st column is (N-1), then the total no. of points is (N)...hope u understand...Now i need to make the following conversion to column one:-
    2*V*(i-Z)/(N-1), where V = is some no. for e.g., 5.786, and Z = 456.5643356 and (N-1) is the last no. of column one or total no. of data points. And i=0,1,2,3,...(N-1).
    Also for the second column i have to do the following conversion:-
    (M*100/K), where M is the values in second columns (for e.g., 1212.23, 2323.45, etc.) and K is some number for e.g., 12312555.5555567.
    So in the command line i need to type the exe file name, input files name, output file, Z, V and K.
    Here my code:-
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    
    #define LNGMAX 1500
    
    int main(int argc, char **argv)
      {
      FILE *fpein,*fpaus;
      char *fs,datein[LNGMAX],dataus[LNGMAX],zeile[LNGMAX];
      double V,Z
      int ndat
    
      if (argc != 6)
        {
        puts(" !!! Aufruf: vel <datein> <dataus> <Z> <V> <K> "); 
        puts(" ... Z  : 57Fe's zero velocity channel");
        puts(" ... V  : Drive velocity in mm/s");
        return(-1);
        }
      strcpy(datein,argv[1]);
        if ( (fpein=fopen(datein,"r")) == NULL )
        { printf("\n !!! Error %s cannot open\n",datein);
          return(-1); }
      strcpy(dataus,argv[2]);
      if ( (fpaus=fopen(dataus,"w")) == NULL )
      { printf("\n !!! Error %s cannot open\n",dataus);
        return(-1); }
    
      sscanf(argv[3],"%lf",&Z);
      sscanf(argv[4],"%lf",&V);
      sscanf(argv[5],"%lf",&K);
    
    /* det. no. of data points */
    
      i=0;
        do {
    	fg=fgets(zeile,LNGMAX,fpein);
    	
            sscanf(zeile,"%lf" %lf\n",&x[i],&y[i]);
    
    	i++;
        } while (fg!=NULL);
        fclose(fpein);
        ndat = i-1;
    
    for i=0;i<=LNGMAX;i++)
    i=0;
    do {
      x[i]=2*V*(i-Z)/(ndat-1);
      i++;
          } while (fg!=NULL);
    do {
      i=0;
       y[i]=i/K;
    } while (fg!=NULL);
    
    fprintf ( fpaus," %lf  %lf\n", x[i],y[i]);
    fclose(fpaus);
    puts("....Done");
    return(0);
    }
    correct this c code..

    thanks
    Kumar.#

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to split long programe in small files
    By umeshjaviya in forum C Programming
    Replies: 11
    Last Post: 04-15-2008, 03:45 AM
  2. Small executables in VC++ 8
    By Bleech in forum Windows Programming
    Replies: 3
    Last Post: 06-20-2007, 09:28 AM
  3. want to make this small program...
    By psycho88 in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2005, 02:05 AM
  4. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 07:51 PM
  5. yhatzee, small straight
    By uglyjack in forum C++ Programming
    Replies: 2
    Last Post: 06-13-2002, 04:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21