Problem in simple code.

This is a discussion on Problem in simple code. within the C Programming forums, part of the General Programming Boards category; Hi, I am having a problem with this seemingly simple code. This is a program that I'm writing from a ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    100

    Problem in simple code.

    Hi,
    I am having a problem with this seemingly simple code. This is a program that I'm writing from a book. It asks the user to enter a zip code. If the zip code is already in the zip code array, it should increment the corresponding counter in another array. If it is not in the zip code array, add it to the END of the array and increment the counter. Then it displays the contents of the two arrays: the zip codes and the number of times each appeared.

    Because the zip codes have to be added to the end of the array, the first for loop starts with the highest number and decends to the lowest. The problem seems to be that although the codes are entered from the last to the first element of the array, they are displayed from first to last. This causes the program to display 0's or just garbage values for the first elements of both arrays. If I change the display loops to display in reverse there is no problem. Any help would be appreciated. (BTW, cant use zips starting with 0.)
    Code:
    #include <stdio.h>
    
    #define num_of_codes 5
    #define num_of_counters 5
    
    main()
    
    {
    	int zip_codes[] = {0,0,0,0,0},
    		counters[] = {0,0,0,0,0},
    	
    		codes,						
    		response=1,					
    		entered_code;
    
    
    	while(response)
    
    	{
    
    		printf("\nPlease enter a zip code: ");
    		scanf("%d" , &entered_code);
    
    		
    		for(codes=num_of_codes; codes>0; --codes)
    		
    			if(zip_codes[codes] == entered_code)
    				++counters[codes];
    		
    			
    			else
    																				
    				if(zip_codes[codes] == 0)
    					{
    						zip_codes[codes] = entered_code;
    						++counters[codes];
    					}
    					
    					
    						
    		
    			printf("\nwould you like to process another?(1 for yes, 0 for no): ");
    			scanf("%d" , &response);
    
    	}
    			
    	
    	for(codes=0; codes<5; ++codes)
    		printf("%7d" , zip_codes[codes]);
    			
    		printf("\n");
    				
    	for(codes=0; codes<5; ++codes)
    		printf("%7d" , counters[codes]);
    
    	    printf("\n");
    				
    }
    If I change the last two for loops to the values of the first:
    Code:
    for(codes=num_of_codes; codes>0; --codes)
    everything works.

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    Code:
    for(codes=num_of_codes; codes>0; --codes)
    		
    			if(zip_codes[codes] == entered_code)
    				++counters[codes];
    		
    			
    			else
    																				
    				if(zip_codes[codes] == 0)
    					{
    						zip_codes[codes] = entered_code;
    						++counters[codes];
    					}
    					
    					
    						
    		
    			printf("\nwould you like to process another?(1 for yes, 0 for no): ");
    			scanf("%d" , &response);
    u need to realize that the this bit of code will be executed 5 times for every response,that means that every 0 in the zip[codes] array will be replaced by what the user enters the first time,so after that no matter what the user enters it wont go into zipcodes array.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    100

    Question

    lol sorry. The prompt should be inside the loop:
    Code:
    for(codes=num_of_codes; codes>0; --codes)
    		{	
    		
    			printf("\nPlease enter a zip code: ");
    			scanf("%d" , &entered_code);
    
    			
    			if(zip_codes[codes] == entered_code)
    				++counters[codes];
    		
    			
    			else
    																				/*for(codes=num_of_codes; codes>0; --codes)*/
    				if(zip_codes[codes] == 0)
    					{
    						zip_codes[codes] = entered_code;
    						++counters[codes];
    					}
    					
    		}
    But even so, it doesn't work right:
    Code:
    Please enter a zip code: 12345
    
    Please enter a zip code: 54321
    
    Please enter a zip code: 98745
    
    Please enter a zip code: 45789
    
    Please enter a zip code: 66354
          0  66354  45789  98745  54321
          0      1      1      1      1
    The first entry 12345 should be the last one in the output, and the first element in both arrays are 0.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    This for loop is wrong:
    Code:
    		for(codes=num_of_codes; codes>0; --codes)
    That results in a buffer overflow. The first value of "codes" within the loop is 5, and the last value is 1. But arrays go from 0 to 4!
    That loop would need to read:
    Code:
    		for(codes=num_of_codes-1; codes>=0; --codes)
    The "increment" part of the for loop syntax is always run AFTER each iteration. Your later two for-loops are correct, although they should use the #defines defined earlier, instead of just having "5" hard-coded in them. While I'm on the subject, you're #defines should be uppercase too.
    Last edited by iMalc; 03-20-2006 at 12:46 AM.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    Thanks folks,
    If you cant tell I'm fairly new at this. Arrays sometimes throw me because of the offset thing. Have to pay more attention.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    Quote Originally Posted by qqqqxxxx
    and for checking u need to have nested loops.
    what did you mean by "checking"?

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    >and for checking u need to have nested loops.
    if your code is working the way u want it to,that doesnt matter then.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using java programs within C code
    By lemania in forum Linux Programming
    Replies: 1
    Last Post: 05-08-2005, 02:02 AM
  2. problem with some simple code =/
    By Josh Norton in forum C++ Programming
    Replies: 3
    Last Post: 02-23-2004, 05:27 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 12:30 PM
  5. Big Code, Little Problem
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2001, 05:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21