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 That's a viable approach and certainly worth doing, if only for the benefit of the programming ...

  1. #16
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by R.Stiltskin View Post
    That's a viable approach and certainly worth doing, if only for the benefit of the programming practice it gives you. However, think about how many operations the computer will be performing as compared to your original program. To me, it seems a horribly inefficient way to solve an extremely simple problem. So after you finish that, you should go back to the original version and think about it some more. When you come to your `Eureka' moment you'll see what I mean.
    While I do not fully disagree, tell me about the time complexity of the operation that Debojyoti is going to make. Of course it's not optimum, but he is probably a beginner in C.

    By the way I didn't know that you say Eureka too
    Last edited by std10093; 12-21-2012 at 09:51 AM.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  2. #17
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    We shouldn't turn this into a debate about complexity, but doesn't repeatedly shifting the entire array change it from O(n) to O(n2)?

    But even ignoring the time complexity, it makes the program itself much more complicated than is necessary.

    Oh, and some of us have actually heard of a fellow named Archimedes as well!

  3. #18
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Exactly

    Of course you do
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #19
    Registered User
    Join Date
    Dec 2012
    Posts
    32
    Quote Originally Posted by std10093 View Post
    Exactly

    Of course you do
    Here's my solution (it work's ) but it became quite complex and due to the shifting the garbage value also shows.
    Now will think on R.Stiltskin's idea.

    Code:
    #include <stdio.h>#include <stdlib.h>
    #define MAXSIZE 10
    int compact(int[]);
    int main()
    {
        int num=0, numarray[MAXSIZE]={0,0,0,0,0,0,0,0,0,0}, i=0;
        int top = -1;
        printf("Please enter some numbers. Enter -1 to stop.\n");
        while(num != -1)
        {
            scanf("%d", &num);
            if(top==(MAXSIZE-1) || num == -1)
            {
                printf("Done !\n");
                break;
            }
            else
            {
                top+=1;
                numarray[top] = num;
            }
        }
    
    
        while(i<MAXSIZE)
        {
            printf("%d ", numarray[i]);
            i++;
        }
        printf("\nPrinting compact numvers.\n");
        compact(numarray);
        getchar();
        return 0;
    }
    int compact(int changeArray[MAXSIZE])
    {
        int i=0, j=0, k=0, l=0;
        while(l<MAXSIZE)
        {
            i=0, j=0;
            while(i < MAXSIZE)
            {
                if(changeArray[i]==changeArray[i+1])
                {
                    j=i;
                    while(j < MAXSIZE)
                    {
                        changeArray[j]=changeArray[j+1];
                        j++;
                    }
                }
    
    
                    i++;
            }
            l++;
        }
    
    
         while(k<MAXSIZE)
         {
             printf("%d ", changeArray[k]);
             k++;
         }
         getchar();
         return 0;
    }
    I did not implement the size returning function as I am not very satisfied with the garbage values creeping in. Maybe I will modify to store 0/-1 instead of garbage and check conflict if user inputs the same.
    Last edited by Debojyoti Das; 12-21-2012 at 01:45 PM.

  5. #20
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Spelling
    Code:
    printf("\nPrinting compact numvers.\n");
    Of course take a shot in R.Stiltskin's idea.
    Debojyoti Das likes this.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #21
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Nice job. Now why don't you modify the program so it counts (and prints) the number of assignments performed (i.e., how many times a number is moved from one place to another) while processing a given input. Then do the same thing with your previous program and compare the number of operations for the same input array.
    Debojyoti Das likes this.

  7. #22
    Registered User
    Join Date
    Dec 2012
    Posts
    32
    Quote Originally Posted by R.Stiltskin View Post
    Nice job. Now why don't you modify the program so it counts (and prints) the number of assignments performed (i.e., how many times a number is moved from one place to another) while processing a given input. Then do the same thing with your previous program and compare the number of operations for the same input array.
    You are recommending me to find out it's time complexity. Sure thing doing it now .

  8. #23
    Registered User
    Join Date
    Dec 2012
    Posts
    32
    Quote Originally Posted by std10093 View Post
    Spelling
    Code:
    printf("\nPrinting compact numvers.\n");
    Of course take a shot in R.Stiltskin's idea.
    Why did they keep v and b adjacently on the keyboard ??

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Quote Originally Posted by Debojyoti Das
    Why did they keep v and b adjacently on the keyboard ??
    Shh... this is the C programming forum, not VB
    Debojyoti Das likes this.
    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

  10. #25
    Registered User
    Join Date
    Dec 2012
    Posts
    32
    Code:
    while(l<MAXSIZE)	{
    		i=0, j=0;
    		while(i < MAXSIZE)
    		{
    			if(changeArray[i]==changeArray[i+1])
    			{
    				j=i;
    				while(j < MAXSIZE)
    				{
    					changeArray[j]=changeArray[j+1];
    					j++;
    				}
    
    
    				stepcounter++;
    			}
    
    
    				i++;
    		}
    		l++;
    	}
    So, I added a step-counter here and also here:

    Code:
    for(i = 0; i != arrLength; i++)    {        if(newArray[j] != arrSeries[i])
            {
                newArray[++j] = arrSeries[i];
    			stepcounter++;
            }
    Attachment 12372

    Attachment 12373

    No doubt my previous code was much more efficient than this one. Trying to modify the previous code to make it work dynamically .

  11. #26
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Your attachments do not lead me anywhere :/
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  12. #27
    Registered User
    Join Date
    Dec 2012
    Posts
    32
    Quote Originally Posted by std10093 View Post
    Your attachments do not lead me anywhere :/
    It got deleted for reasons I don't know

    1 2 1 2 3 1
    The number of steps: 5

    1 1 1 2 2 1 3 3 1 1
    Printing compact numbers.
    1 2 1 3 1 1 1 1 1 1
    Steps Performed: 63
    Well it's the number of steps my first and second piece of code does for similar set of numbers. In fact the second piece of code was provided a number less yet it took substantially more steps to compute than the first step, which did the computation with one number more in the series in so less steps. Sorry for being ambiguous in my previous post .
    Last edited by Debojyoti Das; 12-22-2012 at 06:59 AM.

  13. #28
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    No problem

    So, can you post the code that you think is the best you have until now?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  14. #29
    Registered User
    Join Date
    Dec 2012
    Posts
    32
    Quote Originally Posted by std10093 View Post
    No problem

    So, can you post the code that you think is the best you have until now?
    Code:
    #include <stdio.h>#include <stdlib.h>
    void compact (int[], int);
    int main()
    {
        int arrSeries[11] = {1, 1, 1, 2, 2, 1, 2, 3, 3, 1, 1 };
        int arrLength = 11;
        compact(arrSeries, arrLength);
        getchar();
        return 0;
    }
     
     
    void compact(int arrSeries[], int arrLength)
    {
        int i, j= 0, counter=0, newArray[11], stepcounter = 0;
        newArray[0]=arrSeries[0];
        for(i = 0; i != arrLength; i++)    {
            if(newArray[j] != arrSeries[i])
            {
                newArray[++j] = arrSeries[i];
    			stepcounter++;
            } 
        }
    
    
        while(counter !=6)
        {
            printf("%d  ", newArray[counter]);
            counter++;
        }
    	printf("\nThe number of steps: %d", stepcounter);
     
    }
    This one, but it is highly restricted as it works only with a static data set.

  15. #30
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Only one for loop in the function which is good. Well do you really need stepcounter? I would say that j has the same value with stepcounter when you reach line 31.
    Debojyoti Das likes this.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Page 2 of 5 FirstFirst 12345 LastLast
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, 12:30 PM
  2. Help on array comparison
    By sivapc in forum C++ Programming
    Replies: 6
    Last Post: 11-10-2009, 09:54 AM
  3. Scanf confusion, 2 dimensional array modification
    By Leojeen in forum C Programming
    Replies: 23
    Last Post: 10-19-2008, 11:58 PM
  4. array comparison
    By cloudy in forum C Programming
    Replies: 2
    Last Post: 10-16-2004, 03:45 PM
  5. array comparison
    By battoujutsu in forum C Programming
    Replies: 12
    Last Post: 12-05-2003, 11: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