Thread: Array Comparison and Modification

  1. #31
    Registered User
    Join Date
    Dec 2012
    Posts
    32
    Quote Originally Posted by std10093 View Post
    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.
    Yes j has the same value, thank you for making me notice . Changed the code and also added data acceptance from user:

    Code:
    #include <stdio.h>#include <stdlib.h>
    #define MAXVALUE 10
    void compact (int[], int);
    int main()
    {
    	int arrSeries[MAXVALUE], i=0;
    	int arrLength = MAXVALUE;
    	printf("Please enter %d numbers.\n", MAXVALUE);
    	while(i != MAXVALUE)
    	{
    		scanf("%d", &arrSeries[i]);
    		i++;
    	}
        compact(arrSeries, arrLength);
        getchar();
        return 0;
    }
    
    
    
    
    void compact(int arrSeries[], int arrLength)
    {
    	int i, j= 0, counter=0, newArray[MAXVALUE]={0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        newArray[0]=arrSeries[0];
        for(i = 0; i != arrLength; i++)    {
            if(newArray[j] != arrSeries[i])
            {
                newArray[++j] = arrSeries[i];
            }
        }
    
    
    	while(counter !=MAXVALUE)
        {
            printf("%d  ", newArray[counter]);
            counter++;
        }
    	printf("\nThe number of steps: %d", j);
    	getchar();
    }
    Last edited by Debojyoti Das; 12-22-2012 at 07:22 AM.

  2. #32
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You are welcome. Now the only problem that I can see is when the user inputs as last numbers zeroes.. But still I would be happy with the code
    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

  3. #33
    Registered User
    Join Date
    Dec 2012
    Posts
    32
    Quote Originally Posted by std10093 View Post
    You are welcome. Now the only problem that I can see is when the user inputs as last numbers zeroes.. But still I would be happy with the code
    Thanks . Maybe I will prompt user not to enter 0's .

  4. #34
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    That would be a nice solution
    Or you could find the minimum element that the user inputted. You could that without increasing complexity.

    How? When doing the scanf, you could find the min

    Then you could pass this min as third argument to your function and initialize the array to this value min instead of zeroes. What is your opinion on that?
    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

  5. #35
    Registered User
    Join Date
    Dec 2012
    Posts
    32
    Quote Originally Posted by std10093 View Post
    That would be a nice solution
    Or you could find the minimum element that the user inputted. You could that without increasing complexity.

    How? When doing the scanf, you could find the min

    Then you could pass this min as third argument to your function and initialize the array to this value min instead of zeroes. What is your opinion on that?
    That's a great idea ! Atleast the user won't need to see zeroes and garbage values . Thanks.

  6. #36
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Correct! You will print until you find the min Go ahead and code!
    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

  7. #37
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Adjust the number of good values, as you remove the repeated values. Print only the good values!

  8. #38
    Registered User
    Join Date
    Dec 2012
    Posts
    32
    Quote Originally Posted by std10093 View Post
    Correct! You will print until you find the min Go ahead and code!
    Quote Originally Posted by Adak View Post
    Adjust the number of good values, as you remove the repeated values. Print only the good values!
    Well a new train of thought came to me. Why not I just remember the last digit in the array of number the user inputs, fill the array say (last digit+1) and during result output truncate all digits from and after last (digit + 1). Will that be more efficient?

    Example:

    User Inputs 1 1 1 2 2 3 3 3 4 5 5
    Next Pass -> Last Digit = 5, Initialize Array to 6 6 6 6 6 6 6 6 6 6
    Next Pass -> Compact Array 1 2 3 4 5 6 g g g g (g=garbage value)
    Next Pass -> Print Before First Appearance of Last Digit Print 1 2 3 4 5

    That way I can skip the following tests :

    i.) MAXVALUE comparisons to find Minimum Digits.
    ii.) Test to check if the last repeating/Non-repeating digits were the minimum value.
    |----------------> if TRUE -----> Keep One Instance of Last Digit.

    Please comment as I may be wrong and I am just a beginner in coding. Thanks.

  9. #39
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Hmm... what is this thing about minimum/maximum digits? Your function should return the length of the reduced range, right? The caller is then responsible for working with the reduced range. Trying to set the "removed" part of the original array/range to some designated garbage value is unnecessary.

    Incidentally, if you ever use C++ instead of C, check out the C++ standard library's std::unique generic algorithm for something along the lines of what you are supposed to do here.

    EDIT:
    ARGH! Looking at your post #31, you're writing a function that does too much. Let the compact function compact the array to a reduced range and return the length of the reduced range. It should not print anything at all.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #40
    Registered User
    Join Date
    Dec 2012
    Posts
    32
    Quote Originally Posted by laserlight View Post
    Hmm... what is this thing about minimum/maximum digits? Your function should return the length of the reduced range, right? The caller is then responsible for working with the reduced range. Trying to set the "removed" part of the original array/range to some designated garbage value is unnecessary.

    Incidentally, if you ever use C++ instead of C, check out the C++ standard library's std::unique generic algorithm for something along the lines of what you are supposed to do here.

    EDIT:
    ARGH! Looking at your post #31, you're writing a function that does too much. Let the compact function compact the array to a reduced range and return the length of the reduced range. It should not print anything at all.
    True the actual question doesn't want me to print the compact array but just for my programming practice I took on the (ARGH! ) task . Thanks for the heads up on C++, I am yet to learn that . I know C# a bit and I know there is a function for the same Distinct()).
    Last edited by Debojyoti Das; 12-22-2012 at 09:27 AM.

  11. #41
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Debojyoti Das View Post
    Well a new train of thought came to me. Why not I just remember the last digit in the array of number the user inputs, fill the array say (last digit+1) and during result output truncate all digits from and after last (digit + 1). Will that be more efficient?

    Example:

    User Inputs 1 1 1 2 2 3 3 3 4 5 5
    Next Pass -> Last Digit = 5, Initialize Array to 6 6 6 6 6 6 6 6 6 6
    Next Pass -> Compact Array 1 2 3 4 5 6 g g g g (g=garbage value)
    Next Pass -> Print Before First Appearance of Last Digit Print 1 2 3 4 5

    That way I can skip the following tests :

    i.) MAXVALUE comparisons to find Minimum Digits.
    ii.) Test to check if the last repeating/Non-repeating digits were the minimum value.
    |----------------> if TRUE -----> Keep One Instance of Last Digit.

    Please comment as I may be wrong and I am just a beginner in coding. Thanks.
    It does not sound like a bad idea. Don't be afraid to try. From our mistakes we learn
    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. #42
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Taking on that task is fine: in fact, I probably would have done it even before implementing the function because it gives me an easy way to verify that the function works. The thing is, the task should be performed in the main function, or another function that calls compact. Later on, you might write automated tests that call compact with some input and checks that the output is as expected. In such a case, you would be printing the success/failure of those unit tests rather than the contents of the array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #43
    Registered User
    Join Date
    Dec 2012
    Posts
    32
    Here's the final code:

    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.", newArrayLength);
        getchar();
        return 0;
    }
    int compact(int arrSeries[], int arrLength)
    {
    	int i=0, j= 0, k=MAXVALUE, counter=0, newArray[MAXVALUE], lastVal, readCounter=MAXVALUE;
    	lastVal=arrSeries[MAXVALUE];
    	lastVal++;
    	while(i!=MAXVALUE)
        {
            newArray[i]=lastVal;
            i++;
        }
        newArray[0]=arrSeries[0];
        for(i = 0; i != arrLength; i++)
            {
                if(newArray[j] != arrSeries[i])
                {
                    newArray[++j] = arrSeries[i];
                }
            }
        while(k!=0)
        {
            if(newArray[k]==lastVal)
            {
                readCounter--;
            }
            k--;
        }
        while(counter <= readCounter)
            {
                printf("%d  ", newArray[counter]);
                counter++;
            }
    	printf("\nThe number of steps: %d.\n", j);
    	return ++readCounter;
    }
    It addresses the main problem as well by returning the array length . Please comment .
    Last edited by Debojyoti Das; 12-22-2012 at 09:46 AM. Reason: Formatting.

  14. #44
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Okay, looking through this thread again, I see that other than your solution in post #19, which is admittedly more complicated than necessary, your solutions use an extra array when this can be done in-place. Why not try for a simpler in-place solution? It seems that you are close. You could re-read Adak's post #9, but note that "move all the higher numbers, down by one index" is an unnecessary step.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #45
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    It seems ok

    I would throw away k and use i in lines 42 to 49.
    Like this
    Code:
     i = MAXVALUE;
     while(i!=0)
        {
            if(newArray[i]==lastVal)
            {
                readCounter--;
            }
            i--;
        }
    Another thing I would change is this
    Code:
    while(i!=MAXVALUE)
        {
            newArray[i]=lastVal;
            i++;
        }
    to this (since we know how many loops are going to be done, we should use for loop. Using while loop is not wrong. )
    Code:
    for( i = 0 ; i < MAXVALUE i++)
    {
            newArray[i]=lastVal;
    }
    As a result, I would change also the part that I used i instead of k.
    Code:
     i = MAXVALUE;
     while(i!=0)
        {
            if(newArray[i]==lastVal)
            {
                readCounter--;
            }
            i--;
        }
    to this
    Code:
     i = MAXVALUE;
     for( i = MAXVALUE ; i > 0 ; i--)
    {
            if(newArray[i]==lastVal)
            {
                readCounter--;
            }
    }

    I run it without changing your code and I got
    Code:
    Macintosh-c8bcc88e5669-9:~ usi$ ./px
    Please enter 10 numbers.
    1
    1
    1
    1
    14
    5
    6
    7
    3
    4
    1  14  5  6  7  3  4  1693429850  
    The number of steps: 6.
    The new array length is 8.
    Also when you print the new array length put a newline to the printf
    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

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, 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, 11:47 AM

Tags for this Thread