Thread: Need help with arrays/two dimesional arrays

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

    Need help with arrays/two dimesional arrays

    Hello all,

    I've been writing a program for school for quite sometime now and havent been able to figure it out. The code is supposed to input and add numbers for donations and requests for a food management donation center. The professor said to use arrays but I really dont see where its fitting, please help anything will be greatly appreciated

    Alex
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int main() {
        int choice, donationchoice, protein, totalprotein, dairy, totaldairy, totalrequestfruits;
        int grains, totalgrains, vegetables, totalvegetables, fruits, totalfruits;
        int requestchoice, requestprotein, totalrequestprotein, requestdairy, totalrequestdairy;
        int requestgrains, totalrequestgrains, requestvegetables, totalrequestvegetables, requestfruits;
    
    printf("Welcome to the Food Bank Management Program!\n");
        while (choice !=5){
            printf("\nWhat would you like to do?\n");
            printf("\t1. - Enter a Donation\n");
            printf("\t2. - Enter a request\n");
            printf("\t3. - Fulfill Requests\n");
            printf("\t4. - Print status report\n");
            printf("\t5. - Exit\n");
            scanf("%d", &choice);
            switch(choice){
                case 1:
                    printf("What donation type would you like to enter?\n");
                    printf("\t0. Protein\n");
                    printf("\t1. Dairy\n");
                    printf("\t2. Grains\n");
                    printf("\t3. Vegetables\n");
                    printf("\t4. Fruits\n");
                    scanf("%d", &donationchoice);
                        switch (donationchoice !=5){
                            case 0:
                                printf("How many would you like to enter?");
                                scanf("%d", &protein);
                                totalprotein = totalprotein + protein;
                                printf("Donation Added.\n");
                                break;
                            case 1:
                                printf("How many would you like to enter?");
                                scanf("%d", &dairy);
                                totaldairy = totaldairy + dairy;
                                printf("Donation Added.\n");
                                break;
                            case 2:
                                printf("How many would you like to enter?");
                                scanf("%d", grains);
                                totalgrains = totalgrains + grains;
                                printf("Donation Added.\n");
                                break;
                            case 3:
                                printf("How many would you like to enter?");
                                scanf("%d", &vegetables);
                                totalvegetables = totalvegetables + vegetables;
                                printf("Donation Added.\n");
                                break;
                            case 4:
                                printf("How many would you like to enter?");
                                scanf("%d", &fruits);
                                totalfruits = totalfruits + fruits;
                                printf("Donation Added.\n");
                                break;
                        break;
                }//end of donation choice switch statement/ case 1 of original choice switch
    
                case 2:
                    switch(requestchoice){
                    case 0:
                        printf("How many would you like to request?");
                        scanf("%d", requestprotein);
                        totalrequestprotein += requestprotein;
                        printf("Request Added.\n");
                        break;
                    case 1:
                        printf("How many would you like to request?");
                        scanf("%d", requestdairy);
                        totalrequestdairy += requestdairy;
                        printf("Request Added.\n");
                        break;
                    case 2:
                        printf("How many would you like to request?");
                        scanf("%d", requestgrains);
                        totalrequestgrains += requestgrains;
                        printf("Request Added.\n");
                        break;
                    case 3:
                        printf("How many would you like to request?");
                        scanf("%d", requestvegetables);
                        totalrequestvegetables += requestvegetables;
                        printf("Request Added.\n");
                        break;
                    case 4:
                        printf("How many would you like to request?\n");
                        scanf("%d", requestfruits);
                        totalrequestfruits += requestfruits;
                        printf("Request Added.\n");
                        break;
                    break;
                    }//end request switch
                case 3:
                    /*
                    if (totalprotein>totalrequestprotein){
                        printf("Requests will be fufilled.\n");
                        totalprotein = totalprotein - totalrequestprotein;
                        totalrequestprotein = 0;
                    }
                    if (totaldairy>totalrequestdairy){
                        printf("Requests will be fufilled.\n");
                        totaldairy = totaldairy - totalrequestdairy;
                        totalrequestdairy = 0;
                    }
                    if (totalgrains>totalrequestgrains){
                        printf("Requests will be fufilled.\n");
                        totalgrains = totalgrains - totalrequestgrains;
                        totalrequestgrains = 0;
                    }
                    if (totalvegetables>totalrequestvegetables){
                        printf("Requests will be fufilled.\n");
                        totalvegetables = totalvegetables - totalrequestvegetables;
                        totalrequestvegetables = 0;
                    }
                    if (totalfruits>totalrequestfruits){
                        printf("Requests will be fufilled.\n");
                        totalfruits = totalfruits - totalrequestfruits;
                        totalrequestfruits = 0;
                    }
                    */
    
                break;
                case 4:
                    printf("Protein:     Donations:%d   Requests:%d\n", totalprotein, totalrequestprotein);
                    printf("Dairy:       Donations:%d   Requests:%d\n", totaldairy, totalrequestdairy);
                    printf("Grains:      Donations:%d   Requests:%d\n", totalgrains, totalrequestgrains);
                    printf("Vegetables:  Donations:%d   Requests:%d\n", totalvegetables, totalrequestvegetables);
                    printf("Fruits:      Donations:%d   Requests:%d\n", totalfruits, totalrequestfruits);
                break;
                case 5:
                    printf("Thank you for running our system!");
                    exit(0);
                    break;
                default:
                    printf("You've made an invalid selection.\n");
    
            }//end switch
        }//end while
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Where's your arrays at? Not that I could help anyway! but you haven't declared any arrays

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    17
    Yeah I don't know where to put them, where would be fitting do you think?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    At present you have variables in pairs like protein and totalprotein, then fruit and totalfruit, then grains and totalgrains. Even better, to decide which one of those to work with, you are using essentially the same code, depending on an integral value read from the user.

    Imagine you had 2000 different categories of food. Would you really want 4000 variables? No? Arrays can help rationalise such things.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Alex Coven View Post
    Yeah I don't know where to put them, where would be fitting do you think?
    The general idea is to allow arrays to help you organize your data more effectively. Here's a simple example:

    Code:
    #include <stdio.h>
    int main( void )
    {
        enum
        {
            dog_index, 
            bird_index,
            snake_index,
            total_indexes
        };
        int
            index,
            foot_count[ total_indexes ];
        const char* 
            animal_name[ total_indexes ];
        animal_name[ dog_index ] = "dog";
        foot_count[ dog_index ] = 4;
        animal_name[ bird_index ] = "bird";
        foot_count[ bird_index ] = 2;
        animal_name[ snake_index ] = "snake";
        foot_count[ snake_index ] = 0;
        for( index = 0; index < total_indexes; ++index )
            printf( "A %s has %d feet.\n", animal_name[ index ], foot_count[ index ] );
        return 0;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Sorry I haven't replied sooner I'm just starting to work on arrays myself I was wondering if grumpy was suggesting that u use two dimensional array for like things like fruit and totalFruit I was thinking that u should use a single dim for grains fruit protein dairy etc maybe if you stated the class problem someone might be able to better understand the problem (I'm not saying you don't understand the problem you might have missed something in the problem definition plus it might help someone understand it better) not that it would be me I probably won't understand better but, some guru might
    Last edited by artistunknown; 10-11-2013 at 01:45 AM.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by artistunknown View Post
    I was wondering if grumpy was suggesting that u use two dimensional array for like things like fruit and totalFruit
    I simply said it is possible to use arrays to rationalise the code given, and did not suggest anything about how many dimensions such arrays might have. That said, a two-dimensional array is one way of representing an array of arrays.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-11-2013, 10:57 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  5. separating line of arrays into array of arrays
    By robocop in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2001, 12:43 AM