Thread: Subtracting one column in a 2d array from another?

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

    Subtracting one column in a 2d array from another?

    I have a 2d array where I manage donations and requests for different foods. I output a menu to the user who picks the options to either make a donation, make a request, fulfill a request, or print a status report of all the donations and requests. What I'm having trouble with is the fulfilling requests option. When the user picks fulfill requests that means that the donations are supposed to be subtracted from the requests. For example if there are 10 donations for grains and 15 requests for grains then after picking fulfill request there should be 0 donations for grains and 5 requests for grains but I don't know how to make the program do that since the values are in a 5 row by 2 column array. The five rows correspond to the five foods and the two columns correspond to donations and requests, respectively. Anyone mind helping me out? Here's my code so far. Just disregard everything but the third case.
    Code:
     #include <stdio.h>int main(){
        int foodbank[5][2]= {
        {0,0},
        {0,0},
        {0,0},
        {0,0},
        {0,0}
        };
        int choice, donation, request, donation_type, request_type;
    
    
    
    
        printf("Welcome to the Food Bank Management Program!\n");
        printf("What 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);
    
    
        while(choice!= 5){
            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",&donation_type);
    
    
            if (donation_type == 0 ) {
                printf("How many would you like to enter?\n");
                scanf("%d",&donation);
                foodbank[0][0]+=donation;
                printf("Donation Added.\n");
                break;
            }
    
    
            else if (donation_type == 1 ) {
                printf("How many would you like to enter?\n");
                scanf("%d",&donation);
                foodbank[1][0]+=donation;
                printf("Donation Added.\n");
                break;
            }
    
    
            else if (donation_type == 2 ) {
                printf("How many would you like to enter?\n");
                scanf("%d",&donation);
                foodbank[2][0]+=donation;
                printf("Donation Added.\n");
                break;
            }
    
    
            else if (donation_type == 3 ) {
                printf("How many would you like to enter?\n");
                scanf("%d",&donation);
                foodbank[3][0]+=donation;
                printf("Donation Added.\n");
                break;
                }
    
    
            else if (donation_type == 4 )  {
                printf("How many would you like to enter?\n");
                scanf("%d",&donation);
                foodbank[4][0]+=donation;
                printf("Donation Added.\n");
                break;
            }
    
    
            else {
                printf("Invalid choice");
            }
            case 2: printf("What would you like to request?\n");
            printf("\t0. Protein\n");
            printf("\t1. Dairy\n");
            printf("\t2. Grains\n");
            printf("\t3. Vegetables\n");
            printf("\t4. Fruits\n");
            scanf("%d",&request_type);
    
    
    
    
    
    
               if (request_type == 0 ) {
                printf("How many would you like to request?\n");
                scanf("%d",&request);
                foodbank[0][1]+=request;
                printf("Request Added.\n");
                break;
            }
    
    
            else if (request_type == 1 ) {
                printf("How many would you like to request?\n");
                scanf("%d",&request);
                foodbank[1][1]+=request;
                printf("Request Added.\n");
                break;
            }
    
    
            else if (request_type == 2 ) {
                printf("How many would you like to request?\n");
                scanf("%d",&request);
                foodbank[2][1]+=request;
                printf("Request Added.\n");
                break;
            }
    
    
            else if (request_type == 3 ) {
                printf("How many would you like to request?\n");
                scanf("%d",&request);
                foodbank[3][1]+=request;
                printf("Request Added.\n");
                break;
                }
    
    
            else if (request_type == 4 )  {
                printf("How many would you like to request?\n");
                scanf("%d",&request);
                foodbank[4][1]+=request;
                printf("Request Added.\n");
                break;
            }
    
    
            else {
                printf("Invalid choice");
    
    
            }
    
    
            case 3: 
            
    
    
    
    
        }
    
    
        printf("What 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);
    }
    
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You figured out how to use +. How is - any different?

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    13
    I was just hoping there was a less repetitive way of subtracting that I could do because as you can see the way I added was very repetitive and probably isn't the most efficient way of doing it. Also I'm only supposed to subtract the donations by the requests only when a request for that food group has been made.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, yes, you do have a lot of repeated code that you could easily refactor. (Basically: if you see that you've typed the same code a bunch of times, but only changed one character, maybe you could bring that together into one piece of code where there's a variable involved.) But that's not really related to addition vs subtraction.

    "only when" just means "if".

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    13
    Haha, I just wished I knew what that variable was and how to use it. Sorry but would you mind maybe giving me another hint?

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    int choice is variable that you are using...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by zaihed13 View Post
    Haha, I just wished I knew what that variable was and how to use it. Sorry but would you mind maybe giving me another hint?
    Well, if you have request_type == 0 and request_type == 1 and request_type == 2 and request_type == 3 and request_type == 4, then maybe instead of writing code that uses 0,1,2,3,4 you could write code that uses request_type.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding a new column in array
    By Tomislav Pti in forum C Programming
    Replies: 3
    Last Post: 11-22-2012, 04:48 AM
  2. Replies: 6
    Last Post: 12-09-2010, 06:27 PM
  3. adding up array column total
    By dmckay in forum C Programming
    Replies: 1
    Last Post: 01-25-2010, 03:14 PM
  4. Reading / input column of numbers into an array
    By boyfarrell in forum C Programming
    Replies: 6
    Last Post: 08-28-2005, 10:24 AM
  5. Replies: 10
    Last Post: 07-13-2003, 01:48 PM

Tags for this Thread