Thread: Trouble moving lines up in 2D array

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    Trouble moving lines up in 2D array

    I am currently working on a food bank project but am running into a little trouble. I cant correctly delete a row from a 2D array and move all the rows below it up in position. I am trying this in the "if(i ==3)" statement...Any help will be greatly appreciated...thankyou!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    
    int main () 
    {
    	char ditem[100][30], ritem[100][30];
    	char temp[30];
    	int dquantity[100], rquantity[100];
    	int i, j, k, l, m, z, y, tempq;
    	
    	i = 0;
    	j = 0;
    	m = 0;
    	k = -1;
    	tempq = 0;
    	
    	for(l=0;l < 100; l++)
    		{
    			rquantity[l] = 0;
    			dquantity[l] = 0;
    		}
        
    	do
    	{
    		printf("Welcome to the Food Bank Program.\n\n");
    		printf("1. Add a donation\n2. Add a request\n3. Fulfill a request\n4. Print status report\n5. Exit\n\n");
    		printf("Please enter a choice from the above: ");
    		scanf("%d", &i);
    		
    		while(i > 5 || i < 1)
    			{
    				printf("You have entered an incorrect choice, please enter a new choice.");
    				scanf("%d", &i);
    			}
    		
    		if(i == 1)
    			{
    				printf("Please enter the item name you would like to donate.\n");
    				scanf("%s", &temp);
    				
    				printf("Please enter the quantity you would like to donate.");
    				scanf("%d", &tempq);
    				
    				for(l=0; l<= j; l++)
    					{
    						if(strcmp(temp, ditem[l]) == 0)
    							{
    								dquantity[l] += tempq;
    								k = 0;
    							}
    							
    					}
    					
    				if(k != 0)
    					{
    						strcpy(ditem[j], temp);
    						dquantity[j] = tempq;
    						j++;
    					}
    				
    				printf("Donation added.\n");
    				for(l=0; l < j; l++)
    					printf("%s %d\n", ditem[l], dquantity[l]);
    			}
    		if(i == 2)
    			{
    				printf("Please enter the item name you would like to request.\n");
    				scanf("%s", &temp);
    				
    				printf("Please enter the quantity you would like to request.");
    				scanf("%d", &tempq);
    				
    				for(l=0; l<= m; l++)
    					{
    						if(strcmp(temp, ritem[l]) == 0)
    							{
    								rquantity[l] += tempq;
    								k = 0;
    							}
    					}
    					
    				if(k != 0)
    							{
    								strcpy(ritem[m], temp);
    								rquantity[m] = tempq;
    								m++;
    							}
    				
    				printf("Request added.\n");
    				for(l = 0; l < m; l++)
    					printf("%s %d\n", ritem[l], rquantity[l]);
    			}
    
    		if(i == 3)
    			{
    				for(z =0; z <= m; z++)
    				{
    					for(l = 0; l <= j; l++)
    					{
    						if(strcmp(ritem[z], ditem[l]) == 0)
    							{
    								dquantity[l] -= rquantity[z];
    								
    								if(dquantity[l] == 0)
    									{
    										for(y = l; y <= j; y++)
    											{
    												strcpy(ditem[y], ditem[y+1]);
    												dquantity[y] = dquantity[y+1];
    											}
    										j--;
    										
    										for(y = z; y <= m; y++)
    											{
    												strcpy(ritem[y], ritem[y+1]);
    												rquantity[y] = rquantity[y+1];
    											}
    										m--;
    									}
    								if(dquantity[l] < 0)
    									{
    										rquantity[z] = abs(dquantity[l]);
    										
    										for(y = l; y <= j; y++)
    											{
    												strcpy(ditem[y], ditem[y+1]);
    												dquantity[y] = dquantity[y+1];
    											}
    										j--;
    									}
    								if(dquantity[l] > 0)
    									{
    										for(y = z; y <= m; y++)
    											{
    												strcpy(ritem[y], ritem[y+1]);
    												rquantity[y] = rquantity[y+1];
    											}
    										m--;
    									}
    									
    							}
    					}
    				}
    			}
    		if(i == 4)
    			{
    				printf("Here's a list of Donations:\n");
    				
    				for(l = 0; l < j; l++)
    					printf("%s %d\n", ditem[l], dquantity[l]);
    				
    				printf("\nHere's a list of Requests:\n");
    					
    				for(l = 0; l < m; l++)
    					printf("%s %d\n", ritem[l], rquantity[l]);
    			}
    		
    			
    	} while(i != 5);
    	
        return 0;
    }

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Your indentation sucks big time and makes it absolutely impossible to read the code! 2-4 spaces per indentation level and braces are right underneath the keyword!

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    I hope this is a little better...I added some comments too

    Yeah I agree. It did that when I copied and pasted. Let me try re-posting it. I added a few comments so its easier to see what I'm trying to do. I know that there's easier ways to solving this problem but this is the way i need to do it to learn...


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    
    int main () 
    {
    	char ditem[100][30], ritem[100][30];
    	char temp[30];
    	int dquantity[100], rquantity[100];
    	int i, j, k, l, m, z, y, tempq;
    	
    	i = 0;
    	j = 0;
    	m = 0;
    	k = -1;
    	tempq = 0;
    	
    	//Ititialize quantity array for food request and donation to 0
    	for(l=0;l < 100; l++)
    	{
    		rquantity[l] = 0;
    		dquantity[l] = 0;
    	}
        
    	//Major loop that runs entire program
    	do
    	{	//Prompt user with menu choices
    		printf("Welcome to the Food Bank Program.\n\n");
    		printf("1. Add a donation\n2. Add a request\n3. Fulfill");
    		printf(" a request\n4. Print status report\n5. Exit\n\n");
    		printf("Please enter a choice from the above: ");
    		scanf("%d", &i);
    		
    		//Check for valid input
    		while(i > 5 || i < 1)
    		{
    			printf("You have entered an incorrect choice, please enter a new choice.");
    			scanf("%d", &i);
    		}
    		
    		//if statement that takes a donation
    		if(i == 1)
    		{	//Get temporary string name and quantity
    			printf("Please enter the item name you would like to donate.\n");
    			scanf("%s", &temp);
    				
    			printf("Please enter the quantity you would like to donate.");
    			scanf("%d", &tempq);
    			
    			//Check to see if the string is already in the 2D array list
    			for(l=0; l<= j; l++)
    			{
    				//If the items are the same then add the quantities together
    				if(strcmp(temp, ditem[l]) == 0)
    				{
    					dquantity[l] += tempq;
    					k = 0;
    				}
    			}
    			
    			//If item isnt found in 2D array then add to 2D array
    			if(k != 0)
    			{
    				strcpy(ditem[j], temp);
    				dquantity[j] = tempq;
    				j++;
    			}
    				
    			printf("Donation added.\n");
    			
    		}
    		
    		//Choice 2-Request from the donated
    		if(i == 2)
    		{
    			//Get user input for the request
    			printf("Please enter the item name you would like to request.\n");
    			scanf("%s", &temp);
    				
    			printf("Please enter the quantity you would like to request.");
    			scanf("%d", &tempq);
    			
    			//Look through request 2D array to update quantity requested
    			for(l=0; l<= m; l++)
    			{
    				if(strcmp(temp, ritem[l]) == 0)
    				{
    					rquantity[l] += tempq;
    					k = 0;
    				}
    			}
    			
    			//If not already requested then add to request 2D array
    			if(k != 0)
    			{
    				strcpy(ritem[m], temp);
    				rquantity[m] = tempq;
    				m++;
    			}
    				
    			printf("Request added.\n");
    		}
    
    		//Choice 3-Fulfill request
    		if(i == 3)
    		{
    			//for loop within for loop to compare all strings in both 2D arrays
    			for(z =0; z <= m; z++)
    			{
    				for(l = 0; l <= j; l++)
    				{
    					//if the strings are the same then fulfill the request
    					if(strcmp(ritem[z], ditem[l]) == 0)
    					{
    						dquantity[l] -= rquantity[z];
    						
    						//if the donation quantity is 0 then "delete" string from both
    						//arrays and move all strings below up a position
    						if(dquantity[l] == 0)
    						{
    							for(y = l; y <= j; y++)
    							{
    								strcpy(ditem[y], ditem[y+1]);
    								dquantity[y] = dquantity[y+1];
    							}
    							j--;
    										
    							for(y = z; y <= m; y++)
    							{
    								strcpy(ritem[y], ritem[y+1]);
    								rquantity[y] = rquantity[y+1];
    							}
    							m--;
    						}
    						//if there's more requests than donations: fulfill as many requests
    						//as possible, "delete" string from donation array
    						//(move all below string up a position),
    						//and update the request array.
    						if(dquantity[l] < 0)
    						{
    							rquantity[z] = abs(dquantity[l]);
    										
    							for(y = l; y <= j; y++)
    							{
    								strcpy(ditem[y], ditem[y+1]);
    								dquantity[y] = dquantity[y+1];
    							}
    							j--;
    						}
    						//if less requests than donations: update donations
    						//and "delete" request string and move all strings below up
    						if(dquantity[l] > 0)
    						{
    							for(y = z; y <= m; y++)
    							{
    								strcpy(ritem[y], ritem[y+1]);
    								rquantity[y] = rquantity[y+1];
    							}
    							m--;
    						}
    									
    					}
    				}
    			}
    		}
    		
    		//Print a list of all donations and requests
    		if(i == 4)
    		{
    			printf("Here's a list of Donations:\n");
    				
    			for(l = 0; l < j; l++)
    				printf("%s %d\n", ditem[l], dquantity[l]);
    				
    			printf("\nHere's a list of Requests:\n");
    					
    			for(l = 0; l < m; l++)
    				printf("%s %d\n", ritem[l], rquantity[l]);
    		}
    		
    			
    	} while(i != 5);
    	
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It looks a lot better - but still runs off my screen on the right hand side.

    May I make a suggestion?

    Set your editor up to have your tabs consist of 3 or 4 spaces. Spaces, not tabs. (the forum will work with tabs differently, and frequently make them 8 spaces or so, which is just way too much.

    Also, what's the problem with your program, now? You added comments, you improved your indenting, but what's the problems you see, now?

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    The problem is that my choice 3(the if(i ==3)) statement isnt set up correctly...It compiles and runs but doesnt give the correct output. I have tried to debug it piece by piece but cant seem to come to a conclusion. The gist of it is that I'm using a 2D array as a list or strings. Then I'm trying to remove a row(string) from the 2D array and then move all the rows below up one position to fill in the removed rows spot. ie-So I dont have a blank space in my list of strings. I hope this makes some sense

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Can you provide more detail for the algorithm you have coded for the i==3 case.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing to my 2D Array
    By qwertysingh in forum C Programming
    Replies: 0
    Last Post: 04-12-2009, 01:16 PM
  2. Dictionary into a 2d Char Array... Problems.
    By Muzzaro in forum C Programming
    Replies: 10
    Last Post: 12-02-2006, 12:34 PM
  3. passing/pointing to a 2d array
    By jamie85 in forum C Programming
    Replies: 7
    Last Post: 10-28-2005, 10:16 PM
  4. Text file to 2D Array PLEASE HELP!
    By lostboy101 in forum C Programming
    Replies: 0
    Last Post: 03-26-2002, 10:51 AM
  5. Comparing a 2d Array (newbie)
    By Cockney in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2001, 12:15 PM