1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How Do You Do This In C/C++? Will Pay For Help.

Discussion in 'C, C++, C#' started by Ignito, Jun 3, 2013.

  1. Ignito

    Ignito Newbie

    Joined:
    May 25, 2013
    Messages:
    43
    Likes Received:
    10
    Occupation:
    IM
    Location:
    BHW
    I'm currently seeking C/C++ advise, but may end up hiring a programmer. I'm attempting to take a text file as an input with, let's say, six columns and twenty rows and make various calculations based on the data in the text file.

    Is there a way to be able to access a specific column/row in the code and compare it to another? I'm basically trying to see how many numbers in, let's say, column two are +10 away from each other so if column two was 10 11 16 20 21 25 30 31 34 40 50, the program would give me the solution 10,20,30,40,50 and 11,21,31.

    Any help would be appreciated. If you know your stuff, I could always use another good programmer so feel free to PM me.
     
  2. escoentllc

    escoentllc Newbie

    Joined:
    Oct 14, 2009
    Messages:
    29
    Likes Received:
    12
    Post your input file and I can show you how to do it in PHP
     
  3. s0ap

    s0ap Executive VIP Jr. VIP Premium Member

    Joined:
    Sep 23, 2008
    Messages:
    230
    Likes Received:
    810
    Occupation:
    :] guess
    Location:
    Congo/DRC
    Yes it is pretty straightforward in C. Your better off with input in CSV instead of XLS/XLSX however.
     
    • Thanks Thanks x 1
  4. Ignito

    Ignito Newbie

    Joined:
    May 25, 2013
    Messages:
    43
    Likes Received:
    10
    Occupation:
    IM
    Location:
    BHW
    Does anyone have a helpful C resource? (Yes I've checked big G, but couldn't find anything on this specific task)
     
  5. Ignito

    Ignito Newbie

    Joined:
    May 25, 2013
    Messages:
    43
    Likes Received:
    10
    Occupation:
    IM
    Location:
    BHW
    Would it make most sense to use macros or templates to accomplish this?
     
    Last edited: Jun 3, 2013
  6. TheRevaN

    TheRevaN Newbie

    Joined:
    Jun 2, 2013
    Messages:
    18
    Likes Received:
    6
    That's very easy to do in c/c++ actually.

    I'm not gonna give you the answer but here is a hint:

    Create a two dimensional array with 6 columns and 20 rown like so:
    Code:
    int my_array[20][6];
    
    After this, read the file into the array and work with it. It's alot easier then to work directely with the file.

    Good luck!
     
    • Thanks Thanks x 1
  7. Ignito

    Ignito Newbie

    Joined:
    May 25, 2013
    Messages:
    43
    Likes Received:
    10
    Occupation:
    IM
    Location:
    BHW
    You say answer as if I'm doing a homework assignment...

    The only thing about storing the values in an array is that some of the columns are strings and some are ints.

    I also want to take the difference of two rows that have "a" in the same column (if that made any sense).

    x = doesn't matter

    x 10 x a x
    x 20 x b x
    x 30 x a x

    I want that to be able to print "a = 20" (as well as about 4 other calculations based on the data).

    I appreciate all of the tips, though I am willing to pay a programmer to just do it so if you have the skillset send me a message.
     
  8. s0ap

    s0ap Executive VIP Jr. VIP Premium Member

    Joined:
    Sep 23, 2008
    Messages:
    230
    Likes Received:
    810
    Occupation:
    :] guess
    Location:
    Congo/DRC
    First, convert to CSV and read the file into a buffer. Strtok once on this buffer with "\n" to give you discreet rows. Strtok on each of these new strings with "," to give you individual cells. Organize this in memory however you want; a two-dimensional array of dynamic char*'s or fixed character arrays is fine.

    Next, create a two-dimensional array of ints to store your values. Read each cell and do an integer compare against the first character of the array for an ASCII value of 48-57 (this is 0-9). If you have positive or negative signs you can test for those here as well. Any string with a number as the first letter is generally going to be a number, so take this string and pass it to atoi() to get an integer value out of it and put it in your 2d array in the correct location.

    Assuming you wrote your loops correctly, you should have a 2d array populated with the correct data. You can do whatever math you want on this now. Note that atio() is undefined on non-ints, so make sure you don't pass it a string. If you have floats or other non-int POD numeric types look into strtol() instead.
     
    • Thanks Thanks x 1
  9. lancis

    lancis Elite Member

    Joined:
    Jul 31, 2010
    Messages:
    1,632
    Likes Received:
    2,384
    Occupation:
    Entrepreneur
    Location:
    Milky Way
    Home Page:
    The most general approach is to use a variant structure in your case.

    Code:
    class variant
    {
         char type;
         union
         {
                int intv;
                char *strv;
         }
    }
    
    Then allocate a two dimensional array of such a structures. Wherever you need integer set the type to T_INT and fill the intv field. Wherever you need string set the type to T_STR and fill the strv value after allocating it.

    Apart from that you need a very basic CSV parser.
     
    • Thanks Thanks x 1
  10. Ignito

    Ignito Newbie

    Joined:
    May 25, 2013
    Messages:
    43
    Likes Received:
    10
    Occupation:
    IM
    Location:
    BHW
    I really appreciate the help. I sent you a PM, but initially do I getline of each line of my text and have each line a different structure under the same structure type? Then once I have all those, go through and put each into an array and then I'll be able to access the specific column I need to write a loop to see if there is a seies?
     
  11. Ignito

    Ignito Newbie

    Joined:
    May 25, 2013
    Messages:
    43
    Likes Received:
    10
    Occupation:
    IM
    Location:
    BHW
    I want to thank everyone for helping me out thus far. The way lancis suggested is the way I am going to try to go about doing this, though if someone is willing to code it then send me a message with your rate and how long it will take you. I still don't completely get reading a file into a strict line by line and then putting that into a 2d array in order to evaluate it. I will do my research and see what I can do bit again, if anyone has a free couple of hours I am willing to pay for this to get done.
     
  12. CSharp

    CSharp Newbie

    Joined:
    Jun 7, 2013
    Messages:
    39
    Likes Received:
    7
    Occupation:
    Developer, Entrepeneur
    Location:
    UK
    A different approach would be to put it into an actual DataTable...

    Code:
    DataTable TableDecode = New DataTable();
    // Create a number of columns matching the amount you need.
    // You could do this by using the String.Split function and split your 
    // data by the number of commas it has on each row.
    
    // PseudoCode (but should be close enough)
    // Read first line of the split string to create the headers
    int i = 0;
    foreach (string ColumName in SplitString array)
    {
        TableDecode.Columns.Add("ColumnNumber" + i, typeof(decimal));
        i++;
    }
    
    // Foreach row of text, splitstring and insert to datatable.
    
    // Now add the rows
    foreach (string CellValue in SplitString array)
    {
        TableDecode.Rows.Add(12,15,28,30,44,50);
    }
    
    //All thats left is figuring out if there are multiple of 10's or any other number
    //First count the number of columns and store the count in an integer
    //iterate through each row and column, if you find any number divisible by 10 
    //resulting in a whole number. Note that number and carry on iterating with a counter.
    //next time you hit a divisible number, you will have found the difference between.
    
    
    Any other questions give me a shout, if you can't do it and need to hire someone, I'm happy to do that for you too ;)
     
  13. qrazy

    qrazy Senior Member

    Joined:
    Mar 19, 2012
    Messages:
    1,111
    Likes Received:
    1,712
    Location:
    Banana Republic
    It'll be very easy if you use STL and vectors(vector of vector) to store the 2D data of rows & columns.
     
  14. mynameis940

    mynameis940 Power Member

    Joined:
    May 1, 2011
    Messages:
    520
    Likes Received:
    77
    It sounds like you may want to utilize this functionality to do more than just figure out if numbers in a row are a set distance from eachother, so I'll provide a more generalized solution.

    First create a 20x6 matrix of character pointers:

    Code:
    char *inputmatrix[20][6];

    Then load up the matrix with the values from the file. We first get the whole line from the file with fgets, from there we need to parse the line based on spaces using strtok. From there we'll need to create space for each element using malloc, copy in the value from strtok (because it gets overridden on the next call to strtok), and then store the pointer in our array:

    Code:
    char buffer[256];
    char *value;
    while(!feof(f)){
     if(!fgets(buffer,256,f))
       break;
     value = strtok(buffer," ");
     while(value != NULL){
       inputmatrix[currow][curcol] = (char*)malloc(strlen(value+1));
       memset(inputmatrix[currow][curcol],0,strlen(value+1));
       memcpy(inputmatrix[currow][curcol],value,strlen(value));
       curcol++;
       value = strtok(NULL," ");
     }
     currow++;
     curcol = 0;
    }
    Now that we've got a matrix of strings, we can go through and run any algorithm you want. For instance, to find out all the elements in a column that are +10 away from eachother we'll have to first determine if the element can be converted to an int using atoi, then compare it with the next int in the column and so on:


    Code:
    int curelement = -1, nextelement = -1;
    for(int i=0;i<3;i++){
      for(int j=0;j<6;j++){
        if((nextelement = atoi(inputmatrix[i][j])) != 0){
          if(nextelement - curelement == 10){
            printf("row %i,: %i,%i\n",i,curelement,nextelement);
        }
        curelement = nextelement;
      }
    }
    The above algorithm only works if the integers in the row are in ascending order; if not you have to take each integer and compare it with the rest of the integers in the row.
     
  15. sanmao

    sanmao Newbie

    Joined:
    May 28, 2013
    Messages:
    48
    Likes Received:
    5
    Home Page:
    Is it solved?