Thread: logica question

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    52

    logica question

    Hi guys! I got a simple problem but I dont know whats wrong in my code.
    The situation is:
    I have an array of integers:
    int rptds[] { 12, 32, 40, 12, 35, 95, 32, 12}; (any else)
    Then I want to overwrite the repeated numbers with the next:
    12, 32, 40, 12, 35, 95, 32, 12
    12, 32, 40, 35, 95

    look the code:

    Code:
    int a, b, c, d;
    a = b = c = d = 0;
    
    while (rptds[a++]);
    a--;
    
    for (b=0; b<=a; b++)
    {
    	for (c=b+1; c<=a; c++)
    	{
    		if (rptds[b]==rptds[c])
    		{
    			for (d=c; d<a; d++)
    				rptds[d]=rptds[d+1];
    		}
    	}
    }
    thanks for any help

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    The code looks fine. The rptds[] array should be "terminated" with a 0, which you didn't post. Other than that, you will have to be more specific about the problem you are having.

    gg

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    52
    the problem is that in some cases it doesn't overwrite repeated numbers and in others it overwrite non-repeated numbers with repeated ones. this code isn't working properly.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Post an rptds[] array that doesn't work.

    gg

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    When calculating the size of an array, its safer to use
    Code:
    a = sizeof( rptds ) / sizeof( int ) - 1;
    Also
    Code:
    for (d=c; d<a; d++)
    	rptds[d]=rptds[d+1];
    a--;
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >while (rptds[a++]);

    If you want to use such like this, you need to put a terminating symbol in your array. The while-loop ends when the condition is false, in this case the while-loop will end if rptds[i] == 0 for a certain i.

    Also note that in your for-loops, the conditions should be < a, instead of <= a. This is because you start counting at 0. In your code the loop runs from 0 to a, which implies an array length of a+1, but it the array has length a.

    If I understand you correctly, you want to remove the double values and shift the array to the left when found. I have not tested the following:

    Code:
    for (i = 0; i < length; i++)
    {
      // check for duplicates
      for (j = i; j < length; j++)
      {
        if (array [i] == array [j])
        {
          // duplicate found, shift array to left
          for (k = j; j < length; k++)
          {
            array [k] = array [k+1];
          }
        }
      }
    }
    You could make the algorithm more faster by realising that shifting is only needed until the terminating symbol is found.

  7. #7
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    well, my way to do it is stupid but...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
    	int arr[7] = {11,25,55,33,11,27,25};
    	int i,j;
    
    	for (i=0; i<7; ++i)
    		for (j=i+1; j<7; ++j) //I think here I explode the buffer
    			if (arr[i] == arr[j])
    				arr[j] = 0;
    
    	for (i=0; i<7; ++i) {
    	 	if (arr[i] != 0)
    		printf("%d ",arr[i]);
    	}
    }

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Also note that in your for-loops, the conditions should be < a, instead of <= a.
    Wrong! "while (rptds[a++]);a--;" will put 'a' on the index of rptds[] that contains 0, and this assumption is used in the remainder of the code.

    Like I said, I don't see anything wrong with the code you originally posted, ipe (aside from a few optimizations like the one Xsquared posted).

    gg

  9. #9
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    I thought a was the length of the array. My mistake. Anyway, this is wrong:while (rptds[a++]);, because the array has no termination symbol. It goes outside the array range and then behaviour is undefined.

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    52
    thank you guys!!

    XSqaured:

    thank you. was missing a--!

    a = sizeof( rptds ) / sizeof( int ) - 1;
    pretty one! it may be faster

  11. #11
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    a-- will optimize the code, but it won't fix it.
    Try this array, it should clear up why things aren't working as you (and I originally) expected:
    Code:
    int rptds[] = {12, 12, 12, 12, 12, 12, 12, 12, 12, 0};
    Give another post if you are still stuck.

    gg

  12. #12
    Registered User
    Join Date
    Jan 2003
    Posts
    52
    still failing when a sequential of 2 repeated numbers is in array.
    look: ...28,28....

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int rptds[] = { 31,17,22,25,28,13,20,20,25,28,28,32 };
    	int a,b,c,d,e;
    	a=b=c=d=e=0;
    	
    	a = sizeof(rptds)/sizeof(int)-1;
    	printf("0 -> %d\n",a);
    	for (b=0;b<=a; b++) printf("%d|",rptds[b]);
    	
    	
    	for (b=0; b<=a; b++)
    	{
    		for (c=b+1; c<=a; c++)
    		{
    			if (rptds[b]==rptds[c])
    			{
    				for (d=c; d<a; d++)
    					rptds[d]=rptds[d+1];
    				a--;
    			}
    		}
    	}
    	puts("\n--------------------\n");
    	printf("0 -> %d\n",a);
    	for (b=0;b<=a; b++) printf("%d|",rptds[b]);
    	return(0);
    	
    }
    just compile it. you will see easily.

  13. #13
    Registered User
    Join Date
    Jan 2003
    Posts
    52
    finish!

    where is a--; must be a--; c--;

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Just for fun, here's a more generic way of find the number of elements in an array:
    Code:
    #include <stdio.h>
    
    #define ASIZE(a) (sizeof(a) / sizeof(*a))
    
    int main(void) 
    {
      int myarray[10];
      printf ("There are %d elements in the array\n", ASIZE(myarray));
      return 0;
    }
    
    /*
     * Output:
     There are 10 elements in the array
     *
     */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM