Thread: Need Ideas!

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    39

    Post Need Ideas!

    Hi I have to code up this problem:

    I have a set of formulas ( stored in an array of strings) on whom i have to test if they are true at 100 different time points. i.e the array of strings is stored such that the smallest formula is stored as the last element and the next smaller the last but one etc.and to evaluate the larger the results of the smaller formulas are necessary.

    hence i have to loop through the time points and check if a formula is true and if it is true , then may be store up that it is true(may be maintain an int array as big as the array of strings and store 1 if formula is true or 0 if false) ( this will be used during evaluating the next set of bigger formulas)

    I dunno what is the datastructure i have to use, and how to go about coding this up. please give me some ideas.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Humans are generally rather lazy, and thus rather efficient. We just have a problem with remembering anything beyond about 7 "chunks" of data, at a time, without laboriously writing it down, getting it all organized so it can be found quickly, etc.

    That's why I'd recommend solving this with your program, in much the same way that you would solve it with paper and pencil. Writing out the steps you use, would be a good start at pseudo code for your program.

    Remember what the computers strengths are: sorting, searching (especially through a sorted list/queue/or array, comparing quantities, and organizing.

    Remember what the computers weaknesses are: "looking", determining "meaning" or "quality" (instead of quantity), and just good old "common sense".

    Using a parallel integer array sounds fine. For 3 items, I'd use a struct, (and an array of that struct), rather than using 3 parallel arrays, as a general rule.

    It sounds like a series of if statements will be needed, remember the else and else if parts of it. Maybe a switch statement would be useful as well. The "fall through" part of the switch statement can be used very effectively with some problems.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    You will probably need to use the string function strtok() to break down your formula strings into something that can be tested arithmetically. If you give a few examples then we could be more specific.

    Adak: switch is something that I have never been able to use effectively - what is meant by the fall through?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by KBriggs View Post
    what is meant by the fall through?
    Leaving the break statement off the end of a case statement in a cleverly thought out list of cases, I would guess, eg
    Code:
    switch (x) {
         case (5):  dolevel(5);
         case (4):  dolevel(4);
    etc
    So '5' will do all levels, '4' will do levels up to 4, '3' upto 3, etc.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    39

    Exclamation

    Hi ,

    please tell me if the approach is right.

    I create a Structure which has an int array as big as the array of strings, a int keeping an account of the time step, and a pointer.

    The pointer is for creating a linked list.

    So, I create a linked list of the timepoints.

    and fill the array of int corresponding to a formula to 1 if it is true at that time point.

    Can u tell me if this approach is right?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I wouldn't do it that way. Sounds too Rube Goldberg.

    If you have more data to organize, I'd suggest using a struct, and putting everything connected with one item, inside the struct:

    Code:
    struct mystruct:  //define the struct
       int myInt;
       time_t myTimeStamp; //don't know if time_t is right for you, however
       char myString[25];
       //etc.
    } oneStruct;  //make one instance of mystruct
    Then make an array of your struct. You can think of each struct, as an object.

    Fall through on a switch statement:

    It's sing along time!

    On the 12th day of Christmas, my true love gave to me:


    Code:
    /* switch.c
    Switch Statement Fall Through Example
    */
    
    #include <stdio.h>
    
    int main(void)  {
       int day;
       
       printf("\n\n\tWhich day of Christmas do you want to see presents for? ");
       scanf("%d", &day);   
       putchar('\n');
       switch(day)  {
          case 12: printf("\t12 drummers drumming\n");
          case 11: printf("\t11 pipers piping\n");
          case 10: printf("\t10 Lord a'leaping\n");
          case 9 : printf("\t9 ladies dancing\n");
          case 8: printf("\t8 maids a'milking\n");
          case 7: printf("\t7 swans a'swimming\n");
          case 6: printf("\t6 geese a'laying\n");
          case 5: printf("\tFive Golden Rings\n");
          case 4: printf("\t4 calling birds\n");
          case 3: printf("\t3 french hens\n");
          case 2: printf("\t2 turtle doves, and\n");
          case 1: printf("\ta partridge, in a pear tree\n");
       };
       printf("\n\n\t\t\t     Press Enter When Ready ");
       day = getchar(); day = getchar();
       return 0;
    
    }
    By stacking up the gifts in descending order of days, and using the switch statements "fall through" feature, the repetitive (or multi-function), code that would be needed, is neatly eliminated.
    Last edited by Adak; 07-02-2009 at 10:49 AM.

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    39
    Hi Adak,

    I think i should put my array of strings i.e char[][] into the struct right? also i think it has to bea an array of int?

    do you suggest that i switch based on the time point, the constarint here is that currently i know that there r 100 time ppoints, however there may be a case when i have 1000 time points as well.

    thanks for your help.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by doubty View Post
    Hi Adak,

    I think i should put my array of strings i.e char[][] into the struct right? also i think it has to bea an array of int?

    do you suggest that i switch based on the time point, the constarint here is that currently i know that there r 100 time ppoints, however there may be a case when i have 1000 time points as well.

    thanks for your help.
    I'm not making *any* detailed suggestions, because I haven't seen all the details of what you want to do.

    When you're first designing a program, you are well advised to *put off* the details, and concentrate on the overall program structure and logic. *THEN* after you have that right, go back and fill in your details.


    My first general suggestion, is to have your program mimic the way you would solve this problem, if you were doing it by hand. Write out the steps. Then make those steps into pseudo-code that are closer to what the computer program will need.

    Then make up your functions from the natural functional jobs you have in your pseudo code, (input, sorting, searching, computations on the data, output, etc.), and use your pseudo code as your "skeleton" for your code.

    My second general suggestion is, if you have more than 2 variables that need to be saved, for each item, then use a struct, instead of more parallel arrays.

    You need to start making a stab at this - show your pseudo code at least, and give an example - a detailed example, of the work you want done. Be specific - quantity, range, data types, analysis needed, etc.

    I don't want to see any more questions about this until you show some work, on the next post you make.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ideas, how do you get them? xD
    By Akkernight in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-22-2009, 03:53 AM
  2. cool ideas for a game
    By Shadow12345 in forum Game Programming
    Replies: 7
    Last Post: 05-18-2004, 08:37 PM
  3. Company Name Ideas
    By brunomiranda in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 12-16-2003, 05:15 PM
  4. idea's idea's idea's
    By mithrandir in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-29-2002, 12:30 AM
  5. Small app ideas
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 02-15-2002, 08:57 PM