Like Tree19Likes

Array Comparison and Modification

This is a discussion on Array Comparison and Modification within the C Programming forums, part of the General Programming Boards category; Originally Posted by R.Stiltskin Still working on this, I see. I was hoping that by the time you finished implementing ...

  1. #61
    Registered User
    Join Date
    Dec 2012
    Posts
    32
    Quote Originally Posted by R.Stiltskin View Post
    Still working on this, I see. I was hoping that by the time you finished implementing the step-counting you would have realized how simply (and efficiently) you could complete the assignment with only a minor modification to your original program. Does this give you a hint?
    Code:
    #include <stdio.h>#include <stdlib.h>
    #define MAXVALUE 10
    int compact (int[], int);
    int main()
    {
    	int arrSeries[MAXVALUE], i=0, newArrayLength;
    	int arrLength = MAXVALUE;
    	printf("Please enter %d numbers.\n", MAXVALUE);
    	while(i != MAXVALUE)
    	{
    		scanf("%d", &arrSeries[i]);
    		i++;
    	}
        newArrayLength=compact(arrSeries, arrLength);
        printf("The new array length is %d.\n", newArrayLength);
        getchar();
        return 0;
    }
    int compact(int arrSeries[], int arrLength)
    {
    	int i=0, j= 0, counter=0, newArray[MAXVALUE];
        newArray[0]=arrSeries[0];
        for(i = 0; i != arrLength; i++)
            {
                if(newArray[j] != arrSeries[i])
                {
                    newArray[++j] = arrSeries[i];
                }
            }
        j++;
        while(counter < j)
            {
                printf("%d  ", newArray[counter]);
                counter++;
            }
    	printf("\nThe number of steps: %d.\n", j);
    	return j;
    }
    Isn't this the solution you intended? Or is there an even more elegant method other than in place compacting ?
    Thanks.

  2. #62
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    Since you have newArray, that is not in-place. But as I said, it looks like you're close. Remove newArray. How would you adjust your algorithm to work with just arrSeries?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #63
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    I showed how to solve this kind of problem efficiently in a recent thread. Basicaly you use destination and source "iterators", copying from one to the other (even though they may initially point to the same item), and only advancing the source iterator in some cases.

    Your case is slightly different here. The above should just give you the idea, and you'll still have to work out the exact the details yourself.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #64
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    In the current code attempts, the "iterators" are basically the indices. The idea is already there and implemented, just that it isn't yet in-place
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #65
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Oh so it is.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #66
    Registered User
    Join Date
    Dec 2012
    Posts
    32
    Quote Originally Posted by laserlight View Post
    Code:
    (i < max_size && scanf("%d", &numbers[i]) == 1)
    Sorry for my so less experience in coding, but I have never seen a while loop number reading like this. Can you please explain the logic so that I can implement loops like this myself? The implementation seems very elegant to me. I know that a while(1) loop is an infinite loop, but I don't' get the ==1 testing part.
    Thanks !.

  7. #67
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    Basically, the idea is to keep reading integers until the max_size is reached, or there are no more integers left to read. Read up on scanf to understand its return value.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #68
    Registered User
    Join Date
    Dec 2012
    Posts
    32
    Quote Originally Posted by laserlight View Post
    Basically, the idea is to keep reading integers until the max_size is reached, or there are no more integers left to read. Read up on scanf to understand its return value.
    Sorry for being late, here's the solution that I did, don't know how efficient it is...

    Code:
    #include <stdio.h>#include <stdlib.h>
    size_t read_numbers(int numbers[], int max_size)
    {
        size_t i = 0;
        printf("Please enter up to %d integers (trigger EOF to stop early).\n", max_size);
        while (i < max_size && scanf("%d", &numbers[i]) == 1)
        {
            ++i;
        }
        return i;
    }
    
    
    void print_numbers(const int numbers[], size_t size)
    {
        size_t i;
        printf(">>> ");
        for (i = 0; i < size; ++i)
        {
            printf("%d ", numbers[i]);
        }
        printf("<<<\n");
    }
    
    
    size_t compact(int numbers[], size_t size)
    {
        size_t i, j=0, k=0;
        if (size <= 1)
        {
            return size;
        }
        else
        {
            for(i=(size-1); i>0; i--)
            {
                if(numbers[i-1]==numbers[i])
                {
    				k=i;
    				while(k!=size)
    				{
                    numbers[k-1]=numbers[k];
    				k++;
    				}
    				j++;
                }
            }
    		size=size-j;
    		return (size);
        }
    }
    
    
    int main(void)
    {
        int numbers[10];
        size_t size = read_numbers(numbers, sizeof(numbers) / sizeof(numbers[0]));
        size = compact(numbers, size);
        print_numbers(numbers, size);
    	system("pause");
        return 0;
    }

  9. #69
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    In the worst case, it's still O(n2).

    You would do better to go back to your compact function in post #61 and make it in-place by simplifying it, rather than by complicating it. Essentially all you have to do is eliminate counter and newArray.

  10. #70
    Registered User
    Join Date
    Dec 2012
    Posts
    32
    Quote Originally Posted by R.Stiltskin View Post
    In the worst case, it's still O(n2).

    You would do better to go back to your compact function in post #61 and make it in-place by simplifying it, rather than by complicating it. Essentially all you have to do is eliminate counter and newArray.
    Will try to do the same.

Page 5 of 5 FirstFirst 12345
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character Array Comparison
    By programit in forum C Programming
    Replies: 2
    Last Post: 01-31-2011, 11:30 AM
  2. Help on array comparison
    By sivapc in forum C++ Programming
    Replies: 6
    Last Post: 11-10-2009, 08:54 AM
  3. Scanf confusion, 2 dimensional array modification
    By Leojeen in forum C Programming
    Replies: 23
    Last Post: 10-19-2008, 10:58 PM
  4. array comparison
    By cloudy in forum C Programming
    Replies: 2
    Last Post: 10-16-2004, 02:45 PM
  5. array comparison
    By battoujutsu in forum C Programming
    Replies: 12
    Last Post: 12-05-2003, 10:47 AM

Tags for this Thread


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