Thread: array Calculations

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    4

    array Calculations

    I have a 2d array with chars and ints, what i want to do is to use the char to search and sum the ints the file looks something like this
    array example:
    Flour 1000 January
    Eggs 500 January
    Butter 500 January
    Sugar 600 January
    Flour 1200 February
    Eggs 500 February
    Butter 500 February
    Sugar 400 February
    Flour 2000 March
    Eggs 1000 March

    for example i want to use flour and for every occurrence of flour add all and then display the total using the strgcmp function

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by HolyMan View Post
    I have a 2d array with chars and ints
    In C, you can have an array with chars or ints, not both.

    what i want to do is to use the char to search and sum the ints
    [...]
    for example i want to use flour and for every occurrence of flour add all and then display the total using the strgcmp function
    There's a lot of ways you could do this, depending on your skill level, how efficient the search has to be, how much work you want to put in, and so on, but I think all of the (sane) ways will involve a struct:

    Code:
    struct eg {
        char name[64];
        int quantity;
    };
    You may or may not want a "month" field in there, depending on how you organize this.
    Last edited by MK27; 11-26-2011 at 12:15 PM.
    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

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    4
    this is what i have coded
    Code:
    #include <stdio.h>
    int main()
    {
        char ingred[50][50]= {" "};
        int amount[50]= {0};
        char month[50][50]= {" " };
    int i = 0;
    FILE *ptr;
    ptr = fopen ("OrderList.txt","r");
    printf ("\t\t\t\tINGREDIENTS\t\tAMOUNT \t\tMONTH");
    while(!feof(ptr))
    {
    
        fscanf (ptr,"%s%d%s",ingred[i], &amount[i], month[i]);    
        printf ("\t\t\t\t%s\t\t%d\t\t%s\n", ingred[i], amount[i], month[i]);
        
        i++;
    }
    what i want to do is is search the file for Eg: "FLOUR" and then calculate the running total for every occurrence of flour

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by HolyMan View Post
    what i want to do is is search the file for Eg: "FLOUR" and then calculate the running total for every occurrence of flour
    There is a difference between scanning a file for this, and scanning an array (or set of arrays) into which data from a file has been placed -- the later is what your first post implied, and it seems to be the direction you are going. But this implies a further purpose for the data -- eg, a user interface where search criteria can be provided during execution (otherwise, why keep all the data?).

    So you might want to be more clear about exactly what it is this program has to accomplish.

    Going with what you have, however, you can use the parallel arrays to get a total for each individual foodstuff. Just iterate thru "ingred", and for each category keep a += count from the parallel int array. If strcmp() on ingred[i] matches "flour", then amount[i] is what you want to add to the count.
    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
    Nov 2011
    Posts
    4
    yes that is exactly what i want to do getting the total for each food stuff. canyou give me an example how I would code what you said

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    29
    supppose n is the number of lines, and you have read in the data till now:
    Code:
        int n;    // suppose number of lines
        int cnt = 0;
        for (i = 0; i < n; ++i) {
            if (strcmp(ingred[i], "Flour") == 0) {
                cnt += amount[i];
            }
        }
        printf("total amount of flour: %d\n", cnt);
    cnt gives total amount for flour. Try adding more else if statements if you want to get the total amount for each food stuff. Tip: you can use an array for cnt then, with the number of different foods you have.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    16
    Quote Originally Posted by naturegirl View Post
    supppose n is the number of lines, and you have read in the data till now:
    Code:
        int n;    // suppose number of lines
        int cnt = 0;
        for (i = 0; i < n; ++i) {
            if (strcmp(ingred[i], "Flour") == 0) {
                cnt += amount[i];
            }
        }
        printf("total amount of flour: %d\n", cnt);
    cnt gives total amount for flour. Try adding more else if statements if you want to get the total amount for each food stuff. Tip: you can use an array for cnt then, with the number of different foods you have.
    Please explain why strcmp is opening with ingred[i] and not the filename?

    If I remember correctly the format for strcmp is
    Code:
    strcmp(filename, "string")
    ... or some sort of pointer...

  8. #8
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Quote Originally Posted by Spamtacus View Post
    Please explain why strcmp is opening with ingred[i] and not the filename?

    If I remember correctly the format for strcmp is
    Code:
    strcmp(filename, "string")
    ... or some sort of pointer...
    Errr, no. strcmp - C++ Reference

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Spamtacus View Post
    Please explain why strcmp is opening with ingred[i] and not the filename?

    If I remember correctly the format for strcmp is
    Code:
    strcmp(filename, "string")
    ... or some sort of pointer...

    Because Naturegirl said the data had already been read in from the file. That would normally use a 2D array of char, with each ingredient on a separate line:

    ingred[0] = "flour"
    ingred[1] = "sugar"
    ingred[2] = "salt"
    etc.

    so ingred[i] IS the pointer to the string of "flour" or "sugar", or "salt", etc.

    strcmp(), will have no problem with ingred[i].

    And that is OK for strcmp(), I'm not sure what Tclausex means.

  10. #10
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Quote Originally Posted by Adak View Post
    I'm not sure what Tclausex means.
    I guess I mean that strcmp() doesn't operate on files ...

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    34
    The entire solution for this problem would be great, im trying to do the same thing with my code..

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Kristyy_mariee View Post
    The entire solution for this problem would be great, im trying to do the same thing with my code..
    We're not here to give away free code...

    You need to get your thinking cap on and figure this stuff out for yourself.

  13. #13
    Registered User
    Join Date
    Nov 2011
    Posts
    4
    ok
    will try this and see how it works out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculations on an entered array
    By MV1 in forum C Programming
    Replies: 7
    Last Post: 03-10-2009, 10:28 AM
  2. Replies: 2
    Last Post: 11-19-2008, 02:36 PM
  3. Help with calculations
    By Taka in forum C Programming
    Replies: 14
    Last Post: 09-01-2007, 07:00 PM
  4. array calculations
    By trixxma in forum C Programming
    Replies: 8
    Last Post: 04-02-2006, 03:16 PM
  5. pi calculations
    By nbice in forum C++ Programming
    Replies: 7
    Last Post: 09-30-2002, 01:48 PM