Thread: Array Comparison and Modification

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    32

    Array Comparison and Modification

    This is what my code is intended to do: When called with the following array:
    Code:
    { 1, 1, 1, 2, 2, 1, 2, 3, 3, 1, 1 }
    , your function should modify the array to
    Code:
    {1, 2, 1, 2, 3, 1}
    and store it.

    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];
        newArray[0]=arrSeries[0];
        for(i = 1; i != arrLength; i++)
        {
            if(newArray[j] != arrSeries[i-1]);
            {
                newArray[++j] = arrSeries[i];
            }
    
    
        }
    
    
        while(counter !=6)
        {
            printf("%d  ", newArray[counter]);
            counter++;
        }
    
    
    }
    However it is not working as intended. It works as if the IF statement doesn't even exist ! Where am I going wrong?
    Thanks
    Last edited by Debojyoti Das; 12-21-2012 at 08:31 AM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    if(newArray[j] != arrSeries[i-1]);
    That semi-colon at the end of your "if()" statement is making that statement do nothing.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    32
    Quote Originally Posted by Matticus View Post
    Code:
    if(newArray[j] != arrSeries[i-1]);
    That semi-colon at the end of your "if()" statement is making that statement do nothing.
    Oops ! That was a typo...edited and recompiled the code. Still No luck Gives erroneous results: 1 2 2 3 1 2686632

    Also how should I modify the code to satisfy this problem?

    Write a function compact ( ) which takes as parameters an array of integers and its length.The function should modify the array, so that all consecutive
    occurrences of the same; integers are replaced by a single occurrence of that integer.
    The function should return the length of this new array.
    Last edited by Debojyoti Das; 12-21-2012 at 08:22 AM.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Quote Originally Posted by Debojyoti Das View Post
    Code:
            if(newArray[j] != arrSeries[i-1])
    Are you sure you want to compare to element [i-1]?

    PS: when you see entries like 2686632 popping up, you should suspect that you might be running out of the bounds of your array somewhere.

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    32
    Quote Originally Posted by R.Stiltskin View Post
    Are you sure you want to compare to element [i-1]?

    PS: when you see entries like 2686632 popping up, you should suspect that you might be running out of the bounds of your array somewhere.
    Yeah I thought that ! Thanks ! I got the error !

    Code:
    for(i = 0; i != arrLength; i++)    {
            if(newArray[j] != arrSeries[i])
            {
                newArray[++j] = arrSeries[i];
            }
    
    
        }
    gives correct output.

    But another question, as arrays are static data structures in C, is this question solvable using C?

    Write a function compact ( ) which takes as parameters an array of integers and its length.The function should modify the array, so that all consecutive
    occurrences of the same; integers are replaced by a single occurrence of that integer.
    The function should return the length of this new array.

    Thanks Again

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Of course it's solvable. Think.

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    32
    Quote Originally Posted by R.Stiltskin View Post
    Of course it's solvable. Think.
    Are you suggesting I use dynamic memory allocation?

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Many solutions.
    One would be to create dynamically a new array and fill it with the elements. But your function returns only the length of the array....
    So are you sure you need to create a new array??
    The requirement says to modify the array!
    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

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Debojyoti Das View Post
    Are you suggesting I use dynamic memory allocation?
    Say you were, for a moment, the tip of the pen, and you start with index 0 of this array:
    Code:
    int arrSeries[11] = {1, 1, 1, 2, 2, 1, 2, 3, 3, 1, 1 };
    You look ahead, and see the next number is the same as your current number.

    What would you have to do to get rid of that current number, and move all the higher numbers, down by one index?

    Work on it with paper and pen, and see what patterns of logic you need to follow to make this happen. Repeat it a few times, and the patterns will become evident.

    And that's the backbone of your programs logic. Don't let someone give you the answer - work it out.

  10. #10
    Registered User
    Join Date
    Dec 2012
    Posts
    32
    Quote Originally Posted by std10093 View Post
    Many solutions.
    One would be to create dynamically a new array and fill it with the elements. But your function returns only the length of the array....
    So are you sure you need to create a new array??
    The requirement says to modify the array!
    I assume that means I should create the first array (that keeps the repeating number dynamically so that I can modify it later). Trying to code it now..

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Adak View Post
    And that's the backbone of your programs logic. Don't let someone give you the answer - work it out.
    Tip to remeber forever!
    Quote Originally Posted by Debojyoti Das View Post
    I assume that means I should create the first array (that keeps the repeating number dynamically so that I can modify it later). Trying to code it now..
    I am not sure what you want to accomplish. I would suggest to follow Adak's advice. But before doing that read again and again the requirement!
    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. #12
    Registered User
    Join Date
    Dec 2012
    Posts
    32
    Quote Originally Posted by std10093 View Post
    Tip to remeber forever!

    I am not sure what you want to accomplish. I would suggest to follow Adak's advice. But before doing that read again and again the requirement!
    Sorry I overlooked Adak's post while refreshing the page. I get the logic, I will simply shift the digits and throw out the repeating digits...say

    Array s -> 1 1 1 2 2 3 1 4 1
    Fisrt Pass -> Check Index 0 and 1 > Index 0 is top -> Pop index 0 -> Set index 1 to s.top.
    Next Pass - > 1 1 2 2 3 1 4 1
    Next Pass - > 1 2 2 3 1 4 1 g
    Next Pass - > 1 2 3 1 4 1 g g

    now the indices with g has the previous value or I may set it to 0. Will that be the solution?

  13. #13
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Nice explanation and nice idea Bravo. Try it out and post back even though it work!! I am eager to see the result
    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. #14
    Registered User
    Join Date
    Dec 2012
    Posts
    32
    Quote Originally Posted by std10093 View Post
    Nice explanation and nice idea Bravo. Try it out and post back even though it work!! I am eager to see the result
    Thanks ! Working on it !

  15. #15
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Quote Originally Posted by Debojyoti Das View Post
    Sorry I overlooked Adak's post while refreshing the page. I get the logic, I will simply shift the digits and throw out the repeating digits...say

    Array s -> 1 1 1 2 2 3 1 4 1
    Fisrt Pass -> Check Index 0 and 1 > Index 0 is top -> Pop index 0 -> Set index 1 to s.top.
    Next Pass - > 1 1 2 2 3 1 4 1
    Next Pass - > 1 2 2 3 1 4 1 g
    Next Pass - > 1 2 3 1 4 1 g g

    now the indices with g has the previous value or I may set it to 0. Will that be the solution?
    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.

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